| 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 OSGVIEWER_RENDERER |
|---|
| 15 | #define OSGVIEWER_RENDERER 1 |
|---|
| 16 | |
|---|
| 17 | #include <OpenThreads/Condition> |
|---|
| 18 | #include <osg/Timer> |
|---|
| 19 | #include <osgDB/DatabasePager> |
|---|
| 20 | #include <osgUtil/SceneView> |
|---|
| 21 | #include <osgViewer/Export> |
|---|
| 22 | |
|---|
| 23 | namespace osgViewer { |
|---|
| 24 | |
|---|
| 25 | class OSGVIEWER_EXPORT OpenGLQuerySupport : public osg::Referenced |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | OpenGLQuerySupport(); |
|---|
| 29 | |
|---|
| 30 | virtual void checkQuery(osg::Stats* stats, osg::State* state, |
|---|
| 31 | osg::Timer_t startTick) = 0; |
|---|
| 32 | |
|---|
| 33 | virtual void beginQuery(unsigned int frameNumber, osg::State* state) = 0; |
|---|
| 34 | virtual void endQuery(osg::State* state) = 0; |
|---|
| 35 | virtual void initialize(osg::State* state, osg::Timer_t startTick); |
|---|
| 36 | protected: |
|---|
| 37 | |
|---|
| 38 | const osg::Drawable::Extensions* _extensions; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | |
|---|
| 45 | Renderer(osg::Camera* camera); |
|---|
| 46 | |
|---|
| 47 | osgUtil::SceneView* getSceneView(unsigned int i) { return _sceneView[i].get(); } |
|---|
| 48 | const osgUtil::SceneView* getSceneView(unsigned int i) const { return _sceneView[i].get(); } |
|---|
| 49 | |
|---|
| 50 | void setDone(bool done) { _done = done; } |
|---|
| 51 | bool getDone() { return _done; } |
|---|
| 52 | |
|---|
| 53 | void setGraphicsThreadDoesCull(bool flag); |
|---|
| 54 | bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | virtual void cull(); |
|---|
| 58 | virtual void draw(); |
|---|
| 59 | virtual void cull_draw(); |
|---|
| 60 | |
|---|
| 61 | virtual void compile(); |
|---|
| 62 | |
|---|
| 63 | void setCompileOnNextDraw(bool flag) { _compileOnNextDraw = flag; } |
|---|
| 64 | bool getCompileOnNextDraw() const { return _compileOnNextDraw; } |
|---|
| 65 | |
|---|
| 66 | virtual void operator () (osg::Object* object); |
|---|
| 67 | |
|---|
| 68 | virtual void operator () (osg::GraphicsContext* context); |
|---|
| 69 | |
|---|
| 70 | virtual void release(); |
|---|
| 71 | |
|---|
| 72 | /** Force update of state associated with cameras. */ |
|---|
| 73 | void setCameraRequiresSetUp(bool flag); |
|---|
| 74 | bool getCameraRequiresSetUp() const; |
|---|
| 75 | |
|---|
| 76 | protected: |
|---|
| 77 | void initialize(osg::State* state); |
|---|
| 78 | virtual ~Renderer(); |
|---|
| 79 | |
|---|
| 80 | virtual void updateSceneView(osgUtil::SceneView* sceneView); |
|---|
| 81 | |
|---|
| 82 | osg::observer_ptr<osg::Camera> _camera; |
|---|
| 83 | |
|---|
| 84 | bool _done; |
|---|
| 85 | bool _graphicsThreadDoesCull; |
|---|
| 86 | bool _compileOnNextDraw; |
|---|
| 87 | |
|---|
| 88 | osg::ref_ptr<osgUtil::SceneView> _sceneView[2]; |
|---|
| 89 | |
|---|
| 90 | struct OSGVIEWER_EXPORT ThreadSafeQueue |
|---|
| 91 | { |
|---|
| 92 | OpenThreads::Mutex _mutex; |
|---|
| 93 | OpenThreads::Condition _cond; |
|---|
| 94 | typedef std::list<osgUtil::SceneView*> SceneViewList; |
|---|
| 95 | SceneViewList _queue; |
|---|
| 96 | bool _isReleased; |
|---|
| 97 | |
|---|
| 98 | ThreadSafeQueue(); |
|---|
| 99 | ~ThreadSafeQueue(); |
|---|
| 100 | |
|---|
| 101 | /** Release any thread waiting on the queue, even if the queue is empty. */ |
|---|
| 102 | void release(); |
|---|
| 103 | |
|---|
| 104 | /** Take a SceneView from the queue. Can return 0 if release() is called when the queue is empty. */ |
|---|
| 105 | osgUtil::SceneView* takeFront(); |
|---|
| 106 | |
|---|
| 107 | /** Add a SceneView object to the back of the queue. */ |
|---|
| 108 | void add(osgUtil::SceneView* sv); |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | ThreadSafeQueue _availableQueue; |
|---|
| 113 | ThreadSafeQueue _drawQueue; |
|---|
| 114 | |
|---|
| 115 | bool _initialized; |
|---|
| 116 | osg::ref_ptr<OpenGLQuerySupport> _querySupport; |
|---|
| 117 | osg::Timer_t _startTick; |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | #endif |
|---|