root/OpenSceneGraph/trunk/include/osg/GraphicsContext @ 10890

Revision 10890, 20.9 kB (checked in by robert, 5 years ago)

Renamed osg::GraphicsContext::OperationQueue? typedef to GraphicsOperationQueue? to avoid naming conflict with osg::OperationQueue?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
28/** Base class for providing Windowing API agnostic access to creating and managing graphics context.*/
29class OSG_EXPORT GraphicsContext : public Object
30{
31    public:
32   
33        struct OSG_EXPORT ScreenIdentifier
34        {
35            ScreenIdentifier();
36
37            ScreenIdentifier(int in_screenNum);
38
39            ScreenIdentifier(const std::string& in_hostName,int in_displayNum, int in_screenNum);
40           
41            /** Return the display name in the form hostName::displayNum:screenNum. */   
42            std::string displayName() const;
43           
44            /** Read the DISPLAY environmental variable, and set the ScreenIdentifier accordingly.
45              * Note, if either of displayNum or screenNum are not defined then -1 is set respectively to
46              * signify the this parameter has not been set. When parameters are undefined one can call
47              * call setUndefinedScreenDetalstoDefaultScreen() method after readDISPLAY() to ensure valid values. */
48            void readDISPLAY();
49           
50            /** Set the screenIndentifier from the displayName string.
51              * Note, if either of displayNum or screenNum are not defined then -1 is set respectively to
52              * signify the this parameter has not been set. When parameters are undefined one can call
53              * call setUndefinedScreenDetalstoDefaultScreen() method after readDISPLAY() to ensure valid values. */
54            void setScreenIdentifier(const std::string& displayName);
55
56            /** Set any undefined displayNum or screenNum values (i.e. -1) to the default display & screen of 0 respectively.*/
57            void setUndefinedScreenDetailsToDefaultScreen()
58            {
59                if (displayNum<0) displayNum = 0;
60                if (screenNum<0) screenNum = 0;
61            }
62
63            std::string  hostName;
64            int displayNum;
65            int screenNum;
66        };
67   
68        /** GraphicsContext Traits object provides the specification of what type of graphics context is required.*/
69        struct OSG_EXPORT Traits : public osg::Referenced, public ScreenIdentifier
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;
81            bool        windowDecoration;
82            bool        supportsResize;
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;
98            bool quadBufferStereo;
99            bool doubleBuffer;
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            // use multithreaded OpenGL-engine (OS X only)
112            bool            useMultiThreadedOpenGLEngine;
113           
114            // enable cursor
115            bool            useCursor;
116
117            // settings used in set up of graphics context, only presently used by GL3 build of OSG.
118            std::string     glContextVersion;
119            unsigned int    glContextFlags;
120            unsigned int    glContextProfileMask;
121           
122           
123            // shared context
124            GraphicsContext* sharedContext;
125           
126            osg::ref_ptr<osg::Referenced> inheritedWindowData;
127           
128            // ask the GraphicsWindow implementation to set the pixel format of an inherited window
129            bool setInheritedWindowPixelFormat;
130           
131            // X11 hint whether to override the window managers window size/position redirection
132            bool overrideRedirect;
133        };
134
135        /** Simple resolution structure used by WindowingSystemInterface to get and set screen resolution.
136          * Note the '0' value stands for 'unset'. */
137        struct ScreenSettings {
138            ScreenSettings() :
139                width(0),
140                height(0),
141                refreshRate(0),
142                colorDepth(0)
143            {}
144            ScreenSettings(int width, int height, double refreshRate=0, unsigned int colorDepth=0) :
145                width(width),
146                height(height),
147                refreshRate(refreshRate),
148                colorDepth(colorDepth)
149            {}
150
151            int width;
152            int height;
153            double refreshRate;         ///< Screen refresh rate, in Hz.
154            unsigned int colorDepth;    ///< RGB(A) color buffer depth.
155        };
156
157        typedef std::vector<ScreenSettings> ScreenSettingsList;
158
159        /** Callback to be implemented to provide access to Windowing API's ability to create Windows/pbuffers.*/
160        struct WindowingSystemInterface : public osg::Referenced
161        {
162            virtual unsigned int getNumScreens(const ScreenIdentifier& screenIdentifier = ScreenIdentifier()) = 0;
163
164            virtual void getScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettings & resolution) = 0;
165
166            virtual bool setScreenSettings(const ScreenIdentifier& /*screenIdentifier*/, const ScreenSettings & /*resolution*/) { return false; }
167
168            virtual void enumerateScreenSettings(const ScreenIdentifier& screenIdentifier, ScreenSettingsList & resolutionList) = 0;
169
170            virtual GraphicsContext* createGraphicsContext(Traits* traits) = 0;
171           
172            virtual ~WindowingSystemInterface() {}
173
174
175            /** Gets screen resolution without using the ScreenResolution structure.
176              * \deprecated Provided only for backward compatibility. */
177            inline void getScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int& width, unsigned int& height)
178            {
179                ScreenSettings settings;
180                getScreenSettings(screenIdentifier, settings);
181                width = settings.width;
182                height = settings.height;
183            }
184
185            /** Sets screen resolution without using the ScreenSettings structure.
186              * \deprecated Provided only for backward compatibility. */
187            inline bool setScreenResolution(const ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height)
188            {
189                return setScreenSettings(screenIdentifier, ScreenSettings(width, height));
190            }
191
192            /** \deprecated Provided only for backward compatibility. */
193            inline bool setScreenRefreshRate(const ScreenIdentifier& screenIdentifier, double refreshRate)
194            {
195                ScreenSettings settings;
196                getScreenSettings(screenIdentifier, settings);
197                settings.refreshRate = refreshRate;
198                return setScreenSettings(screenIdentifier, settings);
199            }
200        };
201   
202   
203        /** Set the query the windowing system for screens and create graphics context - this functor should be supplied by the windows toolkit. */
204        static void setWindowingSystemInterface(WindowingSystemInterface* wsInterface);
205       
206        /** Get the WindowingSystemInterface*/
207        static WindowingSystemInterface* getWindowingSystemInterface();
208   
209        /** Create a graphics context for a specified set of traits.*/
210        static GraphicsContext* createGraphicsContext(Traits* traits);
211       
212        /** Create a contextID for a new graphics context, this contextID is used to set up the osg::State associate with context.
213          * Automatically increments the usage count of the contextID to 1.*/
214        static unsigned int createNewContextID();
215
216        /** Get the current max ContextID.*/
217        static unsigned int getMaxContextID();
218
219        /** Increment the usage count associate with a contextID. The usage count specifies how many graphics contexts a specific contextID is shared between.*/
220        static void incrementContextIDUsageCount(unsigned int contextID);
221
222        /** Decrement the usage count associate with a contextID. Once the contextID goes to 0 the contextID is then free to be reused.*/
223        static void decrementContextIDUsageCount(unsigned int contextID);
224   
225        typedef std::vector<GraphicsContext*> GraphicsContexts;
226       
227        /** Get all the registered graphics contexts.*/
228        static GraphicsContexts getAllRegisteredGraphicsContexts();
229       
230        /** Get all the registered graphics contexts associated with a specific contextID.*/
231        static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID);
232       
233        /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
234        static void setCompileContext(unsigned int contextID, GraphicsContext* gc);
235
236        /** Get existing or create a new GraphicsContext to do background compilation for GraphicsContexts associated with specified contextID.*/
237        static  GraphicsContext* getOrCreateCompileContext(unsigned int contextID);
238       
239        /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/
240        static GraphicsContext* getCompileContext(unsigned int contextID);
241
242    public:
243   
244        /** Add operation to end of OperationQueue.*/
245        void add(Operation* operation);
246       
247        /** Remove operation from OperationQueue.*/
248        void remove(Operation* operation);
249
250        /** Remove named operation from OperationQueue.*/
251        void remove(const std::string& name);
252
253        /** Remove all operations from OperationQueue.*/
254        void removeAllOperations();
255
256        /** Run the operations. */
257        void runOperations();
258
259        typedef std::list< ref_ptr<Operation> > GraphicsOperationQueue;
260       
261        /** Get the operations queue, not you must use the OperationsMutex when accessing the queue.*/
262        GraphicsOperationQueue& getOperationsQueue() { return _operations; }
263
264        /** Get the operations queue mutex.*/
265        OpenThreads::Mutex* getOperationsMutex() { return &_operationsMutex; }
266
267        /** Get the operations queue block used to mark an empty queue, if you end items into the empty queue you must release this block.*/
268        osg::RefBlock* getOperationsBlock() { return _operationsBlock.get(); }
269
270        /** Get the current operations that is being run.*/
271        Operation* getCurrentOperation() { return _currentOperation.get(); }
272
273
274    public:
275   
276        /** Get the traits of the GraphicsContext.*/
277        inline const Traits* getTraits() const { return _traits.get(); }
278
279        /** Return whether a valid and usable GraphicsContext has been created.*/
280        virtual bool valid() const = 0;
281
282
283        /** Set the State object which tracks the current OpenGL state for this graphics context.*/
284        inline void setState(State* state) { _state = state; }
285       
286        /** Get the State object which tracks the current OpenGL state for this graphics context.*/
287        inline State* getState() { return _state.get(); }
288       
289        /** Get the const State object which tracks the current OpenGL state for this graphics context.*/
290        inline const State* getState() const { return _state.get(); }
291
292
293        /** Sets the clear color. */
294        inline void setClearColor(const Vec4& color) { _clearColor = color; }
295
296        /** Returns the clear color. */
297        inline const Vec4& getClearColor() const { return _clearColor; }
298       
299        /** Set the clear mask used in glClear(..).
300          * Defaults to 0 - so no clear is done by default by the GraphicsContext, instead the Camera's attached the GraphicsContext will do the clear.
301          * GraphicsContext::setClearMask() is useful for when the Camera's Viewports don't conver the whole context, so the context will fill in the gaps. */
302        inline void setClearMask(GLbitfield mask) { _clearMask = mask; }
303
304        /** Get the clear mask.*/
305        inline GLbitfield getClearMask() const { return _clearMask; }
306       
307        /** Do an OpenGL clear of the full graphics context/window.
308          * Note, must only be called from a thread with this context current.*/
309        virtual void clear();
310       
311        double getTimeSinceLastClear() const { return osg::Timer::instance()->delta_s(_lastClearTick, osg::Timer::instance()->tick()); }
312       
313
314        /** Realize the GraphicsContext.*/
315        bool realize();
316
317        /** close the graphics context.
318          * close(bool) stops any associated graphics threads, releases the contextID for the GraphicsContext then
319          * optional calls closeImplementation() to do the actual deletion of the graphics.  This call is made optional
320          * as there are times when the graphics context has already been deleted externally and only the OSG side
321          * of the its data need to be closed down. */
322        void close(bool callCloseImplementation=true);
323
324        /** swap the front and back buffers.*/
325        void swapBuffers();
326
327        /** Return true if the graphics context has been realized and is ready to use.*/
328        inline bool isRealized() const { return isRealizedImplementation(); }
329
330
331        /** Make this graphics context current.
332          * Implemented by calling makeCurrentImplementation().
333          * Returns true on success. */
334        bool makeCurrent();
335       
336        /** Make this graphics context current with specified read context.
337          * Implemented by calling makeContextCurrentImplementation().
338          * Returns true on success. */
339        bool makeContextCurrent(GraphicsContext* readContext);
340       
341        /** Release the graphics context.
342          * Returns true on success. */
343        bool releaseContext();
344       
345        /** Return true if the current thread has this OpenGL graphics context.*/
346        inline bool isCurrent() const { return _threadOfLastMakeCurrent == OpenThreads::Thread::CurrentThread(); }
347
348        /** Bind the graphics context to associated texture.*/
349        inline void bindPBufferToTexture(GLenum buffer) { bindPBufferToTextureImplementation(buffer); }
350
351
352
353        /** Create a graphics thread to the graphics context, so that the thread handles all OpenGL operations.*/
354        void createGraphicsThread();
355
356        /** Assign a graphics thread to the graphics context, so that the thread handles all OpenGL operations.*/
357        void setGraphicsThread(GraphicsThread* gt);
358
359        /** Get the graphics thread assigned the graphics context.*/
360        GraphicsThread* getGraphicsThread() { return _graphicsThread.get(); }
361
362        /** Get the const graphics thread assigned the graphics context.*/
363        const GraphicsThread* getGraphicsThread() const { return _graphicsThread.get(); }
364
365
366        /** Realize the GraphicsContext implementation,
367          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
368        virtual bool realizeImplementation() = 0;
369
370        /** Return true if the graphics context has been realized, and is ready to use, implementation.
371          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
372        virtual bool isRealizedImplementation() const = 0;
373
374        /** Close the graphics context implementation.
375          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
376        virtual void closeImplementation() = 0;
377
378        /** Make this graphics context current implementation.
379          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
380        virtual bool makeCurrentImplementation() = 0;
381       
382        /** Make this graphics context current with specified read context implementation.
383          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
384        virtual bool makeContextCurrentImplementation(GraphicsContext* readContext) = 0;
385
386        /** Release the graphics context implementation.*/
387        virtual bool releaseContextImplementation() = 0;
388
389        /** Pure virtual, Bind the graphics context to associated texture implementation.
390          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
391        virtual void bindPBufferToTextureImplementation(GLenum buffer) = 0;
392
393        /** Swap the front and back buffers implementation.
394          * Pure virtual - must be implemented by concrete implementations of GraphicsContext. */
395        virtual void swapBuffersImplementation() = 0;
396
397
398
399        /** resized method should be called when the underlying window has been resized and the GraphicsWindow and associated Cameras must
400            be updated to keep in sync with the new size. */
401        void resized(int x, int y, int width, int height)
402        {
403            if (_resizedCallback.valid()) _resizedCallback->resizedImplementation(this, x, y, width, height);
404            else resizedImplementation(x, y, width, height);
405        }
406
407        struct ResizedCallback : public osg::Referenced
408        {
409            virtual void resizedImplementation(GraphicsContext* gc, int x, int y, int width, int height) = 0;
410        };
411
412        /** Set the resized callback which overrides the GraphicsConext::realizedImplementation(), allow developers to provide custom behavior
413          * in response to a window being resized.*/
414        void setResizedCallback(ResizedCallback* rc) { _resizedCallback = rc; }
415
416        /** Get the resized callback which overrides the GraphicsConext::realizedImplementation().*/
417        ResizedCallback* getResizedCallback() { return _resizedCallback.get(); }
418
419        /** Get the const resized callback which overrides the GraphicsConext::realizedImplementation().*/
420        const ResizedCallback* getResizedCallback() const { return _resizedCallback.get(); }
421
422        /** resized implementation, by default resizes the viewports and aspect ratios the cameras associated with the graphics Window. */
423        virtual void resizedImplementation(int x, int y, int width, int height);
424
425
426        typedef std::list< osg::Camera* > Cameras;
427
428        /** Get the the list of cameras associated with this graphics context.*/
429        Cameras& getCameras() { return _cameras; }
430
431        /** Get the the const list of cameras associated with this graphics context.*/
432        const Cameras& getCameras() const { return _cameras; }
433
434    public:
435
436        virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsContext*>(object)!=0; }
437        virtual const char* libraryName() const { return "osg"; }
438        virtual const char* className() const { return "GraphicsContext"; }
439
440    protected:
441       
442        GraphicsContext();
443        GraphicsContext(const GraphicsContext&, const osg::CopyOp&);
444
445        virtual ~GraphicsContext();
446
447        virtual Object* cloneType() const { return 0; }
448        virtual Object* clone(const CopyOp&) const { return 0; }
449
450        /** Register a GraphicsContext.*/
451        static void registerGraphicsContext(GraphicsContext* gc);
452
453        /** Unregister a GraphicsContext.*/
454        static void unregisterGraphicsContext(GraphicsContext* gc);
455
456
457        void addCamera(osg::Camera* camera);
458        void removeCamera(osg::Camera* camera);
459       
460        Cameras _cameras;
461
462        friend class osg::Camera;
463
464        ref_ptr<Traits>         _traits;       
465        ref_ptr<State>          _state;
466
467        Vec4                    _clearColor;
468        GLbitfield              _clearMask;
469
470        OpenThreads::Thread*    _threadOfLastMakeCurrent;
471       
472        OpenThreads::Mutex                  _operationsMutex;
473        osg::ref_ptr<osg::RefBlock>         _operationsBlock;
474        GraphicsOperationQueue              _operations;
475        osg::ref_ptr<Operation>             _currentOperation;
476
477        ref_ptr<GraphicsThread>             _graphicsThread;
478       
479        ref_ptr<ResizedCallback>            _resizedCallback;
480       
481        Timer_t                             _lastClearTick;
482};
483
484
485}
486
487#endif
Note: See TracBrowser for help on using the browser.