openscenegraph
GraphicsThread
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSG_GRAPHICSTHREAD
15#define OSG_GRAPHICSTHREAD 1
16
17#include <osg/OperationThread>
18#include <osg/State>
19
20namespace osg {
21
22class GraphicsContext;
23
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
26{
27 public:
28
29 GraphicsThread();
30
31 /** Run does the graphics thread run loop.*/
32 virtual void run();
33};
34
35struct OSG_EXPORT GraphicsOperation : public Operation
36{
37 GraphicsOperation(const std::string& name, bool keep):
38 Operation(name,keep) {}
39
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);
43
44 virtual void operator () (GraphicsContext* context) = 0;
45
46 /** Resize any per context GLObject buffers to specified size. */
47 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
48
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 {}
53};
54
55
56/** SwapBufferOperation calls swap buffers on the GraphicsContext.*/
57struct OSG_EXPORT SwapBuffersOperation : public GraphicsOperation
58{
59 SwapBuffersOperation():
60 osg::Referenced(true),
61 GraphicsOperation("SwapBuffers",true) {}
62
63 virtual void operator () (GraphicsContext* context);
64};
65
66/** BarrierOperation allows one to synchronize multiple GraphicsThreads with each other.*/
67struct OSG_EXPORT BarrierOperation : public Operation, public OpenThreads::Barrier
68{
69 enum PreBlockOp
70 {
71 NO_OPERATION,
72 GL_FLUSH,
73 GL_FINISH
74 };
75
76 BarrierOperation(int numThreads, PreBlockOp op=NO_OPERATION, bool keep=true):
77 osg::Referenced(true),
78 Operation("Barrier", keep),
79 OpenThreads::Barrier(numThreads),
80 _preBlockOp(op) {}
81
82 virtual void release();
83
84 virtual void operator () (Object* object);
85
86 PreBlockOp _preBlockOp;
87};
88
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
92{
93 ReleaseContext_Block_MakeCurrentOperation():
94 osg::Referenced(true),
95 GraphicsOperation("ReleaseContext_Block_MakeCurrent", false) {}
96
97 virtual void release();
98
99 virtual void operator () (GraphicsContext* context);
100};
101
102struct OSG_EXPORT BlockAndFlushOperation : public GraphicsOperation, public OpenThreads::Block
103{
104 BlockAndFlushOperation();
105
106 virtual void release();
107
108 virtual void operator () (GraphicsContext*);
109};
110
111
112struct OSG_EXPORT FlushDeletedGLObjectsOperation : public GraphicsOperation
113{
114 FlushDeletedGLObjectsOperation(double availableTime, bool keep=false);
115
116 virtual void operator () (GraphicsContext*);
117
118 double _availableTime;
119};
120
121class OSG_EXPORT RunOperations : public osg::GraphicsOperation
122{
123public:
124
125 RunOperations():
126 osg::GraphicsOperation("RunOperation",true) {}
127
128 virtual void operator () (osg::GraphicsContext* context);
129
130};
131
132class OSG_EXPORT EndOfDynamicDrawBlock : public OpenThreads::BlockCount, public osg::State::DynamicObjectRenderingCompletedCallback
133{
134 public:
135
136 EndOfDynamicDrawBlock(unsigned int);
137
138 void completed(osg::State* state);
139
140 protected:
141
142 ~EndOfDynamicDrawBlock() {}
143};
144
145}
146
147#endif