| 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 OSGDB_DATABASEPAGER |
|---|
| 15 | #define OSGDB_DATABASEPAGER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/NodeVisitor> |
|---|
| 18 | #include <osg/Group> |
|---|
| 19 | #include <osg/PagedLOD> |
|---|
| 20 | #include <osg/Drawable> |
|---|
| 21 | #include <osg/GraphicsThread> |
|---|
| 22 | #include <osg/FrameStamp> |
|---|
| 23 | |
|---|
| 24 | #include <OpenThreads/Thread> |
|---|
| 25 | #include <OpenThreads/Mutex> |
|---|
| 26 | #include <OpenThreads/ScopedLock> |
|---|
| 27 | #include <OpenThreads/Condition> |
|---|
| 28 | |
|---|
| 29 | #include <osgDB/SharedStateManager> |
|---|
| 30 | #include <osgDB/ReaderWriter> |
|---|
| 31 | #include <osgDB/Export> |
|---|
| 32 | |
|---|
| 33 | #include <map> |
|---|
| 34 | #include <list> |
|---|
| 35 | #include <algorithm> |
|---|
| 36 | #include <functional> |
|---|
| 37 | |
|---|
| 38 | namespace osgDB { |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** Database paging class which manages the loading of files in a background thread, |
|---|
| 43 | * and synchronizing of loaded models with the main scene graph.*/ |
|---|
| 44 | class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandler |
|---|
| 45 | { |
|---|
| 46 | public : |
|---|
| 47 | |
|---|
| 48 | typedef OpenThreads::Thread::ThreadPriority ThreadPriority; |
|---|
| 49 | |
|---|
| 50 | DatabasePager(); |
|---|
| 51 | |
|---|
| 52 | DatabasePager(const DatabasePager& rhs); |
|---|
| 53 | |
|---|
| 54 | /** Create a shallow copy on the DatabasePager.*/ |
|---|
| 55 | virtual DatabasePager* clone() const { return new DatabasePager(*this); } |
|---|
| 56 | |
|---|
| 57 | /** get the prototype singleton used by DatabasePager::create().*/ |
|---|
| 58 | static osg::ref_ptr<DatabasePager>& prototype(); |
|---|
| 59 | |
|---|
| 60 | /** create a DatabasePager by cloning DatabasePager::prototype().*/ |
|---|
| 61 | static DatabasePager* create(); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** Add a request to load a node file to end the the database request list.*/ |
|---|
| 66 | virtual void requestNodeFile(const std::string& fileName,osg::Group* group, |
|---|
| 67 | float priority, const osg::FrameStamp* framestamp, |
|---|
| 68 | osg::ref_ptr<osg::Referenced>& databaseRequest); |
|---|
| 69 | |
|---|
| 70 | virtual void requestNodeFile(const std::string& fileName,osg::Group* group, |
|---|
| 71 | float priority, const osg::FrameStamp* framestamp, |
|---|
| 72 | osg::ref_ptr<osg::Referenced>& databaseRequest, |
|---|
| 73 | ReaderWriter::Options* loadOptions); |
|---|
| 74 | |
|---|
| 75 | /** Set the priority of the database pager thread(s).*/ |
|---|
| 76 | int setSchedulePriority(OpenThreads::Thread::ThreadPriority priority); |
|---|
| 77 | |
|---|
| 78 | /** Cancel the database pager thread(s).*/ |
|---|
| 79 | virtual int cancel(); |
|---|
| 80 | |
|---|
| 81 | virtual bool isRunning() const; |
|---|
| 82 | |
|---|
| 83 | /** Clear all internally cached structures.*/ |
|---|
| 84 | virtual void clear(); |
|---|
| 85 | |
|---|
| 86 | class OSGDB_EXPORT DatabaseThread : public osg::Referenced, public OpenThreads::Thread |
|---|
| 87 | { |
|---|
| 88 | public: |
|---|
| 89 | |
|---|
| 90 | enum Mode |
|---|
| 91 | { |
|---|
| 92 | HANDLE_ALL_REQUESTS, |
|---|
| 93 | HANDLE_NON_HTTP, |
|---|
| 94 | HANDLE_ONLY_HTTP |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | DatabaseThread(DatabasePager* pager, Mode mode, const std::string& name); |
|---|
| 98 | |
|---|
| 99 | DatabaseThread(const DatabaseThread& dt, DatabasePager* pager); |
|---|
| 100 | |
|---|
| 101 | void setDone(bool done) { _done = done; } |
|---|
| 102 | bool getDone() const { return _done; } |
|---|
| 103 | |
|---|
| 104 | void setActive(bool active) { _active = active; } |
|---|
| 105 | bool getActive() const { return _active; } |
|---|
| 106 | |
|---|
| 107 | virtual int cancel(); |
|---|
| 108 | |
|---|
| 109 | virtual void run(); |
|---|
| 110 | |
|---|
| 111 | protected: |
|---|
| 112 | |
|---|
| 113 | virtual ~DatabaseThread(); |
|---|
| 114 | |
|---|
| 115 | bool _done; |
|---|
| 116 | bool _active; |
|---|
| 117 | DatabasePager* _pager; |
|---|
| 118 | Mode _mode; |
|---|
| 119 | std::string _name; |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | void setUpThreads(unsigned int totalNumThreads=2, unsigned int numHttpThreads=1); |
|---|
| 123 | |
|---|
| 124 | unsigned int addDatabaseThread(DatabaseThread::Mode mode, const std::string& name); |
|---|
| 125 | |
|---|
| 126 | DatabaseThread* getDatabaseThread(unsigned int i) { return _databaseThreads[i].get(); } |
|---|
| 127 | |
|---|
| 128 | const DatabaseThread* getDatabaseThread(unsigned int i) const { return _databaseThreads[i].get(); } |
|---|
| 129 | |
|---|
| 130 | unsigned int getNumDatabaseThreads() const { return _databaseThreads.size(); } |
|---|
| 131 | |
|---|
| 132 | /** Set whether the database pager thread should be paused or not.*/ |
|---|
| 133 | void setDatabasePagerThreadPause(bool pause); |
|---|
| 134 | |
|---|
| 135 | /** Get whether the database pager thread should is paused or not.*/ |
|---|
| 136 | bool getDatabasePagerThreadPause() const { return _databasePagerThreadPaused; } |
|---|
| 137 | |
|---|
| 138 | /** Set whether new database request calls are accepted or ignored.*/ |
|---|
| 139 | void setAcceptNewDatabaseRequests(bool acceptNewRequests) { _acceptNewRequests = acceptNewRequests; } |
|---|
| 140 | |
|---|
| 141 | /** Get whether new database request calls are accepted or ignored.*/ |
|---|
| 142 | bool getAcceptNewDatabaseRequests() const { return _acceptNewRequests; } |
|---|
| 143 | |
|---|
| 144 | /** Get the number of frames that are currently active.*/ |
|---|
| 145 | int getNumFramesActive() const { return _numFramesActive; } |
|---|
| 146 | |
|---|
| 147 | /** Signal the database thread that the update, cull and draw has begun for a new frame. |
|---|
| 148 | * Note, this is called by the application so that the database pager can go to sleep while the CPU is busy on the main rendering threads. */ |
|---|
| 149 | virtual void signalBeginFrame(const osg::FrameStamp* framestamp); |
|---|
| 150 | |
|---|
| 151 | /** Signal the database thread that the update, cull and draw dispatch has completed. |
|---|
| 152 | * Note, this is called by the application so that the database pager can go to wake back up now the main rendering threads are iddle waiting for the next frame.*/ |
|---|
| 153 | virtual void signalEndFrame(); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** Find all PagedLOD nodes in a subgraph and register them with |
|---|
| 157 | * the DatabasePager so it can keep track of expired nodes. |
|---|
| 158 | * note, should be only be called from the update thread. */ |
|---|
| 159 | virtual void registerPagedLODs(osg::Node* subgraph, int frameNumber = 0); |
|---|
| 160 | |
|---|
| 161 | /** Set whether the database pager should pre compile OpenGL objects before allowing |
|---|
| 162 | * them to be merged into the scene graph. |
|---|
| 163 | * Pre compilation helps reduce the chances of frame drops, but also slows the |
|---|
| 164 | * speed at which tiles are merged as they have to be compiled first.*/ |
|---|
| 165 | void setDoPreCompile(bool flag) { _doPreCompile = flag; } |
|---|
| 166 | |
|---|
| 167 | /** Get whether the database pager should pre compile OpenGL objects before allowing |
|---|
| 168 | * them to be merged into the scene graph.*/ |
|---|
| 169 | bool getDoPreCompile() const { return _doPreCompile; } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /** Set the target frame rate that the DatabasePager should assume. |
|---|
| 173 | * Typically one would set this to the value refresh rate of your display system i.e. 60Hz. |
|---|
| 174 | * Default value is 100. |
|---|
| 175 | * Usage notes. The TargetFrameRate and the MinimumTimeAvailableForGLCompileAndDeletePerFrame |
|---|
| 176 | * parameters are not directly used by DatabasePager, but are should be used as a guide for how |
|---|
| 177 | * long to set aside per frame for compiling and deleting OpenGL objects - ie. the value to use |
|---|
| 178 | * when calling DatabasePager::compileGLObjectgs(state,availableTime,). The longer amount of |
|---|
| 179 | * time to set aside cthe faster databases will be paged in but with increased chance of frame drops, |
|---|
| 180 | * the lower the amount of time the set aside the slower databases will paged it but with better |
|---|
| 181 | * chance of avoid any frame drops. The default values are chosen to achieve the later when running |
|---|
| 182 | * on a modern mid to high end PC. |
|---|
| 183 | * The way to compute the amount of available time use a scheme such as : |
|---|
| 184 | * availableTime = maximum(1.0/targetFrameRate - timeTakenDuringUpdateCullAndDraw, minimumTimeAvailableForGLCompileAndDeletePerFrame). |
|---|
| 185 | */ |
|---|
| 186 | void setTargetFrameRate(double tfr) { _targetFrameRate = tfr; } |
|---|
| 187 | |
|---|
| 188 | /** Get the target frame rate that the DatabasePager should assume.*/ |
|---|
| 189 | double getTargetFrameRate() const { return _targetFrameRate; } |
|---|
| 190 | |
|---|
| 191 | /** Set the minimum amount of time (in seconds) that should be made available for compiling and delete OpenGL objects per frame. |
|---|
| 192 | * Default value is 0.001 (1 millisecond). |
|---|
| 193 | * For usage see notes in setTargetFrameRate.*/ |
|---|
| 194 | void setMinimumTimeAvailableForGLCompileAndDeletePerFrame(double ta) { _minimumTimeAvailableForGLCompileAndDeletePerFrame = ta; } |
|---|
| 195 | |
|---|
| 196 | /** Get the minimum amount of time that should be made available for compiling and delete OpenGL objects per frame. |
|---|
| 197 | * For usage see notes in setTargetFrameRate.*/ |
|---|
| 198 | double getMinimumTimeAvailableForGLCompileAndDeletePerFrame() const { return _minimumTimeAvailableForGLCompileAndDeletePerFrame; } |
|---|
| 199 | |
|---|
| 200 | /** Set the maximum number of OpenGL objects that the page should attempt to compile per frame. |
|---|
| 201 | * Note, Lower values reduces chances of a frame drop but lower the rate that database will be paged in at. |
|---|
| 202 | * Default value is 8. */ |
|---|
| 203 | void setMaximumNumOfObjectsToCompilePerFrame(unsigned int num) { _maximumNumOfObjectsToCompilePerFrame = num; } |
|---|
| 204 | |
|---|
| 205 | /** Get the maximum number of OpenGL objects that the page should attempt to compile per frame.*/ |
|---|
| 206 | unsigned int getMaximumNumOfObjectsToCompilePerFrame() const { return _maximumNumOfObjectsToCompilePerFrame; } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | /** Set the target maximum number of PagedLOD to maintain in memory. |
|---|
| 210 | * Note, if more than the target number are required for rendering of a frame then these active PagedLOD are excempt from being expiried. |
|---|
| 211 | * But once the number of active drops back below the target the inactive PagedLOD will be trimmed back to the target number.*/ |
|---|
| 212 | void setTargetMaximumNumberOfPageLOD(unsigned int target) { _targetMaximumNumberOfPageLOD = target; } |
|---|
| 213 | |
|---|
| 214 | /** Get the target maximum number of PagedLOD to maintain in memory.*/ |
|---|
| 215 | unsigned int getTargetMaximumNumberOfPageLOD() const { return _targetMaximumNumberOfPageLOD; } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | /** Deprecated.*/ |
|---|
| 219 | void setExpiryDelay(double expiryDelay) { _expiryDelay = expiryDelay; } |
|---|
| 220 | |
|---|
| 221 | /** Deprecated.*/ |
|---|
| 222 | double getExpiryDelay() const { return _expiryDelay; } |
|---|
| 223 | |
|---|
| 224 | /** Deprecated.*/ |
|---|
| 225 | void setExpiryFrames(int expiryFrames) { _expiryFrames = expiryFrames; } |
|---|
| 226 | |
|---|
| 227 | /** Deprecated.*/ |
|---|
| 228 | int getExpiryFrames() const { return _expiryFrames; } |
|---|
| 229 | |
|---|
| 230 | /** Deprecated.*/ |
|---|
| 231 | void setReleaseDelay(double releaseDelay); |
|---|
| 232 | |
|---|
| 233 | /** Deprecated.*/ |
|---|
| 234 | double getReleaseDelay() const { return _releaseDelay; } |
|---|
| 235 | |
|---|
| 236 | /** Deprecated.*/ |
|---|
| 237 | void setReleaseFrames(int releaseFrames) { _releaseFrames = releaseFrames; } |
|---|
| 238 | |
|---|
| 239 | /** Deprecated.*/ |
|---|
| 240 | int getReleaseFrames() const { return _releaseFrames; } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | /** Set whether the removed subgraphs should be deleted in the database thread or not.*/ |
|---|
| 244 | void setDeleteRemovedSubgraphsInDatabaseThread(bool flag) { _deleteRemovedSubgraphsInDatabaseThread = flag; } |
|---|
| 245 | |
|---|
| 246 | /** Get whether the removed subgraphs should be deleted in the database thread or not.*/ |
|---|
| 247 | bool getDeleteRemovedSubgraphsInDatabaseThread() const { return _deleteRemovedSubgraphsInDatabaseThread; } |
|---|
| 248 | |
|---|
| 249 | enum DrawablePolicy |
|---|
| 250 | { |
|---|
| 251 | DO_NOT_MODIFY_DRAWABLE_SETTINGS, |
|---|
| 252 | USE_DISPLAY_LISTS, |
|---|
| 253 | USE_VERTEX_BUFFER_OBJECTS, |
|---|
| 254 | USE_VERTEX_ARRAYS |
|---|
| 255 | }; |
|---|
| 256 | |
|---|
| 257 | /** Set how loaded drawables should be handled w.r.t their display list/vertex buffer object/vertex array settings.*/ |
|---|
| 258 | void setDrawablePolicy(DrawablePolicy policy) { _drawablePolicy = policy; } |
|---|
| 259 | |
|---|
| 260 | /** Get how loaded drawables should be handled w.r.t their display list/vertex buffer object/vertex array settings.*/ |
|---|
| 261 | DrawablePolicy getDrawablePolicy() const { return _drawablePolicy; } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | /** Set whether newly loaded textures should have their UnrefImageDataAfterApply set to a specified value.*/ |
|---|
| 265 | void setUnrefImageDataAfterApplyPolicy(bool changeAutoUnRef, bool valueAutoUnRef) { _changeAutoUnRef = changeAutoUnRef; _valueAutoUnRef = valueAutoUnRef; } |
|---|
| 266 | |
|---|
| 267 | /** Get whether newly loaded textures should have their UnrefImageDataAfterApply set to a specified value.*/ |
|---|
| 268 | void getUnrefImageDataAfterApplyPolicy(bool& changeAutoUnRef, bool& valueAutoUnRef) const { changeAutoUnRef = _changeAutoUnRef; valueAutoUnRef = _valueAutoUnRef; } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | /** Set whether newly loaded textures should have their MaxAnisotopy set to a specified value.*/ |
|---|
| 272 | void setMaxAnisotropyPolicy(bool changeAnisotropy, float valueAnisotropy) { _changeAnisotropy = changeAnisotropy; _valueAnisotropy = valueAnisotropy; } |
|---|
| 273 | |
|---|
| 274 | /** Set whether newly loaded textures should have their MaxAnisotopy set to a specified value.*/ |
|---|
| 275 | void getMaxAnisotropyPolicy(bool& changeAnisotropy, float& valueAnisotropy) const { changeAnisotropy = _changeAnisotropy; valueAnisotropy = _valueAnisotropy; } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | /** Return true if there are pending updates to the scene graph that require a call to updateSceneGraph(double). */ |
|---|
| 279 | bool requiresUpdateSceneGraph() const; |
|---|
| 280 | |
|---|
| 281 | /** Merge the changes to the scene graph by calling calling removeExpiredSubgraphs then addLoadedDataToSceneGraph. |
|---|
| 282 | * Note, must only be called from single thread update phase. */ |
|---|
| 283 | virtual void updateSceneGraph(const osg::FrameStamp& frameStamp) |
|---|
| 284 | { |
|---|
| 285 | removeExpiredSubgraphs(frameStamp); |
|---|
| 286 | addLoadedDataToSceneGraph(frameStamp); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** Turn the compilation of rendering objects for specified graphics context on (true) or off(false). */ |
|---|
| 290 | void setCompileGLObjectsForContextID(unsigned int contextID, bool on); |
|---|
| 291 | |
|---|
| 292 | /** Get whether the compilation of rendering objects for specified graphics context on (true) or off(false). */ |
|---|
| 293 | bool getCompileGLObjectsForContextID(unsigned int contextID); |
|---|
| 294 | |
|---|
| 295 | /** Return true if an external draw thread should call compileGLObjects(..) or not.*/ |
|---|
| 296 | bool requiresExternalCompileGLObjects(unsigned int contextID) const; |
|---|
| 297 | |
|---|
| 298 | /** Return true if there are pending compile operations that are required. |
|---|
| 299 | * If requiresCompileGLObjects() return true the application should call compileGLObjects() .*/ |
|---|
| 300 | bool requiresCompileGLObjects() const; |
|---|
| 301 | |
|---|
| 302 | /** Compile the rendering objects (display lists,texture objects, VBO's) on loaded subgraph. |
|---|
| 303 | * note, should only be called from the draw thread. |
|---|
| 304 | * Note, must only be called from a valid graphics context. */ |
|---|
| 305 | virtual void compileGLObjects(osg::State& state,double& availableTime); |
|---|
| 306 | |
|---|
| 307 | /** Compile the rendering objects (display lists,texture objects, VBO's) on loaded subgraph. |
|---|
| 308 | * note, should only be called from the draw thread. |
|---|
| 309 | * Note, must only be called from a valid graphics context. */ |
|---|
| 310 | virtual void compileAllGLObjects(osg::State& state); |
|---|
| 311 | |
|---|
| 312 | /** Report how many items are in the _fileRequestList queue */ |
|---|
| 313 | unsigned int getFileRequestListSize() const { return _fileRequestQueue->_requestList.size() + _httpRequestQueue->_requestList.size(); } |
|---|
| 314 | |
|---|
| 315 | /** Report how many items are in the _dataToCompileList queue */ |
|---|
| 316 | unsigned int getDataToCompileListSize() const { return _dataToCompileList->_requestList.size(); } |
|---|
| 317 | |
|---|
| 318 | /** Report how many items are in the _dataToCompileList queue */ |
|---|
| 319 | unsigned int getDataToMergeListSize() const { return _dataToMergeList->_requestList.size(); } |
|---|
| 320 | |
|---|
| 321 | /** Report whether any requests are in the pager.*/ |
|---|
| 322 | bool getRequestsInProgress() const; |
|---|
| 323 | |
|---|
| 324 | /** Get the minimum time between the first request for a tile to be loaded and the time of its merge into the main scene graph.*/ |
|---|
| 325 | double getMinimumTimeToMergeTile() const { return _minimumTimeToMergeTile; } |
|---|
| 326 | |
|---|
| 327 | /** Get the maximum time between the first request for a tile to be loaded and the time of its merge into the main scene graph.*/ |
|---|
| 328 | double getMaximumTimeToMergeTile() const { return _maximumTimeToMergeTile; } |
|---|
| 329 | |
|---|
| 330 | /** Get the average time between the first request for a tile to be loaded and the time of its merge into the main scene graph.*/ |
|---|
| 331 | double getAverageTimeToMergeTiles() const { return (_numTilesMerges > 0) ? _totalTimeToMergeTiles/static_cast<double>(_numTilesMerges) : 0; } |
|---|
| 332 | |
|---|
| 333 | /** Reset the Stats variables.*/ |
|---|
| 334 | void resetStats(); |
|---|
| 335 | |
|---|
| 336 | typedef std::list< osg::ref_ptr<osg::PagedLOD> > PagedLODList; |
|---|
| 337 | typedef std::set< osg::ref_ptr<osg::StateSet> > StateSetList; |
|---|
| 338 | typedef std::vector< osg::ref_ptr<osg::Drawable> > DrawableList; |
|---|
| 339 | typedef std::pair<StateSetList,DrawableList> DataToCompile; |
|---|
| 340 | typedef std::map< unsigned int, DataToCompile > DataToCompileMap; |
|---|
| 341 | typedef std::set<unsigned int> ActiveGraphicsContexts; |
|---|
| 342 | typedef std::vector< osg::observer_ptr<osg::GraphicsContext> > CompileGraphicsContexts; |
|---|
| 343 | |
|---|
| 344 | protected: |
|---|
| 345 | |
|---|
| 346 | virtual ~DatabasePager(); |
|---|
| 347 | |
|---|
| 348 | friend class DatabaseThread; |
|---|
| 349 | friend struct DatabaseRequest; |
|---|
| 350 | |
|---|
| 351 | struct RequestQueue; |
|---|
| 352 | |
|---|
| 353 | struct DatabaseRequest : public osg::Referenced |
|---|
| 354 | { |
|---|
| 355 | DatabaseRequest(): |
|---|
| 356 | osg::Referenced(true), |
|---|
| 357 | _frameNumberFirstRequest(0), |
|---|
| 358 | _timestampFirstRequest(0.0), |
|---|
| 359 | _priorityFirstRequest(0.f), |
|---|
| 360 | _frameNumberLastRequest(0), |
|---|
| 361 | _timestampLastRequest(0.0), |
|---|
| 362 | _priorityLastRequest(0.0f), |
|---|
| 363 | _numOfRequests(0), |
|---|
| 364 | _requestQueue(0) |
|---|
| 365 | {} |
|---|
| 366 | |
|---|
| 367 | std::string _fileName; |
|---|
| 368 | int _frameNumberFirstRequest; |
|---|
| 369 | double _timestampFirstRequest; |
|---|
| 370 | float _priorityFirstRequest; |
|---|
| 371 | int _frameNumberLastRequest; |
|---|
| 372 | double _timestampLastRequest; |
|---|
| 373 | float _priorityLastRequest; |
|---|
| 374 | unsigned int _numOfRequests; |
|---|
| 375 | osg::observer_ptr<osg::Group> _groupForAddingLoadedSubgraph; |
|---|
| 376 | osg::ref_ptr<osg::Node> _loadedModel; |
|---|
| 377 | DataToCompileMap _dataToCompileMap; |
|---|
| 378 | osg::ref_ptr<ReaderWriter::Options> _loadOptions; |
|---|
| 379 | RequestQueue* _requestQueue; |
|---|
| 380 | |
|---|
| 381 | bool isRequestCurrent (int frameNumber) const |
|---|
| 382 | { |
|---|
| 383 | return frameNumber - _frameNumberLastRequest <= 1; |
|---|
| 384 | } |
|---|
| 385 | }; |
|---|
| 386 | |
|---|
| 387 | struct RequestQueue : public osg::Referenced |
|---|
| 388 | { |
|---|
| 389 | typedef std::vector< osg::ref_ptr<DatabaseRequest> > RequestList; |
|---|
| 390 | |
|---|
| 391 | void sort(); |
|---|
| 392 | |
|---|
| 393 | RequestList _requestList; |
|---|
| 394 | OpenThreads::Mutex _requestMutex; |
|---|
| 395 | }; |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | typedef std::vector< osg::ref_ptr<DatabaseThread> > DatabaseThreadList; |
|---|
| 399 | typedef std::vector< osg::ref_ptr<osg::Object> > ObjectList; |
|---|
| 400 | |
|---|
| 401 | struct ReadQueue : public RequestQueue |
|---|
| 402 | { |
|---|
| 403 | ReadQueue(DatabasePager* pager, const std::string& name); |
|---|
| 404 | |
|---|
| 405 | void block() { _block->block(); } |
|---|
| 406 | |
|---|
| 407 | void release() { _block->release(); } |
|---|
| 408 | |
|---|
| 409 | void updateBlock() |
|---|
| 410 | { |
|---|
| 411 | _block->set((!_requestList.empty() || !_childrenToDeleteList.empty()) && |
|---|
| 412 | !_pager->_databasePagerThreadPaused); |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | void clear(); |
|---|
| 416 | |
|---|
| 417 | void add(DatabaseRequest* databaseRequest); |
|---|
| 418 | |
|---|
| 419 | void takeFirst(osg::ref_ptr<DatabaseRequest>& databaseRequest); |
|---|
| 420 | |
|---|
| 421 | osg::ref_ptr<osg::RefBlock> _block; |
|---|
| 422 | |
|---|
| 423 | DatabasePager* _pager; |
|---|
| 424 | std::string _name; |
|---|
| 425 | |
|---|
| 426 | OpenThreads::Mutex _childrenToDeleteListMutex; |
|---|
| 427 | ObjectList _childrenToDeleteList; |
|---|
| 428 | }; |
|---|
| 429 | |
|---|
| 430 | // forward declare inner helper classes |
|---|
| 431 | class FindCompileableGLObjectsVisitor; |
|---|
| 432 | friend class FindCompileableGLObjectsVisitor; |
|---|
| 433 | |
|---|
| 434 | class MarkPagedLODsVisitor; |
|---|
| 435 | |
|---|
| 436 | class FindPagedLODsVisitor; |
|---|
| 437 | friend class FindPagedLODsVisitor; |
|---|
| 438 | |
|---|
| 439 | struct SortFileRequestFunctor; |
|---|
| 440 | friend struct SortFileRequestFunctor; |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | OpenThreads::Mutex _run_mutex; |
|---|
| 444 | bool _startThreadCalled; |
|---|
| 445 | |
|---|
| 446 | // Helper functions for determining if objects need to be |
|---|
| 447 | // compiled. |
|---|
| 448 | inline static bool isCompiled(const osg::Texture* texture, |
|---|
| 449 | unsigned int contextID) |
|---|
| 450 | { |
|---|
| 451 | return( texture->getTextureObject(contextID) != NULL ); |
|---|
| 452 | } |
|---|
| 453 | // Is texture compiled for all active contexts? |
|---|
| 454 | inline bool isCompiled(osg::Texture* texture) const |
|---|
| 455 | { |
|---|
| 456 | for (ActiveGraphicsContexts::const_iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 457 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 458 | { |
|---|
| 459 | if ( texture->getTextureObject(*iter) == NULL ) return false; |
|---|
| 460 | } |
|---|
| 461 | return true; |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | inline static bool isCompiled(const osg::StateSet* stateSet, |
|---|
| 465 | unsigned int contextID) |
|---|
| 466 | { |
|---|
| 467 | for (unsigned i = 0; |
|---|
| 468 | i < stateSet->getTextureAttributeList().size(); |
|---|
| 469 | ++i) |
|---|
| 470 | { |
|---|
| 471 | const osg::Texture* texture |
|---|
| 472 | = dynamic_cast<const osg::Texture*>(stateSet->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); |
|---|
| 473 | if (texture && !isCompiled(texture, contextID)) |
|---|
| 474 | return false; |
|---|
| 475 | } |
|---|
| 476 | return true; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | inline bool isCompiled(osg::StateSet* stateSet) |
|---|
| 480 | { |
|---|
| 481 | for (unsigned i = 0; |
|---|
| 482 | i < stateSet->getTextureAttributeList().size(); |
|---|
| 483 | ++i) |
|---|
| 484 | { |
|---|
| 485 | osg::Texture* texture |
|---|
| 486 | = dynamic_cast<osg::Texture*>(stateSet->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); |
|---|
| 487 | if (texture) |
|---|
| 488 | { |
|---|
| 489 | for (ActiveGraphicsContexts::iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 490 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 491 | { |
|---|
| 492 | if ( texture->getTextureObject(*iter) == NULL ) return false; |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | return true; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | inline static bool isCompiled(const osg::Drawable* drawable, |
|---|
| 500 | unsigned int contextID) |
|---|
| 501 | { |
|---|
| 502 | // Worry about vbos later |
|---|
| 503 | if (drawable->getUseDisplayList()) |
|---|
| 504 | { |
|---|
| 505 | return drawable->getDisplayList(contextID) != 0; |
|---|
| 506 | } |
|---|
| 507 | return true; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | inline bool isCompiled(const osg::Drawable* drawable) const |
|---|
| 511 | { |
|---|
| 512 | if (drawable->getUseDisplayList()) |
|---|
| 513 | { |
|---|
| 514 | for (ActiveGraphicsContexts::const_iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 515 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 516 | { |
|---|
| 517 | if ( drawable->getDisplayList(*iter) == 0 ) return false; |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | return true; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | /** Iterate through the active PagedLOD nodes children removing |
|---|
| 525 | * children which havn't been visited since specified expiryTime. |
|---|
| 526 | * note, should be only be called from the update thread. */ |
|---|
| 527 | virtual void removeExpiredSubgraphs(const osg::FrameStamp &frameStamp); |
|---|
| 528 | |
|---|
| 529 | /** Old expiry delay based removeExpiredSubgraphs. */ |
|---|
| 530 | virtual void expiry_removeExpiredSubgraphs(const osg::FrameStamp &frameStamp); |
|---|
| 531 | |
|---|
| 532 | /** New capped based removeExpiredSubgraphs. */ |
|---|
| 533 | virtual void capped_removeExpiredSubgraphs(const osg::FrameStamp &frameStamp); |
|---|
| 534 | |
|---|
| 535 | /** Add the loaded data to the scene graph.*/ |
|---|
| 536 | void addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp); |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | bool _done; |
|---|
| 540 | bool _acceptNewRequests; |
|---|
| 541 | bool _databasePagerThreadPaused; |
|---|
| 542 | |
|---|
| 543 | DatabaseThreadList _databaseThreads; |
|---|
| 544 | |
|---|
| 545 | int _numFramesActive; |
|---|
| 546 | mutable OpenThreads::Mutex _numFramesActiveMutex; |
|---|
| 547 | int _frameNumber; |
|---|
| 548 | |
|---|
| 549 | osg::ref_ptr<ReadQueue> _fileRequestQueue; |
|---|
| 550 | osg::ref_ptr<ReadQueue> _httpRequestQueue; |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | osg::ref_ptr<RequestQueue> _dataToCompileList; |
|---|
| 554 | |
|---|
| 555 | DrawablePolicy _drawablePolicy; |
|---|
| 556 | |
|---|
| 557 | bool _changeAutoUnRef; |
|---|
| 558 | bool _valueAutoUnRef; |
|---|
| 559 | bool _changeAnisotropy; |
|---|
| 560 | float _valueAnisotropy; |
|---|
| 561 | |
|---|
| 562 | bool _deleteRemovedSubgraphsInDatabaseThread; |
|---|
| 563 | |
|---|
| 564 | osg::ref_ptr<RequestQueue> _dataToMergeList; |
|---|
| 565 | |
|---|
| 566 | PagedLODList _activePagedLODList; |
|---|
| 567 | PagedLODList _inactivePagedLODList; |
|---|
| 568 | |
|---|
| 569 | unsigned int _targetMaximumNumberOfPageLOD; |
|---|
| 570 | |
|---|
| 571 | double _expiryDelay; |
|---|
| 572 | int _expiryFrames; |
|---|
| 573 | |
|---|
| 574 | double _releaseDelay; |
|---|
| 575 | int _releaseFrames; |
|---|
| 576 | |
|---|
| 577 | ActiveGraphicsContexts _activeGraphicsContexts; |
|---|
| 578 | // CompileGraphicsContexts _compileGraphicsContexts; |
|---|
| 579 | |
|---|
| 580 | bool _doPreCompile; |
|---|
| 581 | double _targetFrameRate; |
|---|
| 582 | double _minimumTimeAvailableForGLCompileAndDeletePerFrame; |
|---|
| 583 | unsigned int _maximumNumOfObjectsToCompilePerFrame; |
|---|
| 584 | |
|---|
| 585 | double _minimumTimeToMergeTile; |
|---|
| 586 | double _maximumTimeToMergeTile; |
|---|
| 587 | double _totalTimeToMergeTiles; |
|---|
| 588 | unsigned int _numTilesMerges; |
|---|
| 589 | |
|---|
| 590 | struct CompileOperation : public osg::GraphicsOperation |
|---|
| 591 | { |
|---|
| 592 | CompileOperation(DatabasePager* databasePager); |
|---|
| 593 | |
|---|
| 594 | virtual void operator () (osg::GraphicsContext* context); |
|---|
| 595 | |
|---|
| 596 | osg::observer_ptr<DatabasePager> _databasePager; |
|---|
| 597 | }; |
|---|
| 598 | }; |
|---|
| 599 | |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | #endif |
|---|