1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSG_GRAPHICSTHREAD
15#define OSG_GRAPHICSTHREAD 1
17#include <osg/OperationThread>
24/** GraphicsThread is a helper class for running OpenGL GraphicsOperation within a single thread assigned to a specific GraphicsContext.*/
25class OSG_EXPORT GraphicsThread : public osg::OperationThread
31 /** Run does the graphics thread run loop.*/
35struct OSG_EXPORT GraphicsOperation : public Operation
37 GraphicsOperation(const std::string& name, bool keep):
38 Operation(name,keep) {}
40 /** Override the standard Operation operator and dynamic cast object to a GraphicsContext,
41 * on success call operation()(GraphicsContext*).*/
42 virtual void operator () (Object* object);
44 virtual void operator () (GraphicsContext* context) = 0;
46 /** Resize any per context GLObject buffers to specified size. */
47 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
49 /** If State is non-zero, this function releases any associated OpenGL objects for
50 * the specified graphics context. Otherwise, releases OpenGL objects
51 * for all graphics contexts. */
52 virtual void releaseGLObjects(osg::State* = 0) const {}
56/** SwapBufferOperation calls swap buffers on the GraphicsContext.*/
57struct OSG_EXPORT SwapBuffersOperation : public GraphicsOperation
59 SwapBuffersOperation():
60 osg::Referenced(true),
61 GraphicsOperation("SwapBuffers",true) {}
63 virtual void operator () (GraphicsContext* context);
66/** BarrierOperation allows one to synchronize multiple GraphicsThreads with each other.*/
67struct OSG_EXPORT BarrierOperation : public Operation, public OpenThreads::Barrier
76 BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION, bool keep=true):
77 osg::Referenced(true),
78 Operation("Barrier", keep),
79 OpenThreads::Barrier(numThreads),
82 virtual void release();
84 virtual void operator () (Object* object);
86 PreBlockOp _preBlockOp;
89/** ReleaseContext_Block_MakeCurrentOperation releases the context for another thread to acquire,
90 * then blocks waiting for context to be released, once the block is release the context is re-acquired.*/
91struct OSG_EXPORT ReleaseContext_Block_MakeCurrentOperation : public GraphicsOperation, public RefBlock
93 ReleaseContext_Block_MakeCurrentOperation():
94 osg::Referenced(true),
95 GraphicsOperation("ReleaseContext_Block_MakeCurrent", false) {}
97 virtual void release();
99 virtual void operator () (GraphicsContext* context);
102struct OSG_EXPORT BlockAndFlushOperation : public GraphicsOperation, public OpenThreads::Block
104 BlockAndFlushOperation();
106 virtual void release();
108 virtual void operator () (GraphicsContext*);
112struct OSG_EXPORT FlushDeletedGLObjectsOperation : public GraphicsOperation
114 FlushDeletedGLObjectsOperation(double availableTime, bool keep=false);
116 virtual void operator () (GraphicsContext*);
118 double _availableTime;
121class OSG_EXPORT RunOperations : public osg::GraphicsOperation
126 osg::GraphicsOperation("RunOperation",true) {}
128 virtual void operator () (osg::GraphicsContext* context);
132class OSG_EXPORT EndOfDynamicDrawBlock : public OpenThreads::BlockCount, public osg::State::DynamicObjectRenderingCompletedCallback
136 EndOfDynamicDrawBlock(unsigned int);
138 void completed(osg::State* state);
142 ~EndOfDynamicDrawBlock() {}