Convert GraphicsThread/GraphicsOperation to more generic OperationsThread/Operation.

This paves the way to running cull traversals in seperate threads.

Updated wrappers
This commit is contained in:
Robert Osfield
2007-02-02 22:30:36 +00:00
parent bee545b82d
commit 69da91620f
28 changed files with 439 additions and 300 deletions

View File

@@ -23,7 +23,7 @@ namespace osg {
class Camera;
/** Base class for providing Windowing API agnostic access to creating and managing graphics context.*/
class OSG_EXPORT GraphicsContext : public Referenced
class OSG_EXPORT GraphicsContext : public Object
{
public:
@@ -162,10 +162,10 @@ class OSG_EXPORT GraphicsContext : public Referenced
public:
/** Add operation to end of OperationQueue.*/
void add(GraphicsOperation* operation);
void add(Operation* operation);
/** Remove operation from OperationQueue.*/
void remove(GraphicsOperation* operation);
void remove(Operation* operation);
/** Remove named operation from OperationQueue.*/
void remove(const std::string& name);
@@ -176,7 +176,7 @@ class OSG_EXPORT GraphicsContext : public Referenced
/** Run the operations. */
void runOperations();
typedef std::list< ref_ptr<GraphicsOperation> > OperationQueue;
typedef std::list< ref_ptr<Operation> > OperationQueue;
/** Get the operations queue, not you must use the OperationsMutex when accessing the queue.*/
OperationQueue& getOperationsQueue() { return _operations; }
@@ -188,7 +188,7 @@ class OSG_EXPORT GraphicsContext : public Referenced
osg::Block* getOperationsBlock() { return _operationsBlock.get(); }
/** Get the current operations that is being run.*/
GraphicsOperation* getCurrentOperation() { return _currentOperation.get(); }
Operation* getCurrentOperation() { return _currentOperation.get(); }
public:
@@ -271,13 +271,13 @@ class OSG_EXPORT GraphicsContext : public Referenced
void createGraphicsThread();
/** Assign a graphics thread to the graphics context, so that the thread handles all OpenGL operations.*/
void setGraphicsThread(GraphicsThread* gt);
void setGraphicsThread(OperationsThread* gt);
/** Get the graphics thread assigned the graphics context.*/
GraphicsThread* getGraphicsThread() { return _graphicsThread.get(); }
OperationsThread* getGraphicsThread() { return _graphicsThread.get(); }
/** Get the const graphics thread assigned the graphics context.*/
const GraphicsThread* getGraphicsThread() const { return _graphicsThread.get(); }
const OperationsThread* getGraphicsThread() const { return _graphicsThread.get(); }
/** Realise the GraphicsContext implementation,
@@ -351,9 +351,16 @@ class OSG_EXPORT GraphicsContext : public Referenced
protected:
GraphicsContext();
GraphicsContext(const GraphicsContext&, const osg::CopyOp&);
virtual ~GraphicsContext();
virtual Object* cloneType() const { return 0; }
virtual Object* clone(const CopyOp&) const { return 0; }
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsContext*>(object)!=0; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "GraphicsContext"; }
void addCamera(osg::Camera* camera);
void removeCamera(osg::Camera* camera);
@@ -372,9 +379,9 @@ class OSG_EXPORT GraphicsContext : public Referenced
OpenThreads::Mutex _operationsMutex;
osg::ref_ptr<osg::Block> _operationsBlock;
OperationQueue _operations;
osg::ref_ptr<GraphicsOperation> _currentOperation;
osg::ref_ptr<Operation> _currentOperation;
ref_ptr<GraphicsThread> _graphicsThread;
ref_ptr<OperationsThread> _graphicsThread;
ref_ptr<ResizedCallback> _resizedCallback;

View File

@@ -14,7 +14,9 @@
#ifndef OSG_GRAPHICSTHREAD
#define OSG_GRAPHICSTHREAD 1
#include <osg/State>
#include <osg/observer_ptr>
#include <osg/Object>
#include <OpenThreads/Thread>
#include <OpenThreads/Barrier>
#include <OpenThreads/Condition>
@@ -23,9 +25,6 @@
namespace osg {
// forward declare GraphicsContext
class GraphicsContext;
class Block: virtual public osg::Referenced {
public:
Block():_released(false) {}
@@ -76,13 +75,13 @@ class Block: virtual public osg::Referenced {
};
/** Base class for implementing graphics operations.*/
struct GraphicsOperation : virtual public Referenced
struct Operation : virtual public Referenced
{
GraphicsOperation(const std::string& name, bool keep):
Operation(const std::string& name, bool keep):
_name(name),
_keep(keep) {}
virtual ~GraphicsOperation() {}
virtual ~Operation() {}
/** Set the human readable name of the operation.*/
void setName(const std::string& name) { _name = name; }
@@ -100,25 +99,31 @@ struct GraphicsOperation : virtual public Referenced
virtual void release() {}
/** Do the actual task of this operation.*/
virtual void operator () (GraphicsContext*) {}
virtual void operator () (Object*) = 0;
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
class OSG_EXPORT OperationsThread : public Referenced, public OpenThreads::Thread
{
public:
GraphicsThread();
OperationsThread();
void setParent(Object* parent) { _parent = parent; }
Object* getParent() { return _parent.get(); }
const Object* getParent() const { return _parent.get(); }
/** 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(GraphicsOperation* operation, bool waitForCompletion=false);
void add(Operation* operation, bool waitForCompletion=false);
/** Remove operation from OperationQueue.*/
void remove(GraphicsOperation* operation);
void remove(Operation* operation);
/** Remove named operation from OperationQueue.*/
void remove(const std::string& name);
@@ -127,7 +132,7 @@ class OSG_EXPORT GraphicsThread : public Referenced, public OpenThreads::Thread
void removeAllOperations();
/** Get the operation currently being run.*/
osg::ref_ptr<GraphicsOperation> getCurrentOperation() { return _currentOperation; }
osg::ref_ptr<Operation> getCurrentOperation() { return _currentOperation; }
/** Run does the graphics thread run loop.*/
virtual void run();
@@ -141,34 +146,33 @@ class OSG_EXPORT GraphicsThread : public Referenced, public OpenThreads::Thread
protected:
virtual ~GraphicsThread();
virtual ~OperationsThread();
friend class GraphicsContext;
GraphicsContext* _graphicsContext;
observer_ptr<Object> _parent;
typedef std::list< ref_ptr<GraphicsOperation> > OperationQueue;
typedef std::list< ref_ptr<Operation> > OperationQueue;
bool _done;
OpenThreads::Mutex _operationsMutex;
osg::ref_ptr<osg::Block> _operationsBlock;
OperationQueue _operations;
osg::ref_ptr<GraphicsOperation> _currentOperation;
osg::ref_ptr<Operation> _currentOperation;
};
/** SwapBufferOperation calls swap buffers on the GraphicsContext.*/
struct OSG_EXPORT SwapBuffersOperation : public GraphicsOperation
struct OSG_EXPORT SwapBuffersOperation : public Operation
{
SwapBuffersOperation():
GraphicsOperation("SwapBuffers",true) {}
Operation("SwapBuffers",true) {}
virtual void operator () (GraphicsContext* context);
virtual void operator () (Object* context);
};
/** BarrierOperation allows one to syncronize multiple GraphicsThreads with each other.*/
struct OSG_EXPORT BarrierOperation : public GraphicsOperation, public OpenThreads::Barrier
struct OSG_EXPORT BarrierOperation : public Operation, public OpenThreads::Barrier
{
enum PreBlockOp
{
@@ -178,27 +182,27 @@ struct OSG_EXPORT BarrierOperation : public GraphicsOperation, public OpenThread
};
BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION):
GraphicsOperation("Barrier", true),
Operation("Barrier", true),
OpenThreads::Barrier(numThreads),
_preBlockOp(op) {}
virtual void release();
virtual void operator () (GraphicsContext* context);
virtual void operator () (Object* context);
PreBlockOp _preBlockOp;
};
/** 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 GraphicsOperation, public Block
struct OSG_EXPORT ReleaseContext_Block_MakeCurrentOperation : public Operation, public Block
{
ReleaseContext_Block_MakeCurrentOperation():
GraphicsOperation("ReleaseContext_Block_MakeCurrent", false) {}
Operation("ReleaseContext_Block_MakeCurrent", false) {}
virtual void release();
virtual void operator () (GraphicsContext* context);
virtual void operator () (Object* context);
};
}

View File

@@ -140,10 +140,10 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Referenced
/** Set the graphics operation to call on realization of the viewers graphics windows.*/
void setRealizeOperation(osg::GraphicsOperation* op) { _realizeOperation = op; }
void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
/** Get the graphics operation to call on realization of the viewers graphics windows.*/
osg::GraphicsOperation* getRealizeOperation() { return _realizeOperation.get(); }
osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
/** Stop any threads begin run by viewer.*/
void stopThreading();
@@ -151,7 +151,7 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Referenced
/** Start any threads required by the viewer, as per viewers ThreadingModel.*/
void startThreading();
/** Set up the GraphicsOperations to render the various viewer cameras on the viewers graphics windows.*/
/** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/
void setUpRenderingSupport();
@@ -191,7 +191,7 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Referenced
osg::ref_ptr<osgGA::EventQueue> _eventQueue;
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
osg::ref_ptr<osg::GraphicsOperation> _realizeOperation;
osg::ref_ptr<osg::Operation> _realizeOperation;
};

View File

@@ -134,10 +134,10 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View
void getCameras(Cameras& cameras, bool onlyActive=true);
/** Set the graphics operation to call on realization of the viewers graphics windows.*/
void setRealizeOperation(osg::GraphicsOperation* op) { _realizeOperation = op; }
void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
/** Get the graphics operation to call on realization of the viewers graphics windows.*/
osg::GraphicsOperation* getRealizeOperation() { return _realizeOperation.get(); }
osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
/** Stop any threads begin run by viewer.*/
void stopThreading();
@@ -145,7 +145,7 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View
/** Start any threads required by the viewer, as per viewers ThreadingModel.*/
void startThreading();
/** Set up the GraphicsOperations to render the various viewer cameras on the viewers graphics windows.*/
/** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/
void setUpRenderingSupport();
protected:
@@ -182,7 +182,7 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
osg::ref_ptr<osg::GraphicsOperation> _realizeOperation;
osg::ref_ptr<osg::Operation> _realizeOperation;
};