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>
|
2007-01-02 02:20:10 +08:00
|
|
|
#include <osg/Camera>
|
|
|
|
#include <osg/View>
|
|
|
|
|
2007-01-09 00:20:10 +08:00
|
|
|
#include <osg/FrameBufferObject>
|
|
|
|
#include <osg/Program>
|
|
|
|
#include <osg/Drawable>
|
|
|
|
#include <osg/FragmentProgram>
|
|
|
|
#include <osg/VertexProgram>
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
#include <OpenThreads/ReentrantMutex>
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
#include <osg/Notify>
|
2007-01-02 02:20:10 +08:00
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
#include <map>
|
2006-12-23 05:53:44 +08:00
|
|
|
#include <sstream>
|
2007-07-06 17:06:37 +08:00
|
|
|
#include <algorithm>
|
2005-07-21 16:43:24 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// GraphicsContext static method implementations
|
|
|
|
//
|
|
|
|
|
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;
|
2007-05-25 23:57:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::setWindowingSystemInterface() "<<s_WindowingSystemInterface.get()<<"\t"<<&s_WindowingSystemInterface<<std::endl;
|
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
|
|
|
{
|
2007-05-25 23:57:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::getWindowingSystemInterface() "<<s_WindowingSystemInterface.get()<<"\t"<<&s_WindowingSystemInterface<<std::endl;
|
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;
|
|
|
|
}
|
|
|
|
|
2007-08-03 22:50:58 +08:00
|
|
|
GraphicsContext::ScreenIdentifier::ScreenIdentifier():
|
|
|
|
displayNum(0),
|
|
|
|
screenNum(0) {}
|
|
|
|
|
|
|
|
GraphicsContext::ScreenIdentifier::ScreenIdentifier(int in_screenNum):
|
|
|
|
displayNum(0),
|
|
|
|
screenNum(in_screenNum) {}
|
|
|
|
|
|
|
|
GraphicsContext::ScreenIdentifier::ScreenIdentifier(const std::string& in_hostName,int in_displayNum, int in_screenNum):
|
|
|
|
hostName(in_hostName),
|
|
|
|
displayNum(in_displayNum),
|
|
|
|
screenNum(in_screenNum) {}
|
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();
|
|
|
|
}
|
|
|
|
|
2007-08-03 22:50:58 +08:00
|
|
|
void GraphicsContext::ScreenIdentifier::readDISPLAY()
|
|
|
|
{
|
|
|
|
const char* ptr = 0;
|
|
|
|
if ((ptr=getenv("DISPLAY")) != 0)
|
|
|
|
{
|
|
|
|
setScreenIdentifier(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::ScreenIdentifier::setScreenIdentifier(const std::string& displayName)
|
|
|
|
{
|
|
|
|
std::string::size_type colon = displayName.find_last_of(':');
|
|
|
|
std::string::size_type point = displayName.find_last_of('.');
|
|
|
|
|
|
|
|
if (point!=std::string::npos &&
|
|
|
|
colon==std::string::npos &&
|
|
|
|
point < colon) point = std::string::npos;
|
|
|
|
|
|
|
|
if (colon==std::string::npos)
|
|
|
|
{
|
|
|
|
hostName = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hostName = displayName.substr(0,colon);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string::size_type startOfDisplayNum = (colon==std::string::npos) ? 0 : colon+1;
|
|
|
|
std::string::size_type endOfDisplayNum = (point==std::string::npos) ? displayName.size() : point;
|
|
|
|
|
|
|
|
if (startOfDisplayNum<endOfDisplayNum)
|
|
|
|
{
|
|
|
|
displayNum = atoi(displayName.substr(startOfDisplayNum,endOfDisplayNum-startOfDisplayNum).c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
displayNum = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (point!=std::string::npos && point+1<displayName.size())
|
|
|
|
{
|
|
|
|
screenNum = atoi(displayName.substr(point+1,displayName.size()-point-1).c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
screenNum = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
osg::notify(osg::NOTICE)<<" hostName ["<<hostName<<"]"<<std::endl;
|
|
|
|
osg::notify(osg::NOTICE)<<" displayNum "<<displayNum<<std::endl;
|
|
|
|
osg::notify(osg::NOTICE)<<" screenNum "<<screenNum<<std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2006-12-23 05:53:44 +08:00
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
class ContextData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ContextData():
|
|
|
|
_numContexts(0) {}
|
|
|
|
|
|
|
|
unsigned int _numContexts;
|
|
|
|
|
|
|
|
void incrementUsageCount() { ++_numContexts; }
|
|
|
|
|
|
|
|
void decrementUsageCount()
|
|
|
|
{
|
|
|
|
--_numContexts;
|
|
|
|
|
|
|
|
osg::notify(osg::INFO)<<"decrementUsageCount()"<<_numContexts<<std::endl;
|
|
|
|
|
|
|
|
if (_numContexts <= 1 && _compileContext.valid())
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"resetting compileContext "<<_compileContext.get()<<" refCount "<<_compileContext->referenceCount()<<std::endl;
|
|
|
|
|
|
|
|
GraphicsContext* gc = _compileContext.get();
|
|
|
|
_compileContext = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> _compileContext;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::map<unsigned int, ContextData> ContextIDMap;
|
2005-07-25 22:28:22 +08:00
|
|
|
static ContextIDMap s_contextIDMap;
|
2007-07-06 02:33:20 +08:00
|
|
|
static OpenThreads::ReentrantMutex s_contextIDMapMutex;
|
|
|
|
static GraphicsContext::GraphicsContexts s_registeredContexts;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-07-06 02:33:20 +08:00
|
|
|
if (itr->second._numContexts == 0)
|
2005-07-25 22:28:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// reuse contextID;
|
2007-07-06 02:33:20 +08:00
|
|
|
itr->second._numContexts = 1;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
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();
|
2007-07-06 02:33:20 +08:00
|
|
|
s_contextIDMap[contextID]._numContexts = 1;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
unsigned int GraphicsContext::getMaxContextID()
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
2007-07-17 18:54:17 +08:00
|
|
|
unsigned int maxContextID = 0;
|
|
|
|
for(ContextIDMap::iterator itr = s_contextIDMap.begin();
|
|
|
|
itr != s_contextIDMap.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (itr->first > maxContextID) maxContextID = itr->first;
|
|
|
|
}
|
|
|
|
return maxContextID;
|
2007-07-06 02:33:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
void GraphicsContext::incrementContextIDUsageCount(unsigned int contextID)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::incrementContextIDUsageCount("<<contextID<<") to "<<s_contextIDMap[contextID]._numContexts<<std::endl;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
s_contextIDMap[contextID].incrementUsageCount();
|
2005-07-25 22:28:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID)
|
|
|
|
{
|
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
if (s_contextIDMap[contextID]._numContexts!=0)
|
2005-07-25 22:28:22 +08:00
|
|
|
{
|
2007-07-06 02:33:20 +08:00
|
|
|
s_contextIDMap[contextID].decrementUsageCount();
|
2005-07-25 22:28:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Warning: decrementContextIDUsageCount("<<contextID<<") called on expired contextID."<<std::endl;
|
|
|
|
}
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::decrementContextIDUsageCount("<<contextID<<") to "<<s_contextIDMap[contextID]._numContexts<<std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GraphicsContext::registerGraphicsContext(GraphicsContext* gc)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::registerGraphicsContext "<<gc<<std::endl;
|
|
|
|
|
|
|
|
if (!gc) return;
|
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
|
|
|
GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc);
|
|
|
|
if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr);
|
|
|
|
|
|
|
|
s_registeredContexts.push_back(gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::unregisterGraphicsContext(GraphicsContext* gc)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::unregisterGraphicsContext "<<gc<<std::endl;
|
|
|
|
|
|
|
|
if (!gc) return;
|
2005-07-25 22:28:22 +08:00
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
|
|
|
|
GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc);
|
|
|
|
if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext::GraphicsContexts GraphicsContext::getAllRegisteredGraphicsContexts()
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::getAllRegisteredGraphicsContexts s_registeredContexts.size()="<<s_registeredContexts.size()<<std::endl;
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
return s_registeredContexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext::GraphicsContexts GraphicsContext::getRegisteredGraphicsContexts(unsigned int contextID)
|
|
|
|
{
|
|
|
|
GraphicsContexts contexts;
|
|
|
|
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
for(GraphicsContexts::iterator itr = s_registeredContexts.begin();
|
|
|
|
itr != s_registeredContexts.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
GraphicsContext* gc = *itr;
|
|
|
|
if (gc->getState() && gc->getState()->getContextID()==contextID) contexts.push_back(gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::getRegisteredGraphicsContexts "<<contextID<<" contexts.size()="<<contexts.size()<<std::endl;
|
|
|
|
|
|
|
|
return contexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext* GraphicsContext::getOrCreateCompileContext(unsigned int contextID)
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"GraphicsContext::createCompileContext."<<std::endl;
|
|
|
|
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
if (s_contextIDMap[contextID]._compileContext.valid()) return s_contextIDMap[contextID]._compileContext.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext::GraphicsContexts contexts = GraphicsContext::getRegisteredGraphicsContexts(contextID);
|
|
|
|
if (contexts.empty()) return 0;
|
|
|
|
|
|
|
|
GraphicsContext* src_gc = contexts.front();
|
|
|
|
const osg::GraphicsContext::Traits* src_traits = src_gc->getTraits();
|
|
|
|
|
|
|
|
osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
|
|
|
|
traits->screenNum = src_traits->screenNum;
|
|
|
|
traits->displayNum = src_traits->displayNum;
|
|
|
|
traits->hostName = src_traits->hostName;
|
|
|
|
traits->width = 100;
|
|
|
|
traits->height = 100;
|
|
|
|
traits->red = src_traits->red;
|
|
|
|
traits->green = src_traits->green;
|
|
|
|
traits->blue = src_traits->blue;
|
|
|
|
traits->alpha = src_traits->alpha;
|
|
|
|
traits->depth = src_traits->depth;
|
|
|
|
traits->sharedContext = src_gc;
|
|
|
|
traits->pbuffer = true;
|
|
|
|
|
2007-07-17 18:54:17 +08:00
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits);
|
|
|
|
if (gc.valid() && gc->realize())
|
2007-07-06 02:33:20 +08:00
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
s_contextIDMap[contextID]._compileContext = gc;
|
2007-07-17 18:54:17 +08:00
|
|
|
osg::notify(osg::INFO)<<" succeded GraphicsContext::createCompileContext."<<std::endl;
|
|
|
|
return gc.release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
2007-07-06 02:33:20 +08:00
|
|
|
}
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
}
|
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
void GraphicsContext::setCompileContext(unsigned int contextID, GraphicsContext* gc)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
|
|
|
s_contextIDMap[contextID]._compileContext = gc;
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsContext* GraphicsContext::getCompileContext(unsigned int contextID)
|
|
|
|
{
|
2007-07-17 18:54:17 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"GraphicsContext::getCompileContext "<<contextID<<std::endl;
|
2007-07-06 02:33:20 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
2007-07-17 18:54:17 +08:00
|
|
|
ContextIDMap::iterator itr = s_contextIDMap.find(contextID);
|
|
|
|
if (itr != s_contextIDMap.end()) return itr->second._compileContext.get();
|
|
|
|
else return 0;
|
2007-07-06 02:33:20 +08:00
|
|
|
}
|
2005-07-25 22:28:22 +08:00
|
|
|
|
2007-07-06 02:33:20 +08:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// GraphicsContext standard method implementations
|
|
|
|
//
|
2005-07-21 16:43:24 +08:00
|
|
|
GraphicsContext::GraphicsContext():
|
2007-01-05 23:24:06 +08:00
|
|
|
_clearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)),
|
|
|
|
_clearMask(0),
|
2005-07-21 16:43:24 +08:00
|
|
|
_threadOfLastMakeCurrent(0)
|
|
|
|
{
|
2007-01-05 00:49:58 +08:00
|
|
|
setThreadSafeRefUnref(true);
|
2007-02-24 00:31:34 +08:00
|
|
|
_operationsBlock = new RefBlock;
|
2007-07-06 02:33:20 +08:00
|
|
|
|
|
|
|
registerGraphicsContext(this);
|
2005-07-21 16:43:24 +08:00
|
|
|
}
|
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
GraphicsContext::GraphicsContext(const GraphicsContext&, const osg::CopyOp&):
|
|
|
|
_clearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)),
|
|
|
|
_clearMask(0),
|
|
|
|
_threadOfLastMakeCurrent(0)
|
|
|
|
{
|
|
|
|
setThreadSafeRefUnref(true);
|
2007-02-24 00:31:34 +08:00
|
|
|
_operationsBlock = new RefBlock;
|
2007-07-06 02:33:20 +08:00
|
|
|
|
|
|
|
registerGraphicsContext(this);
|
2007-02-03 06:30:36 +08:00
|
|
|
}
|
|
|
|
|
2005-07-25 22:28:22 +08:00
|
|
|
GraphicsContext::~GraphicsContext()
|
|
|
|
{
|
2005-08-30 22:41:08 +08:00
|
|
|
close(false);
|
2007-07-06 02:33:20 +08:00
|
|
|
|
|
|
|
unregisterGraphicsContext(this);
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
|
2007-01-05 23:24:06 +08:00
|
|
|
void GraphicsContext::clear()
|
|
|
|
{
|
|
|
|
if (_clearMask==0 || !_traits) return;
|
|
|
|
|
|
|
|
glViewport(0, 0, _traits->width, _traits->height);
|
|
|
|
glScissor(0, 0, _traits->width, _traits->height);
|
|
|
|
|
|
|
|
glClearColor( _clearColor[0], _clearColor[1], _clearColor[2], _clearColor[3]);
|
|
|
|
|
|
|
|
glClear( _clearMask );
|
|
|
|
}
|
|
|
|
|
2005-08-20 16:59:03 +08:00
|
|
|
bool GraphicsContext::realize()
|
|
|
|
{
|
|
|
|
if (realizeImplementation())
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"close("<<callCloseImplementation<<")"<<this<<std::endl;
|
2007-01-09 00:20:10 +08:00
|
|
|
|
2005-08-20 16:59:03 +08:00
|
|
|
// switch off the graphics thread...
|
|
|
|
setGraphicsThread(0);
|
|
|
|
|
2007-01-09 00:20:10 +08:00
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
bool sharedContextExists = false;
|
|
|
|
|
|
|
|
if (_state.valid())
|
2007-01-09 00:20:10 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
|
2007-07-06 02:33:20 +08:00
|
|
|
if (s_contextIDMap[_state->getContextID()]._numContexts>1) sharedContextExists = true;
|
2007-01-09 03:29:59 +08:00
|
|
|
}
|
2007-01-09 00:20:10 +08:00
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
// release all the OpenGL objects in the scene graphs associted with this
|
|
|
|
for(Cameras::iterator itr = _cameras.begin();
|
|
|
|
itr != _cameras.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
Camera* camera = (*itr);
|
|
|
|
if (camera)
|
2007-01-09 00:20:10 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"Releasing GL objects for Camera="<<camera<<" _state="<<_state.get()<<std::endl;
|
|
|
|
camera->releaseGLObjects(_state.get());
|
2007-01-09 00:20:10 +08:00
|
|
|
}
|
2007-01-09 03:29:59 +08:00
|
|
|
}
|
2007-01-09 00:20:10 +08:00
|
|
|
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
if (callCloseImplementation && _state.valid() && isRealized())
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Closing still viable window "<<sharedContextExists<<" _state->getContextID()="<<_state->getContextID()<<std::endl;
|
2007-01-09 00:20:10 +08:00
|
|
|
|
|
|
|
makeCurrent();
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"Doing Flush"<<std::endl;
|
2007-01-09 00:20:10 +08:00
|
|
|
|
|
|
|
// flush all the OpenGL object buffer for this context.
|
|
|
|
double availableTime = 100.0f;
|
|
|
|
double currentTime = _state->getFrameStamp()?_state->getFrameStamp()->getReferenceTime():0.0;
|
|
|
|
|
|
|
|
osg::FrameBufferObject::flushDeletedFrameBufferObjects(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::RenderBuffer::flushDeletedRenderBuffers(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::Texture::flushAllDeletedTextureObjects(_state->getContextID());
|
|
|
|
osg::Drawable::flushAllDeletedDisplayLists(_state->getContextID());
|
|
|
|
osg::Drawable::flushDeletedVertexBufferObjects(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::VertexProgram::flushDeletedVertexProgramObjects(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::FragmentProgram::flushDeletedFragmentProgramObjects(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::Program::flushDeletedGlPrograms(_state->getContextID(),currentTime,availableTime);
|
|
|
|
osg::Shader::flushDeletedGlShaders(_state->getContextID(),currentTime,availableTime);
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
osg::notify(osg::INFO)<<"Done Flush "<<availableTime<<std::endl;
|
|
|
|
|
2007-01-09 00:20:10 +08:00
|
|
|
_state->reset();
|
|
|
|
|
|
|
|
releaseContext();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
bool GraphicsContext::makeCurrent()
|
2005-08-19 04:17:51 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
bool result = makeCurrentImplementation();
|
2005-08-19 04:17:51 +08:00
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
if (result)
|
2005-08-19 04:17:51 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
|
2007-07-18 22:54:27 +08:00
|
|
|
|
|
|
|
// initialize extension proces, not only initializes on first
|
|
|
|
// call, will be a non-op on subsequent calls.
|
|
|
|
getState()->initializeExtensionProcs();
|
2005-08-19 04:17:51 +08:00
|
|
|
}
|
2007-01-09 03:29:59 +08:00
|
|
|
|
|
|
|
return result;
|
2005-08-19 04:17:51 +08:00
|
|
|
}
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
bool GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
|
2005-08-19 04:17:51 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
bool result = makeContextCurrentImplementation(readContext);
|
2005-08-19 04:17:51 +08:00
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
|
2007-07-18 22:54:27 +08:00
|
|
|
|
|
|
|
// initialize extension proces, not only initializes on first
|
|
|
|
// call, will be a non-op on subsequent calls.
|
|
|
|
getState()->initializeExtensionProcs();
|
2007-01-09 03:29:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2005-08-19 04:17:51 +08:00
|
|
|
}
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
bool GraphicsContext::releaseContext()
|
2005-08-19 04:17:51 +08:00
|
|
|
{
|
2007-01-09 03:29:59 +08:00
|
|
|
bool result = releaseContextImplementation();
|
|
|
|
|
|
|
|
_threadOfLastMakeCurrent = (OpenThreads::Thread*)(-1);
|
|
|
|
|
|
|
|
return result;
|
2005-08-19 04:17:51 +08:00
|
|
|
}
|
2005-08-20 16:59:03 +08:00
|
|
|
|
|
|
|
void GraphicsContext::swapBuffers()
|
|
|
|
{
|
|
|
|
if (isCurrent())
|
|
|
|
{
|
|
|
|
swapBuffersImplementation();
|
2007-01-05 23:24:06 +08:00
|
|
|
clear();
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
else if (_graphicsThread.valid() &&
|
|
|
|
_threadOfLastMakeCurrent == _graphicsThread.get())
|
|
|
|
{
|
|
|
|
_graphicsThread->add(new SwapBuffersOperation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
makeCurrent();
|
|
|
|
swapBuffersImplementation();
|
2007-01-05 23:24:06 +08:00
|
|
|
clear();
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GraphicsContext::createGraphicsThread()
|
|
|
|
{
|
|
|
|
if (!_graphicsThread)
|
|
|
|
{
|
2007-07-12 23:54:45 +08:00
|
|
|
setGraphicsThread(new GraphicsThread);
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:54:45 +08:00
|
|
|
void GraphicsContext::setGraphicsThread(GraphicsThread* gt)
|
2005-08-20 16:59:03 +08:00
|
|
|
{
|
|
|
|
if (_graphicsThread==gt) return;
|
|
|
|
|
|
|
|
if (_graphicsThread.valid())
|
|
|
|
{
|
|
|
|
// need to kill the thread in some way...
|
|
|
|
_graphicsThread->cancel();
|
2007-02-03 06:30:36 +08:00
|
|
|
_graphicsThread->setParent(0);
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_graphicsThread = gt;
|
|
|
|
|
|
|
|
if (_graphicsThread.valid())
|
|
|
|
{
|
2007-02-03 06:30:36 +08:00
|
|
|
_graphicsThread->setParent(this);
|
2005-08-20 16:59:03 +08:00
|
|
|
}
|
|
|
|
}
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
void GraphicsContext::add(Operation* operation)
|
2006-12-22 00:56:20 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
void GraphicsContext::remove(Operation* operation)
|
2006-12-22 00:56:20 +08:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2007-08-02 19:02:47 +08:00
|
|
|
for(osg::GraphicsContext::Cameras::iterator itr = _cameras.begin();
|
|
|
|
itr != _cameras.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::Camera* camera = *itr;
|
|
|
|
if (camera->getRenderer()) (*(camera->getRenderer()))(this);
|
|
|
|
}
|
|
|
|
|
2006-12-22 00:56:20 +08:00
|
|
|
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())
|
|
|
|
{
|
2007-04-05 23:04:05 +08:00
|
|
|
// osg::notify(osg::INFO)<<"Doing op "<<_currentOperation->getName()<<" "<<this<<std::endl;
|
2006-12-22 00:56:20 +08:00
|
|
|
|
|
|
|
// call the graphics operation.
|
|
|
|
(*_currentOperation)(this);
|
|
|
|
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
|
|
|
|
_currentOperation = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-02 02:20:10 +08:00
|
|
|
|
|
|
|
void GraphicsContext::addCamera(osg::Camera* camera)
|
|
|
|
{
|
|
|
|
_cameras.push_back(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsContext::removeCamera(osg::Camera* camera)
|
|
|
|
{
|
|
|
|
for(Cameras::iterator itr = _cameras.begin();
|
|
|
|
itr != _cameras.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (*itr == camera)
|
|
|
|
{
|
|
|
|
_cameras.erase(itr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-09 00:20:10 +08:00
|
|
|
void GraphicsContext::resizedImplementation(int x, int y, int width, int height)
|
2007-01-02 02:20:10 +08:00
|
|
|
{
|
|
|
|
if (!_traits) return;
|
|
|
|
|
|
|
|
double widthChangeRatio = double(width) / double(_traits->width);
|
|
|
|
double heigtChangeRatio = double(height) / double(_traits->height);
|
|
|
|
double aspectRatioChange = widthChangeRatio / heigtChangeRatio;
|
|
|
|
|
|
|
|
for(Cameras::iterator itr = _cameras.begin();
|
|
|
|
itr != _cameras.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
Camera* camera = (*itr);
|
|
|
|
Viewport* viewport = camera->getViewport();
|
|
|
|
if (viewport)
|
|
|
|
{
|
|
|
|
if (viewport->x()==0 && viewport->y()==0 &&
|
|
|
|
viewport->width()>=_traits->width && viewport->height()>=_traits->height)
|
|
|
|
{
|
|
|
|
viewport->setViewport(0,0,width,height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
viewport->x() = static_cast<osg::Viewport::value_type>(double(viewport->x())*widthChangeRatio);
|
|
|
|
viewport->y() = static_cast<osg::Viewport::value_type>(double(viewport->y())*heigtChangeRatio);
|
|
|
|
viewport->width() = static_cast<osg::Viewport::value_type>(double(viewport->width())*widthChangeRatio);
|
|
|
|
viewport->height() = static_cast<osg::Viewport::value_type>(double(viewport->height())*heigtChangeRatio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if aspect ratio adjusted change the project matrix to suit.
|
|
|
|
if (aspectRatioChange != 1.0)
|
|
|
|
{
|
|
|
|
osg::View* view = camera->getView();
|
|
|
|
osg::View::Slave* slave = view ? view->findSlaveForCamera(camera) : 0;
|
2007-05-22 17:32:38 +08:00
|
|
|
|
2007-01-02 02:20:10 +08:00
|
|
|
if (slave && camera->getReferenceFrame()==osg::Transform::RELATIVE_RF)
|
|
|
|
{
|
2007-05-22 17:32:38 +08:00
|
|
|
switch(view->getCamera()->getProjectionResizePolicy())
|
|
|
|
{
|
|
|
|
case(osg::Camera::HORIZONTAL): slave->_projectionOffset *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0); break;
|
|
|
|
case(osg::Camera::VERTICAL): slave->_projectionOffset *= osg::Matrix::scale(1.0, aspectRatioChange,1.0); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2007-01-02 02:20:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-05-30 17:25:01 +08:00
|
|
|
Camera::ProjectionResizePolicy policy = view ? view->getCamera()->getProjectionResizePolicy() : camera->getProjectionResizePolicy();
|
|
|
|
switch(policy)
|
2007-01-02 02:20:10 +08:00
|
|
|
{
|
2007-05-22 17:32:38 +08:00
|
|
|
case(osg::Camera::HORIZONTAL): camera->getProjectionMatrix() *= osg::Matrix::scale(1.0/aspectRatioChange,1.0,1.0); break;
|
|
|
|
case(osg::Camera::VERTICAL): camera->getProjectionMatrix() *= osg::Matrix::scale(1.0, aspectRatioChange,1.0); break;
|
|
|
|
default: break;
|
2007-01-02 02:20:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_traits->x = x;
|
|
|
|
_traits->y = y;
|
|
|
|
_traits->width = width;
|
|
|
|
_traits->height = height;
|
|
|
|
}
|