2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-07-21 16:43:24 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <osg/GraphicsContext>
|
2005-07-25 22:28:22 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <map>
|
2006-12-23 05:53:44 +08:00
|
|
|
#include <sstream>
|
2005-07-21 16:43:24 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2006-12-18 04:49:01 +08:00
|
|
|
static ref_ptr<GraphicsContext::WindowingSystemInterface> s_WindowingSystemInterface;
|
2005-07-21 16:43:24 +08:00
|
|
|
|
2006-12-18 04:49:01 +08:00
|
|
|
void GraphicsContext::setWindowingSystemInterface(WindowingSystemInterface* callback)
|
2005-07-21 16:43:24 +08:00
|
|
|
{
|
2006-12-18 04:49:01 +08:00
|
|
|
s_WindowingSystemInterface = callback;
|
2005-07-21 16:43:24 +08:00
|
|
|
}
|
|
|
|
|
2006-12-18 04:49:01 +08:00
|
|
|
GraphicsContext::WindowingSystemInterface* GraphicsContext::getWindowingSystemInterface()
|
2005-07-21 16:43:24 +08:00
|
|
|
{
|
2006-12-18 04:49:01 +08:00
|
|
|
return s_WindowingSystemInterface.get();
|
2005-07-21 16:43:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext* GraphicsContext::createGraphicsContext(Traits* traits)
|
|
|
|
{
|
2006-12-18 04:49:01 +08:00
|
|
|
if (s_WindowingSystemInterface.valid())
|
|
|
|
return s_WindowingSystemInterface->createGraphicsContext(traits);
|
2005-07-21 16:43:24 +08:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
|
2006-12-23 05:53:44 +08:00
|
|
|
std::string GraphicsContext::ScreenIdentifier::displayName() const
|
|
|
|
{
|
|
|
|
std::stringstream ostr;
|
|
|
|
ostr<<hostName<<":"<<displayNum<<"."<<screenNum;
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
typedef std::map<unsigned int, unsigned int> ContextIDMap;
|
|
|
|
static ContextIDMap s_contextIDMap;
|
|
|
|
static OpenThreads::Mutex s_contextIDMapMutex;
|
|
|
|
|
|
|
|
unsigned int GraphicsContext::createNewContextID()
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
|
|
|
// first check to see if we can reuse contextID;
|
|
|
|
for(ContextIDMap::iterator itr = s_contextIDMap.begin();
|
|
|
|
itr != s_contextIDMap.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->second == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
// reuse contextID;
|
|
|
|
itr->second = 1;
|
|
|
|
|
2005-12-06 04:11:50 +08:00
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::createNewContextID() reusing contextID="<<itr->first<<std::endl;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
|
|
|
return itr->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int contextID = s_contextIDMap.size();
|
|
|
|
s_contextIDMap[contextID] = 1;
|
|
|
|
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::createNewContextID() creating contextID="<<contextID<<std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
if ( (contextID+1) > osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts() )
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Updating the MaxNumberOfGraphicsContexts to "<<contextID+1<<std::endl;
|
|
|
|
|
|
|
|
// update the the maximum number of graphics contexts,
|
|
|
|
// to ensure that texture objects and display buffers are configured to the correct size.
|
|
|
|
osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts( contextID + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return contextID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::incrementContextIDUsageCount(unsigned int contextID)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::incrementContextIDUsageCount("<<contextID<<") to "<<s_contextIDMap[contextID]<<std::endl;
|
|
|
|
|
|
|
|
++s_contextIDMap[contextID];
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID)
|
|
|
|
{
|
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
|
|
|
|
|
|
|
if (s_contextIDMap[contextID]!=0)
|
|
|
|
{
|
|
|
|
--s_contextIDMap[contextID];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Warning: decrementContextIDUsageCount("<<contextID<<") called on expired contextID."<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::decrementContextIDUsageCount("<<contextID<<") to "<<s_contextIDMap[contextID]<<std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-21 16:43:24 +08:00
|
|
|
GraphicsContext::GraphicsContext():
|
|
|
|
_threadOfLastMakeCurrent(0)
|
|
|
|
{
|
2006-12-22 00:56:20 +08:00
|
|
|
_operationsBlock = new Block;
|
2005-07-21 16:43:24 +08:00
|
|
|
}
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
GraphicsContext::~GraphicsContext()
|
|
|
|
{
|
2005-08-30 22:41:08 +08:00
|
|
|
close(false);
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Realise the GraphicsContext.*/
|
|
|
|
bool GraphicsContext::realize()
|
|
|
|
{
|
|
|
|
if (realizeImplementation())
|
|
|
|
{
|
|
|
|
if (_graphicsThread.valid() && !_graphicsThread->isRunning())
|
|
|
|
{
|
|
|
|
_graphicsThread->startThread();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-25 22:28:22 +08:00
|
|
|
}
|
|
|
|
|
2005-08-30 22:41:08 +08:00
|
|
|
void GraphicsContext::close(bool callCloseImplementation)
|
2005-08-20 16:59:03 +08:00
|
|
|
{
|
|
|
|
// switch off the graphics thread...
|
|
|
|
setGraphicsThread(0);
|
|
|
|
|
2005-08-30 22:41:08 +08:00
|
|
|
if (callCloseImplementation) closeImplementation();
|
|
|
|
|
|
|
|
if (_state.valid())
|
|
|
|
{
|
|
|
|
decrementContextIDUsageCount(_state->getContextID());
|
|
|
|
|
|
|
|
_state = 0;
|
|
|
|
}
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-19 04:17:51 +08:00
|
|
|
void GraphicsContext::makeCurrent()
|
|
|
|
{
|
|
|
|
ReleaseContext_Block_MakeCurrentOperation* rcbmco = 0;
|
|
|
|
|
|
|
|
if (_graphicsThread.valid() &&
|
2005-08-20 16:59:03 +08:00
|
|
|
_threadOfLastMakeCurrent == _graphicsThread.get() &&
|
|
|
|
_threadOfLastMakeCurrent != OpenThreads::Thread::CurrentThread())
|
2005-08-19 04:17:51 +08:00
|
|
|
{
|
|
|
|
// create a relase contex, block and make current operation to stop the graphics thread while we use the graphics context for ourselves
|
|
|
|
rcbmco = new ReleaseContext_Block_MakeCurrentOperation;
|
|
|
|
_graphicsThread->add(rcbmco);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCurrent()) _mutex.lock();
|
|
|
|
|
|
|
|
makeCurrentImplementation();
|
|
|
|
|
|
|
|
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
|
|
|
|
|
|
|
|
if (rcbmco)
|
|
|
|
{
|
|
|
|
// Let the "relase contex, block and make current operation" proceed, which will now move on to trying to aquire the graphics
|
|
|
|
// contex itself with a makeCurrent(), this will then block on the GraphicsContext mutex till releaseContext() releases it.
|
|
|
|
rcbmco->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
|
|
|
|
{
|
|
|
|
if (!isCurrent()) _mutex.lock();
|
|
|
|
|
|
|
|
makeContextCurrentImplementation(readContext);
|
|
|
|
|
|
|
|
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::releaseContext()
|
|
|
|
{
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
2005-08-20 16:59:03 +08:00
|
|
|
|
|
|
|
void GraphicsContext::swapBuffers()
|
|
|
|
{
|
|
|
|
if (isCurrent())
|
|
|
|
{
|
|
|
|
swapBuffersImplementation();
|
|
|
|
}
|
|
|
|
else if (_graphicsThread.valid() &&
|
|
|
|
_threadOfLastMakeCurrent == _graphicsThread.get())
|
|
|
|
{
|
|
|
|
_graphicsThread->add(new SwapBuffersOperation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
makeCurrent();
|
|
|
|
swapBuffersImplementation();
|
|
|
|
releaseContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GraphicsContext::createGraphicsThread()
|
|
|
|
{
|
|
|
|
if (!_graphicsThread)
|
|
|
|
{
|
|
|
|
setGraphicsThread(new GraphicsThread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::setGraphicsThread(GraphicsThread* gt)
|
|
|
|
{
|
|
|
|
if (_graphicsThread==gt) return;
|
|
|
|
|
|
|
|
if (_graphicsThread.valid())
|
|
|
|
{
|
|
|
|
// need to kill the thread in some way...
|
|
|
|
_graphicsThread->cancel();
|
|
|
|
_graphicsThread->_graphicsContext = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_graphicsThread = gt;
|
|
|
|
|
|
|
|
if (_graphicsThread.valid())
|
|
|
|
{
|
|
|
|
_graphicsThread->_graphicsContext = this;
|
2006-12-23 01:46:21 +08:00
|
|
|
#if 0
|
2005-08-30 22:41:08 +08:00
|
|
|
if (!_graphicsThread->isRunning())
|
2005-08-20 16:59:03 +08:00
|
|
|
{
|
|
|
|
_graphicsThread->startThread();
|
|
|
|
}
|
2006-12-23 01:46:21 +08:00
|
|
|
#endif
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
}
|
2006-12-22 00:56:20 +08:00
|
|
|
|
|
|
|
void GraphicsContext::add(Operation* operation)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Doing add"<<std::endl;
|
|
|
|
|
|
|
|
// aquire the lock on the operations queue to prevent anyone else for modifying it at the same time
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
|
|
|
|
// add the operation to the end of the list
|
|
|
|
_operations.push_back(operation);
|
|
|
|
|
|
|
|
_operationsBlock->set(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::remove(Operation* operation)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Doing remove operation"<<std::endl;
|
|
|
|
|
|
|
|
// aquire the lock on the operations queue to prevent anyone else for modifying it at the same time
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
|
|
|
|
for(OperationQueue::iterator itr = _operations.begin();
|
|
|
|
itr!=_operations.end();)
|
|
|
|
{
|
|
|
|
if ((*itr)==operation) itr = _operations.erase(itr);
|
|
|
|
else ++itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_operations.empty())
|
|
|
|
{
|
|
|
|
_operationsBlock->set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::remove(const std::string& name)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Doing remove named operation"<<std::endl;
|
|
|
|
|
|
|
|
// aquire the lock on the operations queue to prevent anyone else for modifying it at the same time
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
|
|
|
|
// find the remove all operations with specificed name
|
|
|
|
for(OperationQueue::iterator itr = _operations.begin();
|
|
|
|
itr!=_operations.end();)
|
|
|
|
{
|
|
|
|
if ((*itr)->getName()==name) itr = _operations.erase(itr);
|
|
|
|
else ++itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_operations.empty())
|
|
|
|
{
|
|
|
|
_operationsBlock->set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::removeAllOperations()
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Doing remove all operations"<<std::endl;
|
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
_operations.clear();
|
|
|
|
_operationsBlock->set(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::runOperations()
|
|
|
|
{
|
|
|
|
for(OperationQueue::iterator itr = _operations.begin();
|
|
|
|
itr != _operations.end();
|
|
|
|
)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
_currentOperation = *itr;
|
|
|
|
|
|
|
|
if (!_currentOperation->getKeep())
|
|
|
|
{
|
|
|
|
itr = _operations.erase(itr);
|
|
|
|
|
|
|
|
if (_operations.empty())
|
|
|
|
{
|
|
|
|
_operationsBlock->set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_currentOperation.valid())
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Doing op "<<_currentOperation->getName()<<" "<<this<<std::endl;
|
|
|
|
|
|
|
|
// call the graphics operation.
|
|
|
|
(*_currentOperation)(this);
|
|
|
|
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
_currentOperation = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|