| 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 | #include <osg/ObserverNodePath> |
|---|
| 24 | #include <osg/observer_ptr> |
|---|
| 25 | |
|---|
| 26 | #include <OpenThreads/Thread> |
|---|
| 27 | #include <OpenThreads/Mutex> |
|---|
| 28 | #include <OpenThreads/ScopedLock> |
|---|
| 29 | #include <OpenThreads/Condition> |
|---|
| 30 | |
|---|
| 31 | #include <osgDB/SharedStateManager> |
|---|
| 32 | #include <osgDB/ReaderWriter> |
|---|
| 33 | #include <osgDB/Options> |
|---|
| 34 | |
|---|
| 35 | #include <map> |
|---|
| 36 | #include <list> |
|---|
| 37 | #include <algorithm> |
|---|
| 38 | #include <functional> |
|---|
| 39 | |
|---|
| 40 | namespace osgDB { |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /** Database paging class which manages the loading of files in a background thread, |
|---|
| 45 | * and synchronizing of loaded models with the main scene graph.*/ |
|---|
| 46 | class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandler |
|---|
| 47 | { |
|---|
| 48 | public : |
|---|
| 49 | |
|---|
| 50 | typedef OpenThreads::Thread::ThreadPriority ThreadPriority; |
|---|
| 51 | |
|---|
| 52 | DatabasePager(); |
|---|
| 53 | |
|---|
| 54 | DatabasePager(const DatabasePager& rhs); |
|---|
| 55 | |
|---|
| 56 | virtual const char* className() const { return "DatabasePager"; } |
|---|
| 57 | |
|---|
| 58 | /** Create a shallow copy on the DatabasePager.*/ |
|---|
| 59 | virtual DatabasePager* clone() const { return new DatabasePager(*this); } |
|---|
| 60 | |
|---|
| 61 | /** get the prototype singleton used by DatabasePager::create().*/ |
|---|
| 62 | static osg::ref_ptr<DatabasePager>& prototype(); |
|---|
| 63 | |
|---|
| 64 | /** create a DatabasePager by cloning DatabasePager::prototype().*/ |
|---|
| 65 | static DatabasePager* create(); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /** Add a request to load a node file to end the the database request list.*/ |
|---|
| 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 | const osg::Referenced* options); |
|---|
| 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 | /** Turn the compilation of rendering objects for specified graphics context on (true) or off(false). */ |
|---|
| 286 | void setCompileGLObjectsForContextID(unsigned int contextID, bool on); |
|---|
| 287 | |
|---|
| 288 | /** Get whether the compilation of rendering objects for specified graphics context on (true) or off(false). */ |
|---|
| 289 | bool getCompileGLObjectsForContextID(unsigned int contextID); |
|---|
| 290 | |
|---|
| 291 | /** Return true if an external draw thread should call compileGLObjects(..) or not.*/ |
|---|
| 292 | bool requiresExternalCompileGLObjects(unsigned int contextID) const; |
|---|
| 293 | |
|---|
| 294 | /** Return true if there are pending compile operations that are required. |
|---|
| 295 | * If requiresCompileGLObjects() return true the application should call compileGLObjects() .*/ |
|---|
| 296 | bool requiresCompileGLObjects() const; |
|---|
| 297 | |
|---|
| 298 | /** Compile the rendering objects (display lists,texture objects, VBO's) on loaded subgraph. |
|---|
| 299 | * note, should only be called from the draw thread. |
|---|
| 300 | * Note, must only be called from a valid graphics context. */ |
|---|
| 301 | virtual void compileGLObjects(osg::State& state,double& availableTime); |
|---|
| 302 | |
|---|
| 303 | /** Compile the rendering objects (display lists,texture objects, VBO's) on loaded subgraph. |
|---|
| 304 | * note, should only be called from the draw thread. |
|---|
| 305 | * Note, must only be called from a valid graphics context. */ |
|---|
| 306 | virtual void compileAllGLObjects(osg::State& state); |
|---|
| 307 | |
|---|
| 308 | /** Report how many items are in the _fileRequestList queue */ |
|---|
| 309 | unsigned int getFileRequestListSize() const { return _fileRequestQueue->_requestList.size() + _httpRequestQueue->_requestList.size(); } |
|---|
| 310 | |
|---|
| 311 | /** Report how many items are in the _dataToCompileList queue */ |
|---|
| 312 | unsigned int getDataToCompileListSize() const { return _dataToCompileList->_requestList.size(); } |
|---|
| 313 | |
|---|
| 314 | /** Report how many items are in the _dataToCompileList queue */ |
|---|
| 315 | unsigned int getDataToMergeListSize() const { return _dataToMergeList->_requestList.size(); } |
|---|
| 316 | |
|---|
| 317 | /** Report whether any requests are in the pager.*/ |
|---|
| 318 | bool getRequestsInProgress() const; |
|---|
| 319 | |
|---|
| 320 | /** 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.*/ |
|---|
| 321 | double getMinimumTimeToMergeTile() const { return _minimumTimeToMergeTile; } |
|---|
| 322 | |
|---|
| 323 | /** 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.*/ |
|---|
| 324 | double getMaximumTimeToMergeTile() const { return _maximumTimeToMergeTile; } |
|---|
| 325 | |
|---|
| 326 | /** 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.*/ |
|---|
| 327 | double getAverageTimeToMergeTiles() const { return (_numTilesMerges > 0) ? _totalTimeToMergeTiles/static_cast<double>(_numTilesMerges) : 0; } |
|---|
| 328 | |
|---|
| 329 | /** Reset the Stats variables.*/ |
|---|
| 330 | void resetStats(); |
|---|
| 331 | |
|---|
| 332 | typedef std::set< osg::ref_ptr<osg::StateSet> > StateSetList; |
|---|
| 333 | typedef std::vector< osg::ref_ptr<osg::Drawable> > DrawableList; |
|---|
| 334 | typedef std::pair<StateSetList,DrawableList> DataToCompile; |
|---|
| 335 | typedef std::map< unsigned int, DataToCompile > DataToCompileMap; |
|---|
| 336 | typedef std::set<unsigned int> ActiveGraphicsContexts; |
|---|
| 337 | typedef std::vector< osg::observer_ptr<osg::GraphicsContext> > CompileGraphicsContexts; |
|---|
| 338 | |
|---|
| 339 | protected: |
|---|
| 340 | |
|---|
| 341 | virtual ~DatabasePager(); |
|---|
| 342 | |
|---|
| 343 | friend class DatabaseThread; |
|---|
| 344 | friend struct DatabaseRequest; |
|---|
| 345 | |
|---|
| 346 | struct RequestQueue; |
|---|
| 347 | |
|---|
| 348 | struct PagedLODObserver : public osg::observer_ptr<osg::PagedLOD> |
|---|
| 349 | { |
|---|
| 350 | typedef osg::observer_ptr<osg::PagedLOD> BaseClass; |
|---|
| 351 | |
|---|
| 352 | PagedLODObserver(osg::PagedLOD* plod):BaseClass(plod) {} |
|---|
| 353 | PagedLODObserver(const PagedLODObserver& plodObserver):BaseClass(plodObserver) {} |
|---|
| 354 | |
|---|
| 355 | PagedLODObserver& operator = (const PagedLODObserver& rhs) |
|---|
| 356 | { |
|---|
| 357 | (*static_cast<BaseClass*>(this)) = rhs; |
|---|
| 358 | return *this; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | virtual void objectDeleted(void* ptr) |
|---|
| 362 | { |
|---|
| 363 | BaseClass::objectDeleted(ptr); |
|---|
| 364 | } |
|---|
| 365 | }; |
|---|
| 366 | |
|---|
| 367 | typedef std::list< PagedLODObserver > PagedLODList; |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | struct DatabaseRequest : public osg::Referenced |
|---|
| 371 | { |
|---|
| 372 | DatabaseRequest(): |
|---|
| 373 | osg::Referenced(true), |
|---|
| 374 | _valid(false), |
|---|
| 375 | _frameNumberFirstRequest(0), |
|---|
| 376 | _timestampFirstRequest(0.0), |
|---|
| 377 | _priorityFirstRequest(0.f), |
|---|
| 378 | _frameNumberLastRequest(0), |
|---|
| 379 | _timestampLastRequest(0.0), |
|---|
| 380 | _priorityLastRequest(0.0f), |
|---|
| 381 | _numOfRequests(0), |
|---|
| 382 | _requestQueue(0) |
|---|
| 383 | {} |
|---|
| 384 | |
|---|
| 385 | void invalidate(); |
|---|
| 386 | |
|---|
| 387 | bool valid() const { return _valid; } |
|---|
| 388 | |
|---|
| 389 | bool _valid; |
|---|
| 390 | std::string _fileName; |
|---|
| 391 | int _frameNumberFirstRequest; |
|---|
| 392 | double _timestampFirstRequest; |
|---|
| 393 | float _priorityFirstRequest; |
|---|
| 394 | int _frameNumberLastRequest; |
|---|
| 395 | double _timestampLastRequest; |
|---|
| 396 | float _priorityLastRequest; |
|---|
| 397 | unsigned int _numOfRequests; |
|---|
| 398 | osg::ObserverNodePath _observerNodePath; |
|---|
| 399 | osg::Group* _groupForAddingLoadedSubgraph; |
|---|
| 400 | osg::ref_ptr<osg::Node> _loadedModel; |
|---|
| 401 | DataToCompileMap _dataToCompileMap; |
|---|
| 402 | osg::ref_ptr<Options> _loadOptions; |
|---|
| 403 | RequestQueue* _requestQueue; |
|---|
| 404 | |
|---|
| 405 | bool isRequestCurrent (int frameNumber) const |
|---|
| 406 | { |
|---|
| 407 | return _valid && (frameNumber - _frameNumberLastRequest <= 1); |
|---|
| 408 | } |
|---|
| 409 | }; |
|---|
| 410 | |
|---|
| 411 | struct RequestQueue : public osg::Referenced |
|---|
| 412 | { |
|---|
| 413 | public: |
|---|
| 414 | |
|---|
| 415 | RequestQueue(DatabasePager* pager); |
|---|
| 416 | |
|---|
| 417 | void add(DatabaseRequest* databaseRequest); |
|---|
| 418 | |
|---|
| 419 | void takeFirst(osg::ref_ptr<DatabaseRequest>& databaseRequest); |
|---|
| 420 | |
|---|
| 421 | /// prune all the old requests and then return true if requestList left empty |
|---|
| 422 | bool pruneOldRequestsAndCheckIfEmpty(); |
|---|
| 423 | |
|---|
| 424 | virtual void updateBlock() {} |
|---|
| 425 | |
|---|
| 426 | void clear(); |
|---|
| 427 | |
|---|
| 428 | typedef std::list< osg::ref_ptr<DatabaseRequest> > RequestList; |
|---|
| 429 | |
|---|
| 430 | DatabasePager* _pager; |
|---|
| 431 | RequestList _requestList; |
|---|
| 432 | OpenThreads::Mutex _requestMutex; |
|---|
| 433 | int _frameNumberLastPruned; |
|---|
| 434 | |
|---|
| 435 | protected: |
|---|
| 436 | virtual ~RequestQueue(); |
|---|
| 437 | }; |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | typedef std::vector< osg::ref_ptr<DatabaseThread> > DatabaseThreadList; |
|---|
| 441 | typedef std::vector< osg::ref_ptr<osg::Object> > ObjectList; |
|---|
| 442 | |
|---|
| 443 | struct ReadQueue : public RequestQueue |
|---|
| 444 | { |
|---|
| 445 | ReadQueue(DatabasePager* pager, const std::string& name); |
|---|
| 446 | |
|---|
| 447 | void block() { _block->block(); } |
|---|
| 448 | |
|---|
| 449 | void release() { _block->release(); } |
|---|
| 450 | |
|---|
| 451 | virtual void updateBlock(); |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | osg::ref_ptr<osg::RefBlock> _block; |
|---|
| 455 | |
|---|
| 456 | std::string _name; |
|---|
| 457 | |
|---|
| 458 | OpenThreads::Mutex _childrenToDeleteListMutex; |
|---|
| 459 | ObjectList _childrenToDeleteList; |
|---|
| 460 | }; |
|---|
| 461 | |
|---|
| 462 | // forward declare inner helper classes |
|---|
| 463 | class FindCompileableGLObjectsVisitor; |
|---|
| 464 | friend class FindCompileableGLObjectsVisitor; |
|---|
| 465 | |
|---|
| 466 | class CountPagedLODsVisitor; |
|---|
| 467 | class FindPagedLODsVisitor; |
|---|
| 468 | friend class FindPagedLODsVisitor; |
|---|
| 469 | |
|---|
| 470 | struct SortFileRequestFunctor; |
|---|
| 471 | friend struct SortFileRequestFunctor; |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | OpenThreads::Mutex _run_mutex; |
|---|
| 475 | bool _startThreadCalled; |
|---|
| 476 | |
|---|
| 477 | // Helper functions for determining if objects need to be |
|---|
| 478 | // compiled. |
|---|
| 479 | inline static bool isCompiled(const osg::Texture* texture, |
|---|
| 480 | unsigned int contextID) |
|---|
| 481 | { |
|---|
| 482 | return( texture->getTextureObject(contextID) != NULL ); |
|---|
| 483 | } |
|---|
| 484 | // Is texture compiled for all active contexts? |
|---|
| 485 | inline bool isCompiled(osg::Texture* texture) const |
|---|
| 486 | { |
|---|
| 487 | for (ActiveGraphicsContexts::const_iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 488 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 489 | { |
|---|
| 490 | if ( texture->getTextureObject(*iter) == NULL ) return false; |
|---|
| 491 | } |
|---|
| 492 | return true; |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | inline static bool isCompiled(const osg::StateSet* stateSet, |
|---|
| 496 | unsigned int contextID) |
|---|
| 497 | { |
|---|
| 498 | for (unsigned i = 0; |
|---|
| 499 | i < stateSet->getTextureAttributeList().size(); |
|---|
| 500 | ++i) |
|---|
| 501 | { |
|---|
| 502 | const osg::Texture* texture |
|---|
| 503 | = dynamic_cast<const osg::Texture*>(stateSet->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); |
|---|
| 504 | if (texture && !isCompiled(texture, contextID)) |
|---|
| 505 | return false; |
|---|
| 506 | } |
|---|
| 507 | return true; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | inline bool isCompiled(osg::StateSet* stateSet) |
|---|
| 511 | { |
|---|
| 512 | for (unsigned i = 0; |
|---|
| 513 | i < stateSet->getTextureAttributeList().size(); |
|---|
| 514 | ++i) |
|---|
| 515 | { |
|---|
| 516 | osg::Texture* texture |
|---|
| 517 | = dynamic_cast<osg::Texture*>(stateSet->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); |
|---|
| 518 | if (texture) |
|---|
| 519 | { |
|---|
| 520 | for (ActiveGraphicsContexts::iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 521 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 522 | { |
|---|
| 523 | if ( texture->getTextureObject(*iter) == NULL ) return false; |
|---|
| 524 | } |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | return true; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | inline static bool isCompiled(const osg::Drawable* drawable, |
|---|
| 531 | unsigned int contextID) |
|---|
| 532 | { |
|---|
| 533 | if (drawable->getUseVertexBufferObjects()) |
|---|
| 534 | { |
|---|
| 535 | // say it's not compiled leaving it up to the compileGLObjects() to handle. |
|---|
| 536 | return false; |
|---|
| 537 | } |
|---|
| 538 | else if (drawable->getUseDisplayList()) |
|---|
| 539 | { |
|---|
| 540 | return drawable->getDisplayList(contextID) != 0; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | return true; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | inline bool isCompiled(const osg::Drawable* drawable) const |
|---|
| 547 | { |
|---|
| 548 | if (drawable->getUseVertexBufferObjects()) |
|---|
| 549 | { |
|---|
| 550 | // say it's not compiled leaving it up to the compileGLObjects() to handle. |
|---|
| 551 | return false; |
|---|
| 552 | } |
|---|
| 553 | if (drawable->getUseDisplayList()) |
|---|
| 554 | { |
|---|
| 555 | for (ActiveGraphicsContexts::const_iterator iter=_activeGraphicsContexts.begin(); |
|---|
| 556 | iter!=_activeGraphicsContexts.end(); ++iter ) |
|---|
| 557 | { |
|---|
| 558 | if ( drawable->getDisplayList(*iter) == 0 ) return false; |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | return true; |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | /** Iterate through the active PagedLOD nodes children removing |
|---|
| 566 | * children which havn't been visited since specified expiryTime. |
|---|
| 567 | * note, should be only be called from the update thread. */ |
|---|
| 568 | virtual void removeExpiredSubgraphs(const osg::FrameStamp &frameStamp); |
|---|
| 569 | |
|---|
| 570 | /** Add the loaded data to the scene graph.*/ |
|---|
| 571 | void addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp); |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | bool _done; |
|---|
| 575 | bool _acceptNewRequests; |
|---|
| 576 | bool _databasePagerThreadPaused; |
|---|
| 577 | |
|---|
| 578 | DatabaseThreadList _databaseThreads; |
|---|
| 579 | |
|---|
| 580 | int _numFramesActive; |
|---|
| 581 | mutable OpenThreads::Mutex _numFramesActiveMutex; |
|---|
| 582 | int _frameNumber; |
|---|
| 583 | |
|---|
| 584 | osg::ref_ptr<ReadQueue> _fileRequestQueue; |
|---|
| 585 | osg::ref_ptr<ReadQueue> _httpRequestQueue; |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | osg::ref_ptr<RequestQueue> _dataToCompileList; |
|---|
| 589 | |
|---|
| 590 | DrawablePolicy _drawablePolicy; |
|---|
| 591 | |
|---|
| 592 | bool _changeAutoUnRef; |
|---|
| 593 | bool _valueAutoUnRef; |
|---|
| 594 | bool _changeAnisotropy; |
|---|
| 595 | float _valueAnisotropy; |
|---|
| 596 | |
|---|
| 597 | bool _deleteRemovedSubgraphsInDatabaseThread; |
|---|
| 598 | |
|---|
| 599 | osg::ref_ptr<RequestQueue> _dataToMergeList; |
|---|
| 600 | |
|---|
| 601 | PagedLODList _activePagedLODList; |
|---|
| 602 | PagedLODList _inactivePagedLODList; |
|---|
| 603 | |
|---|
| 604 | unsigned int _targetMaximumNumberOfPageLOD; |
|---|
| 605 | |
|---|
| 606 | double _expiryDelay; |
|---|
| 607 | int _expiryFrames; |
|---|
| 608 | |
|---|
| 609 | double _releaseDelay; |
|---|
| 610 | int _releaseFrames; |
|---|
| 611 | |
|---|
| 612 | ActiveGraphicsContexts _activeGraphicsContexts; |
|---|
| 613 | // CompileGraphicsContexts _compileGraphicsContexts; |
|---|
| 614 | |
|---|
| 615 | bool _doPreCompile; |
|---|
| 616 | double _targetFrameRate; |
|---|
| 617 | double _minimumTimeAvailableForGLCompileAndDeletePerFrame; |
|---|
| 618 | unsigned int _maximumNumOfObjectsToCompilePerFrame; |
|---|
| 619 | |
|---|
| 620 | double _minimumTimeToMergeTile; |
|---|
| 621 | double _maximumTimeToMergeTile; |
|---|
| 622 | double _totalTimeToMergeTiles; |
|---|
| 623 | unsigned int _numTilesMerges; |
|---|
| 624 | |
|---|
| 625 | struct CompileOperation : public osg::GraphicsOperation |
|---|
| 626 | { |
|---|
| 627 | CompileOperation(DatabasePager* databasePager); |
|---|
| 628 | |
|---|
| 629 | virtual void operator () (osg::GraphicsContext* context); |
|---|
| 630 | |
|---|
| 631 | osg::observer_ptr<DatabasePager> _databasePager; |
|---|
| 632 | }; |
|---|
| 633 | }; |
|---|
| 634 | |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | #endif |
|---|