From 39c0c2df760a2bcd2f35f2d04ddd921909341673 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 24 Dec 2006 16:40:19 +0000 Subject: [PATCH] Unified the osg::GraphicsThread::Operation and osg::GraphicsContext::Operation classes as osg::GraphicsOperation. Unpdated parts of OSG depending upon these. Added a virtaul bool valid() method to osg::GraphicsContext to allow apps to test whether a valid graphis context has been created or not. --- examples/osgcamera/osgcamera.cpp | 3 +- include/osg/GraphicsContext | 49 +++++------------ include/osg/GraphicsThread | 82 +++++++++++++++-------------- include/osgViewer/GraphicsWindowX11 | 13 ++++- src/osg/GraphicsContext.cpp | 4 +- src/osg/GraphicsThread.cpp | 8 +-- src/osgUtil/RenderStage.cpp | 4 +- src/osgViewer/GraphicsWindowX11.cpp | 76 +++++++++++++++++++------- src/osgViewer/View.cpp | 4 +- src/osgViewer/Viewer.cpp | 8 +-- 10 files changed, 139 insertions(+), 112 deletions(-) diff --git a/examples/osgcamera/osgcamera.cpp b/examples/osgcamera/osgcamera.cpp index 24388bd45..ec3a54b6b 100644 --- a/examples/osgcamera/osgcamera.cpp +++ b/examples/osgcamera/osgcamera.cpp @@ -39,10 +39,11 @@ int main( int argc, char **argv ) viewer.setUpViewAcrossAllScreens(); viewer.realize(); + bool limitNumberOfFrames = false; unsigned int numFrames = 0; unsigned int maxFrames = 100; - while(!viewer.done() && numFrames=maxFrames)) { viewer.frame(); ++numFrames; diff --git a/include/osg/GraphicsContext b/include/osg/GraphicsContext index 911b36a2e..b494c60a4 100644 --- a/include/osg/GraphicsContext +++ b/include/osg/GraphicsContext @@ -146,40 +146,11 @@ class OSG_EXPORT GraphicsContext : public Referenced public: - /** Base class for implementing graphics operations.*/ - struct OSG_EXPORT Operation : virtual public Referenced - { - Operation(const std::string& name, bool keep): - _name(name), - _keep(keep) {} - - /** Set the human readable name of the operation.*/ - void setName(const std::string& name) { _name = name; } - - /** Get the human readable name of the operation.*/ - const std::string& getName() const { return _name; } - - /** Set whether the operation should be kept once its been applied.*/ - void setKeep(bool keep) { _keep = keep; } - - /** Get whether the operation should be kept once its been applied.*/ - bool getKeep() const { return _keep; } - - /** if this operation is a barrier then release it.*/ - virtual void release() {} - - /** Do the actual task of this operation.*/ - virtual void operator () (GraphicsContext*) {} - - std::string _name; - bool _keep; - }; - /** Add operation to end of OperationQueue.*/ - void add(Operation* operation); + void add(GraphicsOperation* operation); /** Remove operation from OperationQueue.*/ - void remove(Operation* operation); + void remove(GraphicsOperation* operation); /** Remove named operation from OperationQueue.*/ void remove(const std::string& name); @@ -206,6 +177,9 @@ class OSG_EXPORT GraphicsContext : public Referenced inline const State* getState() const { return _state.get(); } + /** Return whether a valid and usable GraphicsContext has been created.*/ + virtual bool valid() const { return false; } + /** Realise the GraphicsContext.*/ bool realize(); @@ -296,13 +270,14 @@ class OSG_EXPORT GraphicsContext : public Referenced OpenThreads::Mutex _mutex; OpenThreads::Thread* _threadOfLastMakeCurrent; - typedef std::list< ref_ptr > OperationQueue; - OpenThreads::Mutex _operationsMutex; - osg::ref_ptr _operationsBlock; - OperationQueue _operations; - osg::ref_ptr _currentOperation; + typedef std::list< ref_ptr > OperationQueue; + + OpenThreads::Mutex _operationsMutex; + osg::ref_ptr _operationsBlock; + OperationQueue _operations; + osg::ref_ptr _currentOperation; - ref_ptr _graphicsThread; + ref_ptr _graphicsThread; }; diff --git a/include/osg/GraphicsThread b/include/osg/GraphicsThread index 7555ee766..0d7d37987 100644 --- a/include/osg/GraphicsThread +++ b/include/osg/GraphicsThread @@ -75,47 +75,50 @@ class Block: virtual public osg::Referenced { bool _released; }; +/** Base class for implementing graphics operations.*/ +struct GraphicsOperation : virtual public Referenced +{ + GraphicsOperation(const std::string& name, bool keep): + _name(name), + _keep(keep) {} + + virtual ~GraphicsOperation() {} + + /** Set the human readable name of the operation.*/ + void setName(const std::string& name) { _name = name; } + + /** Get the human readable name of the operation.*/ + const std::string& getName() const { return _name; } + + /** Set whether the operation should be kept once its been applied.*/ + void setKeep(bool keep) { _keep = keep; } + + /** Get whether the operation should be kept once its been applied.*/ + bool getKeep() const { return _keep; } + + /** if this operation is a barrier then release it.*/ + virtual void release() {} + + /** Do the actual task of this operation.*/ + virtual void operator () (GraphicsContext*) {} + + std::string _name; + bool _keep; +}; + /** GraphicsThread is a helper class for running OpenGL GraphicsOperation within a single thread assigned to a specific GraphicsContext.*/ class OSG_EXPORT GraphicsThread : public Referenced, public OpenThreads::Thread { public: GraphicsThread(); - /** Base class for implementing graphics operations.*/ - struct OSG_EXPORT Operation : virtual public Referenced - { - Operation(const std::string& name, bool keep): - _name(name), - _keep(keep) {} - - /** Set the human readable name of the operation.*/ - void setName(const std::string& name) { _name = name; } - - /** Get the human readable name of the operation.*/ - const std::string& getName() const { return _name; } - - /** Set whether the operation should be kept once its been applied.*/ - void setKeep(bool keep) { _keep = keep; } - - /** Get whether the operation should be kept once its been applied.*/ - bool getKeep() const { return _keep; } - - /** if this operation is a barrier then release it.*/ - virtual void release() {} - - /** Do the actual task of this operation.*/ - virtual void operator () (GraphicsContext*) {} - - std::string _name; - bool _keep; - }; /** Add operation to end of OperationQueue, this will be * executed by the graphics thread once this operation gets to the head of the queue.*/ - void add(Operation* operation, bool waitForCompletion=false); + void add(GraphicsOperation* operation, bool waitForCompletion=false); /** Remove operation from OperationQueue.*/ - void remove(Operation* operation); + void remove(GraphicsOperation* operation); /** Remove named operation from OperationQueue.*/ void remove(const std::string& name); @@ -124,12 +127,13 @@ class OSG_EXPORT GraphicsThread : public Referenced, public OpenThreads::Thread void removeAllOperations(); /** Get the operation currently being run.*/ - osg::ref_ptr getCurrentOperation() { return _currentOperation; } + osg::ref_ptr getCurrentOperation() { return _currentOperation; } /** Run does the graphics thread run loop.*/ virtual void run(); void setDone(bool done); + bool getDone() const { return _done; } /** Cancel this graphics thread.*/ @@ -142,29 +146,29 @@ class OSG_EXPORT GraphicsThread : public Referenced, public OpenThreads::Thread friend class GraphicsContext; GraphicsContext* _graphicsContext; - typedef std::list< ref_ptr > OperationQueue; + typedef std::list< ref_ptr > OperationQueue; bool _done; OpenThreads::Mutex _operationsMutex; osg::ref_ptr _operationsBlock; OperationQueue _operations; - osg::ref_ptr _currentOperation; + osg::ref_ptr _currentOperation; }; /** SwapBufferOperation calls swap buffers on the GraphicsContext.*/ -struct OSG_EXPORT SwapBuffersOperation : public GraphicsThread::Operation +struct OSG_EXPORT SwapBuffersOperation : public GraphicsOperation { SwapBuffersOperation(): - GraphicsThread::Operation("SwapBuffers",true) {} + GraphicsOperation("SwapBuffers",true) {} virtual void operator () (GraphicsContext* context); }; /** BarrierOperation allows one to syncronize multiple GraphicsThreads with each other.*/ -struct OSG_EXPORT BarrierOperation : public GraphicsThread::Operation, public OpenThreads::Barrier +struct OSG_EXPORT BarrierOperation : public GraphicsOperation, public OpenThreads::Barrier { enum PreBlockOp { @@ -174,7 +178,7 @@ struct OSG_EXPORT BarrierOperation : public GraphicsThread::Operation, public Op }; BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION): - GraphicsThread::Operation("Barrier", true), + GraphicsOperation("Barrier", true), OpenThreads::Barrier(numThreads), _preBlockOp(op) {} @@ -187,10 +191,10 @@ struct OSG_EXPORT BarrierOperation : public GraphicsThread::Operation, public Op /** ReleaseContext_Block_MakeCurrentOperation releases the context for another thread to aquire, * then blocks waiting for context to be released, once the block is release the context is re-aqquired.*/ -struct OSG_EXPORT ReleaseContext_Block_MakeCurrentOperation : public GraphicsThread::Operation, public Block +struct OSG_EXPORT ReleaseContext_Block_MakeCurrentOperation : public GraphicsOperation, public Block { ReleaseContext_Block_MakeCurrentOperation(): - GraphicsThread::Operation("ReleaseContext_Block_MakeCurrent", false) {} + GraphicsOperation("ReleaseContext_Block_MakeCurrent", false) {} virtual void release(); diff --git a/include/osgViewer/GraphicsWindowX11 b/include/osgViewer/GraphicsWindowX11 index d2602ef74..78fd92af9 100644 --- a/include/osgViewer/GraphicsWindowX11 +++ b/include/osgViewer/GraphicsWindowX11 @@ -34,6 +34,7 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow public: GraphicsWindowX11(osg::GraphicsContext::Traits* traits): + _valid(false), _display(0), _parent(0), _window(0), @@ -47,10 +48,17 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow { _traits = traits; - setState( new osg::State ); - getState()->setContextID( osg::GraphicsContext::createNewContextID() ); + init(); + + if (valid()) + { + setState( new osg::State ); + getState()->setContextID( osg::GraphicsContext::createNewContextID() ); + } } + virtual bool valid() const { return _valid; } + /** Realise the GraphicsContext.*/ virtual bool realizeImplementation(); @@ -86,6 +94,7 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow void transformMouseXY(float& x, float& y); void adaptKey(XKeyEvent& keyevent, int& keySymbol, unsigned int& modifierMask); + bool _valid; Display* _display; Window _parent; Window _window; diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index 5363be5b5..2eb3433c9 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -260,7 +260,7 @@ void GraphicsContext::setGraphicsThread(GraphicsThread* gt) } } -void GraphicsContext::add(Operation* operation) +void GraphicsContext::add(GraphicsOperation* operation) { osg::notify(osg::INFO)<<"Doing add"<set(true); } -void GraphicsContext::remove(Operation* operation) +void GraphicsContext::remove(GraphicsOperation* operation) { osg::notify(osg::INFO)<<"Doing remove operation"<displayName().c_str()); @@ -198,7 +212,9 @@ void GraphicsWindowX11::init() if (!_display) { - osg::notify(osg::NOTICE)<<"Error: Unable to open display \"" << XDisplayName(_traits->displayName().c_str()) << "\". Is the DISPLAY environmental variable set?"<displayName().c_str()) << "\"."<pbuffer) { - return new GraphicsContextX11(traits); + osg::ref_ptr pbuffer = new GraphicsContextX11(traits); + if (pbuffer->valid()) return pbuffer.release(); + else return 0; } else { - return new GraphicsWindowX11(traits); + osg::ref_ptr window = new GraphicsWindowX11(traits); + if (window->valid()) return window.release(); + else return 0; } } diff --git a/src/osgViewer/View.cpp b/src/osgViewer/View.cpp index f82995980..19a9ee30e 100644 --- a/src/osgViewer/View.cpp +++ b/src/osgViewer/View.cpp @@ -168,10 +168,10 @@ void View::setUpViewAcrossAllScreens() } // Draw operation, that does a draw on the scene graph. -struct RenderingOperation : public osg::GraphicsContext::Operation +struct RenderingOperation : public osg::GraphicsOperation { RenderingOperation(osgUtil::SceneView* sceneView, osgDB::DatabasePager* databasePager): - osg::GraphicsContext::Operation("Render",true), + osg::GraphicsOperation("Render",true), _sceneView(sceneView), _databasePager(databasePager) { diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index d65946331..ac3688dfc 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -107,10 +107,10 @@ void Viewer::getContexts(Contexts& contexts) OpenThreads::Mutex mutex; // Compile operation, that compile OpenGL objects. -struct CompileOperation : public osg::GraphicsThread::Operation +struct CompileOperation : public osg::GraphicsOperation { CompileOperation(osg::Node* scene): - osg::GraphicsThread::Operation("Compile",false), + osg::GraphicsOperation("Compile",false), _scene(scene) { } @@ -136,10 +136,10 @@ struct CompileOperation : public osg::GraphicsThread::Operation // Draw operation, that does a draw on the scene graph. -struct RunOperations : public osg::GraphicsThread::Operation +struct RunOperations : public osg::GraphicsOperation { RunOperations(osg::GraphicsContext* gc): - osg::GraphicsThread::Operation("RunOperation",true), + osg::GraphicsOperation("RunOperation",true), _originalContext(gc) { }