OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GraphicsContext.h
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 
23 namespace osg {
24 
25 // forward declare osg::Camera
26 class 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 
48  void readDISPLAY();
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;
65  int screenNum;
66  };
67 
70  {
71  Traits(DisplaySettings* ds=0);
72 
73  // graphics context original and size
74  int x;
75  int y;
76  int width;
77  int height;
78 
79  // window decoration and behaviour
80  std::string windowName;
83 
84  // buffer depths, 0 equals off.
85  unsigned int red;
86  unsigned int blue;
87  unsigned int green;
88  unsigned int alpha;
89  unsigned int depth;
90  unsigned int stencil;
91 
92  // multi sample parameters
93  unsigned int sampleBuffers;
94  unsigned int samples;
95 
96  // buffer configuration
97  bool pbuffer;
100 
101  // render to texture
102  GLenum target;
103  GLenum format;
104  unsigned int level;
105  unsigned int face;
106  unsigned int mipMapGeneration;
107 
108  // V-sync
109  bool vsync;
110 
111  // Swap Group
113  GLuint swapGroup;
114  GLuint swapBarrier;
115 
116  // use multithreaded OpenGL-engine (OS X only)
118 
119  // enable cursor
120  bool useCursor;
121 
122  // settings used in set up of graphics context, only presently used by GL3 build of OSG.
123  std::string glContextVersion;
124  unsigned int glContextFlags;
125  unsigned int glContextProfileMask;
126 
128  bool getContextVersion(unsigned int& major, unsigned int& minor) const;
129 
130  // shared context
132 
134 
135  // ask the GraphicsWindow implementation to set the pixel format of an inherited window
137 
138  // X11 hint whether to override the window managers window size/position redirection
140 
142  };
143 
146  struct ScreenSettings {
148  width(0),
149  height(0),
150  refreshRate(0),
151  colorDepth(0)
152  {}
153  ScreenSettings(int width, int height, double refreshRate=0, unsigned int colorDepth=0) :
154  width(width),
155  height(height),
156  refreshRate(refreshRate),
157  colorDepth(colorDepth)
158  {}
159 
160  int width;
161  int height;
162  double refreshRate;
163  unsigned int colorDepth;
164  };
165 
166  typedef std::vector<ScreenSettings> ScreenSettingsList;
167 
170  {
171  virtual unsigned int getNumScreens(const ScreenIdentifier& screenIdentifier = ScreenIdentifier()) = 0;
172 
173  virtual void getScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettings & resolution) = 0;
174 
175  virtual bool setScreenSettings(const ScreenIdentifier& /*screenIdentifier*/, const ScreenSettings & /*resolution*/) { return false; }
176 
177  virtual void enumerateScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettingsList & resolutionList) = 0;
178 
180 
181  virtual osg::DisplaySettings* getDisplaySettings() const { return 0; }
182 
183  virtual GraphicsContext* createGraphicsContext(Traits* traits) = 0;
184 
186 
187 
190  inline void getScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int& width, unsigned int& height)
191  {
192  ScreenSettings settings;
193  getScreenSettings(screenIdentifier, settings);
194  width = settings.width;
195  height = settings.height;
196  }
197 
200  inline bool setScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height)
201  {
202  return setScreenSettings(screenIdentifier, ScreenSettings(width, height));
203  }
204 
206  inline bool setScreenRefreshRate(const ScreenIdentifier& screenIdentifier, double refreshRate)
207  {
208  ScreenSettings settings;
209  getScreenSettings(screenIdentifier, settings);
210  settings.refreshRate = refreshRate;
211  return setScreenSettings(screenIdentifier, settings);
212  }
213 
214  };
215 
216 
218  static void setWindowingSystemInterface(WindowingSystemInterface* wsInterface);
219 
221  static WindowingSystemInterface* getWindowingSystemInterface();
222 
224  static GraphicsContext* createGraphicsContext(Traits* traits);
225 
228  static unsigned int createNewContextID();
229 
231  static unsigned int getMaxContextID();
232 
234  static void incrementContextIDUsageCount(unsigned int contextID);
235 
237  static void decrementContextIDUsageCount(unsigned int contextID);
238 
239  typedef std::vector<GraphicsContext*> GraphicsContexts;
240 
242  static GraphicsContexts getAllRegisteredGraphicsContexts();
243 
245  static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
246 
248  static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
249 
251  static GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
252 
254  static GraphicsContext* getCompileContext(unsigned int contextID);
255 
256  public:
257 
259  void add(Operation* operation);
260 
262  void remove(Operation* operation);
263 
265  void remove(const std::string& name);
266 
268  void removeAllOperations();
269 
271  virtual void runOperations();
272 
273  typedef std::list< ref_ptr<Operation> > GraphicsOperationQueue;
274 
276  GraphicsOperationQueue& getOperationsQueue() { return _operations; }
277 
279  OpenThreads::Mutex* getOperationsMutex() { return &_operationsMutex; }
280 
282  osg::RefBlock* getOperationsBlock() { return _operationsBlock.get(); }
283 
285  Operation* getCurrentOperation() { return _currentOperation.get(); }
286 
287 
288  public:
289 
291  inline const Traits* getTraits() const { return _traits.get(); }
292 
294  virtual bool valid() const = 0;
295 
296 
298  inline void setState(State* state) { _state = state; }
299 
301  inline State* getState() { return _state.get(); }
302 
304  inline const State* getState() const { return _state.get(); }
305 
306 
308  inline void setClearColor(const Vec4& color) { _clearColor = color; }
309 
311  inline const Vec4& getClearColor() const { return _clearColor; }
312 
316  inline void setClearMask(GLbitfield mask) { _clearMask = mask; }
317 
319  inline GLbitfield getClearMask() const { return _clearMask; }
320 
323  virtual void clear();
324 
325  double getTimeSinceLastClear() const { return osg::Timer::instance()->delta_s(_lastClearTick, osg::Timer::instance()->tick()); }
326 
327 
329  bool realize();
330 
336  void close(bool callCloseImplementation=true);
337 
339  void swapBuffers();
340 
342  inline bool isRealized() const { return isRealizedImplementation(); }
343 
344 
348  bool makeCurrent();
349 
353  bool makeContextCurrent(GraphicsContext* readContext);
354 
357  bool releaseContext();
358 
360  inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThread(); }
361 
363  inline void bindPBufferToTexture(GLenum buffer) { bindPBufferToTextureImplementation(buffer); }
364 
365 
366 
368  void createGraphicsThread();
369 
371  void setGraphicsThread(GraphicsThread* gt);
372 
374  GraphicsThread* getGraphicsThread() { return _graphicsThread.get(); }
375 
377  const GraphicsThread* getGraphicsThread() const { return _graphicsThread.get(); }
378 
379 
382  virtual bool realizeImplementation() = 0;
383 
386  virtual bool isRealizedImplementation() const = 0;
387 
390  virtual void closeImplementation() = 0;
391 
394  virtual bool makeCurrentImplementation() = 0;
395 
398  virtual bool makeContextCurrentImplementation(GraphicsContext* readContext) = 0;
399 
401  virtual bool releaseContextImplementation() = 0;
402 
405  virtual void bindPBufferToTextureImplementation(GLenum buffer) = 0;
406 
408  {
409  virtual void swapBuffersImplementation(GraphicsContext* gc) = 0;
410  };
416  void setSwapCallback(SwapCallback* rc) { _swapCallback = rc; }
417 
419  SwapCallback* getSwapCallback() { return _swapCallback.get(); }
420 
422  const SwapCallback* getSwapCallback() const { return _swapCallback.get(); }
423 
428  {
429  if (_state.valid()) _state->frameCompleted();
430 
431  if (_swapCallback.valid()) _swapCallback->swapBuffersImplementation(this);
432  else swapBuffersImplementation();
433  }
434 
437  virtual void swapBuffersImplementation() = 0;
438 
439 
440 
443  void resized(int x, int y, int width, int height)
444  {
445  if (_resizedCallback.valid()) _resizedCallback->resizedImplementation(this, x, y, width, height);
446  else resizedImplementation(x, y, width, height);
447  }
448 
450  {
451  virtual void resizedImplementation(GraphicsContext* gc, int x, int y, int width, int height) = 0;
452  };
453 
456  void setResizedCallback(ResizedCallback* rc) { _resizedCallback = rc; }
457 
459  ResizedCallback* getResizedCallback() { return _resizedCallback.get(); }
460 
462  const ResizedCallback* getResizedCallback() const { return _resizedCallback.get(); }
463 
465  virtual void resizedImplementation(int x, int y, int width, int height);
466 
467 
468  typedef std::list< osg::Camera* > Cameras;
469 
471  Cameras& getCameras() { return _cameras; }
472 
474  const Cameras& getCameras() const { return _cameras; }
475 
477  void setDefaultFboId(GLuint i) { _defaultFboId = i; }
478 
479  GLuint getDefaultFboId() const { return _defaultFboId; }
480 
481  public:
482 
483  virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsContext*>(object)!=0; }
484  virtual const char* libraryName() const { return "osg"; }
485  virtual const char* className() const { return "GraphicsContext"; }
486 
487  protected:
488 
489  GraphicsContext();
491 
492  virtual ~GraphicsContext();
493 
494  virtual Object* cloneType() const { return 0; }
495  virtual Object* clone(const CopyOp&) const { return 0; }
496 
498  static void registerGraphicsContext(GraphicsContext* gc);
499 
501  static void unregisterGraphicsContext(GraphicsContext* gc);
502 
503 
504  void addCamera(osg::Camera* camera);
505  void removeCamera(osg::Camera* camera);
506 
507  Cameras _cameras;
508 
509  friend class osg::Camera;
510 
513 
515  GLbitfield _clearMask;
516 
518 
521  GraphicsOperationQueue _operations;
523 
525 
528 
530 
532 };
533 
534 //#include <osg/GLExtensions>
535 
536 
538 {
539 public:
541 
542  virtual void swapBuffersImplementation(GraphicsContext* gc);
543 
545 };
546 
547 }
548 
549 #endif
const SwapCallback * getSwapCallback() const
bool setScreenResolution(const ScreenIdentifier &screenIdentifier, unsigned int width, unsigned int height)
osg::RefBlock * getOperationsBlock()
GraphicsOperationQueue & getOperationsQueue()
virtual const char * libraryName() const
void setState(State *state)
#define OSG_EXPORT
Definition: Export.h:43
GraphicsThread * getGraphicsThread()
const State * getState() const
const Traits * getTraits() const
DisplaySettings::SwapMethod swapMethod
void setResizedCallback(ResizedCallback *rc)
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
const Vec4 & getClearColor() const
GLbitfield getClearMask() const
OpenThreads::Thread * _threadOfLastMakeCurrent
std::vector< GraphicsContext * > GraphicsContexts
ref_ptr< SwapCallback > _swapCallback
osg::ref_ptr< osg::Referenced > inheritedWindowData
void setClearMask(GLbitfield mask)
static Thread * CurrentThread()
double delta_s(Timer_t t1, Timer_t t2) const
Definition: Timer.h:59
osg::ref_ptr< Operation > _currentOperation
ref_ptr< Traits > _traits
std::list< ref_ptr< Operation > > GraphicsOperationQueue
osg::observer_ptr< GraphicsContext > sharedContext
GraphicsOperationQueue _operations
const ResizedCallback * getResizedCallback() const
void swapBuffersCallbackOrImplemenation()
unsigned long long Timer_t
Definition: Timer.h:24
virtual bool isSameKindAs(const Object *object) const
static Timer * instance()
virtual const char * className() const
ref_ptr< ResizedCallback > _resizedCallback
ref_ptr< GraphicsThread > _graphicsThread
GLint GLenum GLsizei width
Definition: GLU.h:71
This class provides an object-oriented thread interface.
Definition: Thread.h:49
ref_ptr< State > _state
void setDefaultFboId(GLuint i)
Operation * getCurrentOperation()
virtual Object * clone(const CopyOp &) const
void resized(int x, int y, int width, int height)
GLuint getDefaultFboId() const
ScreenSettings(int width, int height, double refreshRate=0, unsigned int colorDepth=0)
void getScreenResolution(const ScreenIdentifier &screenIdentifier, unsigned int &width, unsigned int &height)
GLint GLenum GLsizei GLsizei height
Definition: GLU.h:71
osg::ref_ptr< osg::RefBlock > _operationsBlock
bool setScreenRefreshRate(const ScreenIdentifier &screenIdentifier, double refreshRate)
double getTimeSinceLastClear() const
virtual osg::DisplaySettings * getDisplaySettings() const
T * get()
Definition: State.h:161
Definition: AlphaFunc.h:19
const GraphicsThread * getGraphicsThread() const
ResizedCallback * getResizedCallback()
std::vector< ScreenSettings > ScreenSettingsList
const Cameras & getCameras() const
void setClearColor(const Vec4 &color)
SwapCallback * getSwapCallback()
unsigned int colorDepth
RGB(A) color buffer depth.
void bindPBufferToTexture(GLenum buffer)
OpenThreads::Mutex _operationsMutex
std::list< osg::Camera * > Cameras
void setSwapCallback(SwapCallback *rc)
bool isCurrent() const
virtual Object * cloneType() const
struct __GLsync * GLsync
Definition: GLDefines.h:610
virtual void setDisplaySettings(DisplaySettings *)
double refreshRate
Screen refresh rate, in Hz.
bool isRealized() const
OpenThreads::Mutex * getOperationsMutex()
virtual bool setScreenSettings(const ScreenIdentifier &, const ScreenSettings &)