openscenegraph
GraphicsContext
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_GRAPHICSCONTEXT
15#define OSG_GRAPHICSCONTEXT 1
16
17#include <osg/State>
18#include <osg/GraphicsThread>
19#include <osg/Timer>
20
21#include <vector>
22
23namespace osg {
24
25// forward declare osg::Camera
26class Camera;
27
30{
31 public:
32
34 {
36
37 ScreenIdentifier(int in_screenNum);
38
39 ScreenIdentifier(const std::string& in_hostName,int in_displayNum, int in_screenNum);
40
42 std::string displayName() const;
43
49
54 void setScreenIdentifier(const std::string& displayName);
55
58 {
59 if (displayNum<0) displayNum = 0;
60 if (screenNum<0) screenNum = 0;
61 }
62
63 std::string hostName;
66 };
67
70 {
72
73 // graphics context original and size
74 int x;
75 int y;
76 int width;
77 int height;
78
79 // provide a hint as to which WindowingSystemInterface implementation to use, i.e. "X11", "Win32", "Cocoa", "Carbon" etc.
80 // if the windowingSystemPreference string is empty (default) then return the first available WindowingSystemInterface that
81 // has been registered with the osg::GraphiccsContext::WindowingSystemInterfaces singleton
82 // if the windowingSystemPreference string is not empty then return the first WindowingSystemInterface that matches
84
85 // window decoration and behaviour
86 std::string windowName;
89
90 // buffer depths, 0 equals off.
91 unsigned int red;
92 unsigned int blue;
93 unsigned int green;
94 unsigned int alpha;
95 unsigned int depth;
96 unsigned int stencil;
97
98 // multi sample parameters
99 unsigned int sampleBuffers;
100 unsigned int samples;
101
102 // buffer configuration
106
107 // render to texture
108 GLenum target;
109 GLenum format;
110 unsigned int level;
111 unsigned int face;
112 unsigned int mipMapGeneration;
113
114 // V-sync
115 bool vsync;
116
117 // Swap Group
119 GLuint swapGroup;
121
122 // use multithreaded OpenGL-engine (OS X only)
124
125 // enable cursor
127
128 // settings used in set up of graphics context, only presently used by GL3 build of OSG.
129 std::string glContextVersion;
130 unsigned int glContextFlags;
132
134 bool getContextVersion(unsigned int& major, unsigned int& minor) const;
135
136 // shared context
138
140
141 // ask the GraphicsWindow implementation to set the pixel format of an inherited window
143
144 // X11 hint whether to override the window managers window size/position redirection
146
148
149 // hint of what affinity to use for any thrads associated with the graphics context created using these Traits
151 };
152
157 width(0),
158 height(0),
159 refreshRate(0),
160 colorDepth(0)
161 {}
162
163 ScreenSettings(int in_width, int in_height, double in_refreshRate=0, unsigned int in_colorDepth=0) :
164 width(in_width),
165 height(in_height),
166 refreshRate(in_refreshRate),
167 colorDepth(in_colorDepth)
168 {}
169
170 int width;
172 double refreshRate;
173 unsigned int colorDepth;
174 };
175
176 typedef std::vector<ScreenSettings> ScreenSettingsList;
177
180 {
181 void setName(const std::string& name) { _name = name; }
182 const std::string& getName() const { return _name; }
183
184 virtual unsigned int getNumScreens(const ScreenIdentifier& screenIdentifier = ScreenIdentifier()) = 0;
185
186 virtual void getScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettings & resolution) = 0;
187
188 virtual bool setScreenSettings(const ScreenIdentifier& /*screenIdentifier*/, const ScreenSettings & /*resolution*/) { return false; }
189
190 virtual void enumerateScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettingsList & resolutionList) = 0;
191
193
194 virtual osg::DisplaySettings* getDisplaySettings() const { return 0; }
195
197
200 inline void getScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int& width, unsigned int& height)
201 {
202 ScreenSettings settings;
203 getScreenSettings(screenIdentifier, settings);
204 width = settings.width;
205 height = settings.height;
206 }
207
210 inline bool setScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height)
211 {
212 return setScreenSettings(screenIdentifier, ScreenSettings(width, height));
213 }
214
216 inline bool setScreenRefreshRate(const ScreenIdentifier& screenIdentifier, double refreshRate)
217 {
218 ScreenSettings settings;
219 getScreenSettings(screenIdentifier, settings);
220 settings.refreshRate = refreshRate;
221 return setScreenSettings(screenIdentifier, settings);
222 }
223 protected:
226
227 std::string _name;
228 };
229
231 {
232 public:
234
235 typedef std::vector< osg::ref_ptr<GraphicsContext::WindowingSystemInterface> > Interfaces;
236
237 Interfaces& getInterfaces() { return _interfaces; }
238
240
242
245
246 private:
248 Interfaces _interfaces;
249 };
250
252
254 static WindowingSystemInterface* getWindowingSystemInterface(const std::string& name = "");
255
258
261 static unsigned int createNewContextID();
262
264 static unsigned int getMaxContextID();
265
267 static void incrementContextIDUsageCount(unsigned int contextID);
268
270 static void decrementContextIDUsageCount(unsigned int contextID);
271
272 typedef std::vector<GraphicsContext*> GraphicsContexts;
273
276
278 static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
279
281 static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
282
284 static GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
285
287 static GraphicsContext* getCompileContext(unsigned int contextID);
288
289 public:
290
292 void add(Operation* operation);
293
295 void remove(Operation* operation);
296
298 void remove(const std::string& name);
299
302
304 virtual void runOperations();
305
306 typedef std::list< ref_ptr<Operation> > GraphicsOperationQueue;
307
310
312 OpenThreads::Mutex* getOperationsMutex() { return &_operationsMutex; }
313
315 osg::RefBlock* getOperationsBlock() { return _operationsBlock.get(); }
316
318 Operation* getCurrentOperation() { return _currentOperation.get(); }
319
320
321 public:
322
324 inline const Traits* getTraits() const { return _traits.get(); }
325
327 virtual bool valid() const = 0;
328
329
331 inline void setState(State* state) { _state = state; }
332
334 inline State* getState() { return _state.get(); }
335
337 inline const State* getState() const { return _state.get(); }
338
339
341 inline void setClearColor(const Vec4& color) { _clearColor = color; }
342
344 inline const Vec4& getClearColor() const { return _clearColor; }
345
349 inline void setClearMask(GLbitfield mask) { _clearMask = mask; }
350
352 inline GLbitfield getClearMask() const { return _clearMask; }
353
356 virtual void clear();
357
358 double getTimeSinceLastClear() const { return osg::Timer::instance()->delta_s(_lastClearTick, osg::Timer::instance()->tick()); }
359
360
362 bool realize();
363
369 void close(bool callCloseImplementation=true);
370
373
375 inline bool isRealized() const { return isRealizedImplementation(); }
376
377
382
387
391
393 inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThreadId(); }
394
396 inline void bindPBufferToTexture(GLenum buffer) { bindPBufferToTextureImplementation(buffer); }
397
398
399
402
405
407 GraphicsThread* getGraphicsThread() { return _graphicsThread.get(); }
408
410 const GraphicsThread* getGraphicsThread() const { return _graphicsThread.get(); }
411
412
415 virtual bool realizeImplementation() = 0;
416
419 virtual bool isRealizedImplementation() const = 0;
420
423 virtual void closeImplementation() = 0;
424
427 virtual bool makeCurrentImplementation() = 0;
428
431 virtual bool makeContextCurrentImplementation(GraphicsContext* readContext) = 0;
432
435
438 virtual void bindPBufferToTextureImplementation(GLenum buffer) = 0;
439
441 {
443 };
449 void setSwapCallback(SwapCallback* rc) { _swapCallback = rc; }
450
452 SwapCallback* getSwapCallback() { return _swapCallback.get(); }
453
455 const SwapCallback* getSwapCallback() const { return _swapCallback.get(); }
456
461 {
462 if (_state.valid()) _state->frameCompleted();
463
464 if (_swapCallback.valid()) _swapCallback->swapBuffersImplementation(this);
465 else swapBuffersImplementation();
466 }
467
470 virtual void swapBuffersImplementation() = 0;
471
472
473
476 void resized(int x, int y, int width, int height)
477 {
478 if (_resizedCallback.valid()) _resizedCallback->resizedImplementation(this, x, y, width, height);
479 else resizedImplementation(x, y, width, height);
480 }
481
483 {
484 virtual void resizedImplementation(GraphicsContext* gc, int x, int y, int width, int height) = 0;
485 };
486
489 void setResizedCallback(ResizedCallback* rc) { _resizedCallback = rc; }
490
492 ResizedCallback* getResizedCallback() { return _resizedCallback.get(); }
493
495 const ResizedCallback* getResizedCallback() const { return _resizedCallback.get(); }
496
498 virtual void resizedImplementation(int x, int y, int width, int height);
499
500
501 typedef std::list< osg::Camera* > Cameras;
502
504 Cameras& getCameras() { return _cameras; }
505
507 const Cameras& getCameras() const { return _cameras; }
508
510 void setDefaultFboId(GLuint i) { _defaultFboId = i; }
511
512 GLuint getDefaultFboId() const { return _defaultFboId; }
513
514 public:
515
516 virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsContext*>(object)!=0; }
517 virtual const char* libraryName() const { return "osg"; }
518 virtual const char* className() const { return "GraphicsContext"; }
519
520 protected:
521
524
526
527 virtual Object* cloneType() const { return 0; }
528 virtual Object* clone(const CopyOp&) const { return 0; }
529
532
535
536
537 void addCamera(osg::Camera* camera);
539
541
542 friend class osg::Camera;
543
546
548 GLbitfield _clearMask;
549
551
556
558
561
563
565};
566
567//#include <osg/GLExtensions>
568
569
579
580
581template<class T>
583{
584 WindowingSystemInterfaceProxy(const std::string& name)
585 {
586 _wsi = new T;
587 _wsi->setName(name);
588
589 osg::GraphicsContext::getWindowingSystemInterfaces()->addWindowingSystemInterface(_wsi.get());
590 }
591
593 {
594 osg::GraphicsContext::getWindowingSystemInterfaces()->removeWindowingSystemInterface(_wsi.get());
595 }
596
598};
599
600#define REGISTER_WINDOWINGSYSTEMINTERFACE(ext, classname) \
601 extern "C" void graphicswindow_##ext(void) {} \
602 static osg::WindowingSystemInterfaceProxy<classname> s_proxy_##classname(#ext);
603
604}
605
606#endif
Simple container for specifying which CPU a thread should have affinity with. An empty Affinity....
Definition Affinity:34
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
static size_t CurrentThreadId()
Definition Camera:45
Definition CopyOp:41
Definition DisplaySettings:34
SwapMethod
Definition DisplaySettings:257
WindowingSystemInterface * getWindowingSystemInterface(const std::string &name="")
Interfaces & getInterfaces()
Definition GraphicsContext:237
std::vector< osg::ref_ptr< GraphicsContext::WindowingSystemInterface > > Interfaces
Definition GraphicsContext:235
void removeWindowingSystemInterface(WindowingSystemInterface *wsInterface)
void addWindowingSystemInterface(WindowingSystemInterface *wsInterface)
Definition GraphicsContext:30
virtual bool isSameKindAs(const Object *object) const
Definition GraphicsContext:516
bool isRealized() const
Definition GraphicsContext:375
double getTimeSinceLastClear() const
Definition GraphicsContext:358
const State * getState() const
Definition GraphicsContext:337
Vec4 _clearColor
Definition GraphicsContext:547
bool isCurrent() const
Definition GraphicsContext:393
void resized(int x, int y, int width, int height)
Definition GraphicsContext:476
ref_ptr< State > _state
Definition GraphicsContext:545
void setSwapCallback(SwapCallback *rc)
Definition GraphicsContext:449
void setDefaultFboId(GLuint i)
Definition GraphicsContext:510
static void decrementContextIDUsageCount(unsigned int contextID)
osg::ref_ptr< osg::RefBlock > _operationsBlock
Definition GraphicsContext:553
Timer_t _lastClearTick
Definition GraphicsContext:562
GLuint _defaultFboId
Definition GraphicsContext:564
virtual void bindPBufferToTextureImplementation(GLenum buffer)=0
static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID)
std::list< ref_ptr< Operation > > GraphicsOperationQueue
Definition GraphicsContext:306
virtual bool makeCurrentImplementation()=0
osg::ref_ptr< Operation > _currentOperation
Definition GraphicsContext:555
std::list< osg::Camera * > Cameras
Definition GraphicsContext:501
static void registerGraphicsContext(GraphicsContext *gc)
virtual void clear()
std::vector< GraphicsContext * > GraphicsContexts
Definition GraphicsContext:272
SwapCallback * getSwapCallback()
Definition GraphicsContext:452
ref_ptr< Traits > _traits
Definition GraphicsContext:544
static unsigned int createNewContextID()
void remove(const std::string &name)
Cameras & getCameras()
Definition GraphicsContext:504
static void setCompileContext(unsigned int contextID, GraphicsContext *gc)
virtual ~GraphicsContext()
GLuint getDefaultFboId() const
Definition GraphicsContext:512
void add(Operation *operation)
std::vector< ScreenSettings > ScreenSettingsList
Definition GraphicsContext:176
ref_ptr< ResizedCallback > _resizedCallback
Definition GraphicsContext:559
virtual void closeImplementation()=0
size_t _threadOfLastMakeCurrent
Definition GraphicsContext:550
GraphicsOperationQueue & getOperationsQueue()
Definition GraphicsContext:309
GraphicsThread * getGraphicsThread()
Definition GraphicsContext:407
void bindPBufferToTexture(GLenum buffer)
Definition GraphicsContext:396
osg::RefBlock * getOperationsBlock()
Definition GraphicsContext:315
const GraphicsThread * getGraphicsThread() const
Definition GraphicsContext:410
virtual bool makeContextCurrentImplementation(GraphicsContext *readContext)=0
virtual bool isRealizedImplementation() const =0
GLbitfield getClearMask() const
Definition GraphicsContext:352
virtual void runOperations()
Cameras _cameras
Definition GraphicsContext:540
virtual const char * libraryName() const
Definition GraphicsContext:517
static unsigned int getMaxContextID()
void setClearMask(GLbitfield mask)
Definition GraphicsContext:349
static void incrementContextIDUsageCount(unsigned int contextID)
virtual bool valid() const =0
virtual bool realizeImplementation()=0
void setGraphicsThread(GraphicsThread *gt)
ResizedCallback * getResizedCallback()
Definition GraphicsContext:492
const Cameras & getCameras() const
Definition GraphicsContext:507
void setClearColor(const Vec4 &color)
Definition GraphicsContext:341
OpenThreads::Mutex _operationsMutex
Definition GraphicsContext:552
static WindowingSystemInterface * getWindowingSystemInterface(const std::string &name="")
ref_ptr< SwapCallback > _swapCallback
Definition GraphicsContext:560
void swapBuffersCallbackOrImplementation()
Definition GraphicsContext:460
virtual bool releaseContextImplementation()=0
GraphicsOperationQueue _operations
Definition GraphicsContext:554
void remove(Operation *operation)
const Traits * getTraits() const
Definition GraphicsContext:324
const ResizedCallback * getResizedCallback() const
Definition GraphicsContext:495
static void unregisterGraphicsContext(GraphicsContext *gc)
void setState(State *state)
Definition GraphicsContext:331
Operation * getCurrentOperation()
Definition GraphicsContext:318
const SwapCallback * getSwapCallback() const
Definition GraphicsContext:455
State * getState()
Definition GraphicsContext:334
void removeCamera(osg::Camera *camera)
virtual void resizedImplementation(int x, int y, int width, int height)
virtual const char * className() const
Definition GraphicsContext:518
GraphicsContext(const GraphicsContext &, const osg::CopyOp &)
static GraphicsContext * createGraphicsContext(Traits *traits)
bool makeContextCurrent(GraphicsContext *readContext)
ref_ptr< GraphicsThread > _graphicsThread
Definition GraphicsContext:557
const Vec4 & getClearColor() const
Definition GraphicsContext:344
OpenThreads::Mutex * getOperationsMutex()
Definition GraphicsContext:312
static GraphicsContext * getOrCreateCompileContext(unsigned int contextID)
virtual Object * cloneType() const
Definition GraphicsContext:527
GLbitfield _clearMask
Definition GraphicsContext:548
void close(bool callCloseImplementation=true)
static GraphicsContext * getCompileContext(unsigned int contextID)
static osg::ref_ptr< WindowingSystemInterfaces > & getWindowingSystemInterfaces()
virtual Object * clone(const CopyOp &) const
Definition GraphicsContext:528
virtual void swapBuffersImplementation()=0
void setResizedCallback(ResizedCallback *rc)
Definition GraphicsContext:489
static GraphicsContexts getAllRegisteredGraphicsContexts()
void addCamera(osg::Camera *camera)
Definition GraphicsThread:26
Definition Object:61
Definition OperationThread:51
Definition OperationThread:31
Definition Referenced:44
Definition State:80
T * get()
Definition State:124
Definition GraphicsContext:571
virtual void swapBuffersImplementation(GraphicsContext *gc)
GLsync _previousSync
Definition GraphicsContext:577
static Timer * instance()
double delta_s(Timer_t t1, Timer_t t2) const
Definition Timer:59
Definition Vec4f:28
Definition observer_ptr:39
Definition ref_ptr:32
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
struct __GLsync * GLsync
Definition GLDefines:680
unsigned long long Timer_t
Definition Timer:24
#define OSG_EXPORT
Definition osg/Export:39
Definition GraphicsContext:483
virtual void resizedImplementation(GraphicsContext *gc, int x, int y, int width, int height)=0
Definition GraphicsContext:34
ScreenIdentifier(const std::string &in_hostName, int in_displayNum, int in_screenNum)
void setUndefinedScreenDetailsToDefaultScreen()
Definition GraphicsContext:57
std::string hostName
Definition GraphicsContext:63
int screenNum
Definition GraphicsContext:65
void setScreenIdentifier(const std::string &displayName)
int displayNum
Definition GraphicsContext:64
Definition GraphicsContext:155
unsigned int colorDepth
RGB(A) color buffer depth.
Definition GraphicsContext:173
int height
Definition GraphicsContext:171
double refreshRate
Screen refresh rate, in Hz.
Definition GraphicsContext:172
ScreenSettings()
Definition GraphicsContext:156
int width
Definition GraphicsContext:170
ScreenSettings(int in_width, int in_height, double in_refreshRate=0, unsigned int in_colorDepth=0)
Definition GraphicsContext:163
Definition GraphicsContext:441
virtual void swapBuffersImplementation(GraphicsContext *gc)=0
Definition GraphicsContext:70
int width
Definition GraphicsContext:76
GLenum target
Definition GraphicsContext:108
unsigned int face
Definition GraphicsContext:111
bool vsync
Definition GraphicsContext:115
int y
Definition GraphicsContext:75
unsigned int glContextProfileMask
Definition GraphicsContext:131
bool doubleBuffer
Definition GraphicsContext:105
unsigned int level
Definition GraphicsContext:110
bool getContextVersion(unsigned int &major, unsigned int &minor) const
unsigned int red
Definition GraphicsContext:91
bool pbuffer
Definition GraphicsContext:103
osg::ref_ptr< osg::Referenced > inheritedWindowData
Definition GraphicsContext:139
unsigned int samples
Definition GraphicsContext:100
bool useCursor
Definition GraphicsContext:126
std::string windowingSystemPreference
Definition GraphicsContext:83
GLuint swapBarrier
Definition GraphicsContext:120
bool setInheritedWindowPixelFormat
Definition GraphicsContext:142
unsigned int sampleBuffers
Definition GraphicsContext:99
bool swapGroupEnabled
Definition GraphicsContext:118
unsigned int green
Definition GraphicsContext:93
bool useMultiThreadedOpenGLEngine
Definition GraphicsContext:123
bool windowDecoration
Definition GraphicsContext:87
bool overrideRedirect
Definition GraphicsContext:145
bool supportsResize
Definition GraphicsContext:88
osg::observer_ptr< GraphicsContext > sharedContext
Definition GraphicsContext:137
unsigned int glContextFlags
Definition GraphicsContext:130
int height
Definition GraphicsContext:77
DisplaySettings::SwapMethod swapMethod
Definition GraphicsContext:147
OpenThreads::Affinity affinity
Definition GraphicsContext:150
GLuint swapGroup
Definition GraphicsContext:119
unsigned int depth
Definition GraphicsContext:95
bool quadBufferStereo
Definition GraphicsContext:104
int x
Definition GraphicsContext:74
unsigned int stencil
Definition GraphicsContext:96
GLenum format
Definition GraphicsContext:109
std::string windowName
Definition GraphicsContext:86
unsigned int mipMapGeneration
Definition GraphicsContext:112
unsigned int alpha
Definition GraphicsContext:94
std::string glContextVersion
Definition GraphicsContext:129
Traits(DisplaySettings *ds=0)
unsigned int blue
Definition GraphicsContext:92
bool setScreenResolution(const ScreenIdentifier &screenIdentifier, unsigned int width, unsigned int height)
Definition GraphicsContext:210
WindowingSystemInterface()
Definition GraphicsContext:224
void setName(const std::string &name)
Definition GraphicsContext:181
std::string _name
Definition GraphicsContext:227
bool setScreenRefreshRate(const ScreenIdentifier &screenIdentifier, double refreshRate)
Definition GraphicsContext:216
virtual unsigned int getNumScreens(const ScreenIdentifier &screenIdentifier=ScreenIdentifier())=0
virtual osg::DisplaySettings * getDisplaySettings() const
Definition GraphicsContext:194
void getScreenResolution(const ScreenIdentifier &screenIdentifier, unsigned int &width, unsigned int &height)
Definition GraphicsContext:200
virtual void setDisplaySettings(DisplaySettings *)
Definition GraphicsContext:192
const std::string & getName() const
Definition GraphicsContext:182
virtual GraphicsContext * createGraphicsContext(Traits *traits)=0
virtual void getScreenSettings(const ScreenIdentifier &screenIdentifier, ScreenSettings &resolution)=0
virtual void enumerateScreenSettings(const ScreenIdentifier &screenIdentifier, ScreenSettingsList &resolutionList)=0
virtual bool setScreenSettings(const ScreenIdentifier &, const ScreenSettings &)
Definition GraphicsContext:188
virtual ~WindowingSystemInterface()
Definition GraphicsContext:225
Definition GraphicsContext:583
osg::ref_ptr< T > _wsi
Definition GraphicsContext:597
WindowingSystemInterfaceProxy(const std::string &name)
Definition GraphicsContext:584
~WindowingSystemInterfaceProxy()
Definition GraphicsContext:592