| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/DatabasePager> |
|---|
| 15 | #include <osgDB/WriteFile> |
|---|
| 16 | #include <osgDB/FileNameUtils> |
|---|
| 17 | #include <osgDB/FileUtils> |
|---|
| 18 | #include <osgDB/Registry> |
|---|
| 19 | |
|---|
| 20 | #include <osg/Geode> |
|---|
| 21 | #include <osg/Timer> |
|---|
| 22 | #include <osg/Texture> |
|---|
| 23 | #include <osg/Notify> |
|---|
| 24 | #include <osg/ProxyNode> |
|---|
| 25 | #include <osg/ApplicationUsage> |
|---|
| 26 | |
|---|
| 27 | #include <OpenThreads/ScopedLock> |
|---|
| 28 | |
|---|
| 29 | #include <algorithm> |
|---|
| 30 | #include <functional> |
|---|
| 31 | #include <set> |
|---|
| 32 | |
|---|
| 33 | #include <stdlib.h> |
|---|
| 34 | #include <string.h> |
|---|
| 35 | |
|---|
| 36 | #ifdef WIN32 |
|---|
| 37 | #include <windows.h> |
|---|
| 38 | #else |
|---|
| 39 | #include <unistd.h> |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | using namespace osgDB; |
|---|
| 43 | using namespace OpenThreads; |
|---|
| 44 | |
|---|
| 45 | static osg::ApplicationUsageProxy DatabasePager_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_DO_PRE_COMPILE <ON/OFF>","Switch on or off the pre compile of OpenGL object database pager."); |
|---|
| 46 | static osg::ApplicationUsageProxy DatabasePager_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MINIMUM_COMPILE_TIME_PER_FRAME <float>","minimum compile time alloted to compiling OpenGL objects per frame in database pager."); |
|---|
| 47 | static osg::ApplicationUsageProxy DatabasePager_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MAXIMUM_OBJECTS_TO_COMPILE_PER_FRAME <int>","maximum number of OpenGL objects to compile per frame in database pager."); |
|---|
| 48 | static osg::ApplicationUsageProxy DatabasePager_e3(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_DATABASE_PAGER_DRAWABLE <mode>","Set the drawable policy for setting of loaded drawable to specified type. mode can be one of DoNotModify, DisplayList, VBO or VertexArrays>."); |
|---|
| 49 | static osg::ApplicationUsageProxy DatabasePager_e4(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_DATABASE_PAGER_PRIORITY <mode>", "Set the thread priority to DEFAULT, MIN, LOW, NOMINAL, HIGH or MAX."); |
|---|
| 50 | static osg::ApplicationUsageProxy DatabasePager_e7(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_EXPIRY_DELAY <float> ","Set the length of time a PagedLOD child is kept in memory, without being used, before its tagged as expired, and ear marked to deletion."); |
|---|
| 51 | static osg::ApplicationUsageProxy DatabasePager_e8(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_EXPIRY_FRAMES <int> ","Set number of frames a PagedLOD child is kept in memory, without being used, before its tagged as expired, and ear marked to deletion."); |
|---|
| 52 | static osg::ApplicationUsageProxy DatabasePager_e9(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_RELEASE_DELAY <float> ","Set the length of time a PagedLOD child's OpenGL objects are kept in memory, without being used, before be released (setting to OFF disables this feature.)"); |
|---|
| 53 | static osg::ApplicationUsageProxy DatabasePager_e10(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_RELEASE_FRAMES <int> ","Set number of frames a PagedLOD child's OpenGL objects are kept in memory, without being used, before be released."); |
|---|
| 54 | static osg::ApplicationUsageProxy DatabasePager_e11(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MAX_PAGEDLOD <num>","Set the target maximum number of PagedLOD to maintain."); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | namespace |
|---|
| 64 | { |
|---|
| 65 | template <typename U> |
|---|
| 66 | struct PointerTraits |
|---|
| 67 | { |
|---|
| 68 | typedef class NullType {} PointeeType; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | template <typename U> |
|---|
| 72 | struct PointerTraits<U*> |
|---|
| 73 | { |
|---|
| 74 | typedef U PointeeType; |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | template <typename U> |
|---|
| 78 | struct PointerTraits<const U*> |
|---|
| 79 | { |
|---|
| 80 | typedef U PointeeType; |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | template <typename FuncObj> |
|---|
| 84 | class RefPtrAdapter |
|---|
| 85 | : public std::unary_function<const osg::ref_ptr<typename PointerTraits<typename FuncObj::argument_type>::PointeeType>, |
|---|
| 86 | typename FuncObj::result_type> |
|---|
| 87 | { |
|---|
| 88 | public: |
|---|
| 89 | typedef typename PointerTraits<typename FuncObj::argument_type>::PointeeType PointeeType; |
|---|
| 90 | typedef osg::ref_ptr<PointeeType> RefPtrType; |
|---|
| 91 | explicit RefPtrAdapter(const FuncObj& funcObj) : _func(funcObj) {} |
|---|
| 92 | typename FuncObj::result_type operator()(const RefPtrType& refPtr) const |
|---|
| 93 | { |
|---|
| 94 | return _func(refPtr.get()); |
|---|
| 95 | } |
|---|
| 96 | protected: |
|---|
| 97 | FuncObj _func; |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | template <typename FuncObj> |
|---|
| 101 | RefPtrAdapter<FuncObj> refPtrAdapt(const FuncObj& func) |
|---|
| 102 | { |
|---|
| 103 | return RefPtrAdapter<FuncObj>(func); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | class DatabasePager::FindCompileableGLObjectsVisitor : public osg::NodeVisitor |
|---|
| 112 | { |
|---|
| 113 | public: |
|---|
| 114 | FindCompileableGLObjectsVisitor(DatabasePager::DataToCompile* dataToCompile, |
|---|
| 115 | bool changeAutoUnRef, bool valueAutoUnRef, |
|---|
| 116 | bool changeAnisotropy, float valueAnisotropy, |
|---|
| 117 | DatabasePager::DrawablePolicy drawablePolicy, |
|---|
| 118 | const DatabasePager* pager): |
|---|
| 119 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 120 | _dataToCompile(dataToCompile), |
|---|
| 121 | _changeAutoUnRef(changeAutoUnRef), _valueAutoUnRef(valueAutoUnRef), |
|---|
| 122 | _changeAnisotropy(changeAnisotropy), _valueAnisotropy(valueAnisotropy), |
|---|
| 123 | _drawablePolicy(drawablePolicy), _pager(pager) |
|---|
| 124 | { |
|---|
| 125 | if (osgDB::Registry::instance()->getBuildKdTreesHint()==osgDB::Options::BUILD_KDTREES && |
|---|
| 126 | osgDB::Registry::instance()->getKdTreeBuilder()) |
|---|
| 127 | { |
|---|
| 128 | _kdTreeBuilder = osgDB::Registry::instance()->getKdTreeBuilder()->clone(); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | META_NodeVisitor("osgDB","FindCompileableGLObjectsVisitor") |
|---|
| 133 | |
|---|
| 134 | virtual void apply(osg::Node& node) |
|---|
| 135 | { |
|---|
| 136 | apply(node.getStateSet()); |
|---|
| 137 | |
|---|
| 138 | traverse(node); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | virtual void apply(osg::Geode& geode) |
|---|
| 142 | { |
|---|
| 143 | apply(geode.getStateSet()); |
|---|
| 144 | |
|---|
| 145 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 146 | { |
|---|
| 147 | apply(geode.getDrawable(i)); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | traverse(geode); |
|---|
| 151 | |
|---|
| 152 | if (_kdTreeBuilder.valid()) |
|---|
| 153 | { |
|---|
| 154 | geode.accept(*_kdTreeBuilder); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | inline void apply(osg::StateSet* stateset) |
|---|
| 159 | { |
|---|
| 160 | if (stateset) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | bool compileStateSet = false; |
|---|
| 168 | for(unsigned int i=0;i<stateset->getTextureAttributeList().size();++i) |
|---|
| 169 | { |
|---|
| 170 | osg::Texture* texture = dynamic_cast<osg::Texture*>(stateset->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); |
|---|
| 171 | |
|---|
| 172 | if (texture && !_textureSet.count(texture)) |
|---|
| 173 | { |
|---|
| 174 | _textureSet.insert(texture); |
|---|
| 175 | if (_changeAutoUnRef) texture->setUnRefImageDataAfterApply(_valueAutoUnRef); |
|---|
| 176 | if ((_changeAnisotropy |
|---|
| 177 | && texture->getMaxAnisotropy() != _valueAnisotropy)) |
|---|
| 178 | { |
|---|
| 179 | if (_changeAnisotropy) |
|---|
| 180 | texture->setMaxAnisotropy(_valueAnisotropy); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if (!_pager->isCompiled(texture)) |
|---|
| 184 | { |
|---|
| 185 | compileStateSet = true; |
|---|
| 186 | if (osg::getNotifyLevel() >= osg::DEBUG_INFO) |
|---|
| 187 | { |
|---|
| 188 | osg::notify(osg::DEBUG_INFO) |
|---|
| 189 | <<"Found compilable texture " << texture << " "; |
|---|
| 190 | osg::Image* image = texture->getImage(0); |
|---|
| 191 | if (image) osg::notify(osg::DEBUG_INFO) << image->getFileName(); |
|---|
| 192 | osg::notify(osg::DEBUG_INFO) << std:: endl; |
|---|
| 193 | } |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | if (compileStateSet && _dataToCompile) |
|---|
| 199 | { |
|---|
| 200 | _dataToCompile->first.insert(stateset); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | inline void apply(osg::Drawable* drawable) |
|---|
| 207 | { |
|---|
| 208 | if (_drawableSet.count(drawable)) |
|---|
| 209 | return; |
|---|
| 210 | |
|---|
| 211 | _drawableSet.insert(drawable); |
|---|
| 212 | |
|---|
| 213 | apply(drawable->getStateSet()); |
|---|
| 214 | |
|---|
| 215 | switch(_drawablePolicy) |
|---|
| 216 | { |
|---|
| 217 | case DatabasePager::DO_NOT_MODIFY_DRAWABLE_SETTINGS: |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | break; |
|---|
| 221 | case DatabasePager::USE_DISPLAY_LISTS: |
|---|
| 222 | drawable->setUseDisplayList(true); |
|---|
| 223 | drawable->setUseVertexBufferObjects(false); |
|---|
| 224 | break; |
|---|
| 225 | case DatabasePager::USE_VERTEX_BUFFER_OBJECTS: |
|---|
| 226 | drawable->setUseDisplayList(true); |
|---|
| 227 | drawable->setUseVertexBufferObjects(true); |
|---|
| 228 | |
|---|
| 229 | break; |
|---|
| 230 | case DatabasePager::USE_VERTEX_ARRAYS: |
|---|
| 231 | drawable->setUseDisplayList(false); |
|---|
| 232 | drawable->setUseVertexBufferObjects(false); |
|---|
| 233 | |
|---|
| 234 | break; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if (_dataToCompile && drawable->getUseDisplayList() && !_pager->isCompiled(drawable)) |
|---|
| 242 | { |
|---|
| 243 | |
|---|
| 244 | _dataToCompile->second.push_back(drawable); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | DatabasePager::DataToCompile* _dataToCompile; |
|---|
| 249 | bool _changeAutoUnRef; |
|---|
| 250 | bool _valueAutoUnRef; |
|---|
| 251 | bool _changeAnisotropy; |
|---|
| 252 | float _valueAnisotropy; |
|---|
| 253 | DatabasePager::DrawablePolicy _drawablePolicy; |
|---|
| 254 | const DatabasePager* _pager; |
|---|
| 255 | std::set<osg::ref_ptr<osg::Texture> > _textureSet; |
|---|
| 256 | std::set<osg::ref_ptr<osg::Drawable> > _drawableSet; |
|---|
| 257 | osg::ref_ptr<osg::KdTreeBuilder> _kdTreeBuilder; |
|---|
| 258 | |
|---|
| 259 | protected: |
|---|
| 260 | |
|---|
| 261 | FindCompileableGLObjectsVisitor& operator = (const FindCompileableGLObjectsVisitor&) { return *this; } |
|---|
| 262 | }; |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | struct DatabasePager::SortFileRequestFunctor |
|---|
| 270 | { |
|---|
| 271 | bool operator() (const osg::ref_ptr<DatabasePager::DatabaseRequest>& lhs,const osg::ref_ptr<DatabasePager::DatabaseRequest>& rhs) const |
|---|
| 272 | { |
|---|
| 273 | if (lhs->_timestampLastRequest>rhs->_timestampLastRequest) return true; |
|---|
| 274 | else if (lhs->_timestampLastRequest<rhs->_timestampLastRequest) return false; |
|---|
| 275 | else return (lhs->_priorityLastRequest>rhs->_priorityLastRequest); |
|---|
| 276 | } |
|---|
| 277 | }; |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | void DatabasePager::RequestQueue::sort() |
|---|
| 285 | { |
|---|
| 286 | std::sort(_requestList.begin(),_requestList.end(),SortFileRequestFunctor()); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | DatabasePager::ReadQueue::ReadQueue(DatabasePager* pager, const std::string& name): |
|---|
| 295 | _pager(pager), |
|---|
| 296 | _name(name) |
|---|
| 297 | { |
|---|
| 298 | _block = new osg::RefBlock; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | void DatabasePager::ReadQueue::clear() |
|---|
| 302 | { |
|---|
| 303 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_requestMutex); |
|---|
| 304 | |
|---|
| 305 | for(RequestList::iterator citr = _requestList.begin(); |
|---|
| 306 | citr != _requestList.end(); |
|---|
| 307 | ++citr) |
|---|
| 308 | { |
|---|
| 309 | (*citr)->_loadedModel = 0; |
|---|
| 310 | (*citr)->_requestQueue = 0; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | _requestList.clear(); |
|---|
| 314 | |
|---|
| 315 | updateBlock(); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | void DatabasePager::ReadQueue::add(DatabasePager::DatabaseRequest* databaseRequest) |
|---|
| 319 | { |
|---|
| 320 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_requestMutex); |
|---|
| 321 | _requestList.push_back(databaseRequest); |
|---|
| 322 | databaseRequest->_requestQueue = this; |
|---|
| 323 | |
|---|
| 324 | updateBlock(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void DatabasePager::ReadQueue::takeFirst(osg::ref_ptr<DatabaseRequest>& databaseRequest) |
|---|
| 328 | { |
|---|
| 329 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_requestMutex); |
|---|
| 330 | |
|---|
| 331 | if (!_requestList.empty()) |
|---|
| 332 | { |
|---|
| 333 | sort(); |
|---|
| 334 | |
|---|
| 335 | databaseRequest = _requestList.front(); |
|---|
| 336 | databaseRequest->_requestQueue = 0; |
|---|
| 337 | _requestList.erase(_requestList.begin()); |
|---|
| 338 | |
|---|
| 339 | updateBlock(); |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | DatabasePager::DatabaseThread::DatabaseThread(DatabasePager* pager, Mode mode, const std::string& name): |
|---|
| 348 | _done(false), |
|---|
| 349 | _active(false), |
|---|
| 350 | _pager(pager), |
|---|
| 351 | _mode(mode), |
|---|
| 352 | _name(name) |
|---|
| 353 | { |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | DatabasePager::DatabaseThread::DatabaseThread(const DatabaseThread& dt, DatabasePager* pager): |
|---|
| 357 | _done(false), |
|---|
| 358 | _active(false), |
|---|
| 359 | _pager(pager), |
|---|
| 360 | _mode(dt._mode), |
|---|
| 361 | _name(dt._name) |
|---|
| 362 | { |
|---|
| 363 | |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | DatabasePager::DatabaseThread::~DatabaseThread() |
|---|
| 367 | { |
|---|
| 368 | cancel(); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | int DatabasePager::DatabaseThread::cancel() |
|---|
| 372 | { |
|---|
| 373 | int result = 0; |
|---|
| 374 | |
|---|
| 375 | if( isRunning() ) |
|---|
| 376 | { |
|---|
| 377 | |
|---|
| 378 | _done = true; |
|---|
| 379 | |
|---|
| 380 | switch(_mode) |
|---|
| 381 | { |
|---|
| 382 | case(HANDLE_ALL_REQUESTS): |
|---|
| 383 | _pager->_fileRequestQueue->release(); |
|---|
| 384 | break; |
|---|
| 385 | case(HANDLE_NON_HTTP): |
|---|
| 386 | _pager->_fileRequestQueue->release(); |
|---|
| 387 | break; |
|---|
| 388 | case(HANDLE_ONLY_HTTP): |
|---|
| 389 | _pager->_httpRequestQueue->release(); |
|---|
| 390 | break; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | while(isRunning()) |
|---|
| 398 | { |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | OpenThreads::Thread::YieldCurrentThread(); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | return result; |
|---|
| 409 | |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | void DatabasePager::DatabaseThread::run() |
|---|
| 413 | { |
|---|
| 414 | osg::notify(osg::INFO)<<_name<<": DatabasePager::DatabaseThread::run"<<std::endl; |
|---|
| 415 | |
|---|
| 416 | #if 1 |
|---|
| 417 | |
|---|
| 418 | osg::Texture::setMinimumNumberOfTextureObjectsToRetainInCache(100); |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | osg::Drawable::setMinimumNumberOfDisplayListsToRetainInCache(100); |
|---|
| 422 | #else |
|---|
| 423 | |
|---|
| 424 | osg::Texture::setMinimumNumberOfTextureObjectsToRetainInCache(0); |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | osg::Drawable::setMinimumNumberOfDisplayListsToRetainInCache(0); |
|---|
| 428 | #endif |
|---|
| 429 | |
|---|
| 430 | bool firstTime = true; |
|---|
| 431 | |
|---|
| 432 | osg::ref_ptr<DatabasePager::ReadQueue> read_queue; |
|---|
| 433 | osg::ref_ptr<DatabasePager::ReadQueue> out_queue; |
|---|
| 434 | |
|---|
| 435 | switch(_mode) |
|---|
| 436 | { |
|---|
| 437 | case(HANDLE_ALL_REQUESTS): |
|---|
| 438 | read_queue = _pager->_fileRequestQueue; |
|---|
| 439 | break; |
|---|
| 440 | case(HANDLE_NON_HTTP): |
|---|
| 441 | read_queue = _pager->_fileRequestQueue; |
|---|
| 442 | out_queue = _pager->_httpRequestQueue; |
|---|
| 443 | break; |
|---|
| 444 | case(HANDLE_ONLY_HTTP): |
|---|
| 445 | read_queue = _pager->_httpRequestQueue; |
|---|
| 446 | break; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | do |
|---|
| 451 | { |
|---|
| 452 | _active = false; |
|---|
| 453 | |
|---|
| 454 | read_queue->block(); |
|---|
| 455 | |
|---|
| 456 | _active = true; |
|---|
| 457 | |
|---|
| 458 | osg::notify(osg::INFO)<<_name<<": _pager->_requestList.size()= "<<read_queue->_requestList.size()<<" to delete = "<<read_queue->_childrenToDeleteList.size()<<std::endl; |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | if (_pager->_deleteRemovedSubgraphsInDatabaseThread && !(read_queue->_childrenToDeleteList.empty())) |
|---|
| 465 | { |
|---|
| 466 | ObjectList deleteList; |
|---|
| 467 | |
|---|
| 468 | { |
|---|
| 469 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(read_queue->_childrenToDeleteListMutex); |
|---|
| 470 | deleteList.swap(read_queue->_childrenToDeleteList); |
|---|
| 471 | read_queue->updateBlock(); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | osg::ref_ptr<DatabaseRequest> databaseRequest; |
|---|
| 479 | read_queue->takeFirst(databaseRequest); |
|---|
| 480 | |
|---|
| 481 | bool readFromFileCache = false; |
|---|
| 482 | |
|---|
| 483 | osg::ref_ptr<FileCache> fileCache = osgDB::Registry::instance()->getFileCache(); |
|---|
| 484 | osg::ref_ptr<FileLocationCallback> fileLocationCallback = osgDB::Registry::instance()->getFileLocationCallback(); |
|---|
| 485 | |
|---|
| 486 | if (databaseRequest.valid()) |
|---|
| 487 | { |
|---|
| 488 | if (databaseRequest->_loadOptions.valid()) |
|---|
| 489 | { |
|---|
| 490 | if (databaseRequest->_loadOptions->getFileCache()) fileCache = databaseRequest->_loadOptions->getFileCache(); |
|---|
| 491 | if (databaseRequest->_loadOptions->getFileLocationCallback()) fileLocationCallback = databaseRequest->_loadOptions->getFileLocationCallback(); |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | if (fileLocationCallback.valid() && !fileLocationCallback->useFileCache()) fileCache = 0; |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | if ((_pager->_frameNumber-databaseRequest->_frameNumberLastRequest)<=1) |
|---|
| 500 | { |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | switch(_mode) |
|---|
| 504 | { |
|---|
| 505 | case(HANDLE_ALL_REQUESTS): |
|---|
| 506 | { |
|---|
| 507 | |
|---|
| 508 | if (fileCache.valid() && fileCache->isFileAppropriateForFileCache(databaseRequest->_fileName)) |
|---|
| 509 | { |
|---|
| 510 | if (fileCache->existsInCache(databaseRequest->_fileName)) |
|---|
| 511 | { |
|---|
| 512 | readFromFileCache = true; |
|---|
| 513 | } |
|---|
| 514 | } |
|---|
| 515 | break; |
|---|
| 516 | } |
|---|
| 517 | case(HANDLE_NON_HTTP): |
|---|
| 518 | { |
|---|
| 519 | |
|---|
| 520 | bool isHighLatencyFileRequest = false; |
|---|
| 521 | |
|---|
| 522 | if (fileLocationCallback.valid()) |
|---|
| 523 | { |
|---|
| 524 | isHighLatencyFileRequest = fileLocationCallback->fileLocation(databaseRequest->_fileName, databaseRequest->_loadOptions.get()) == FileLocationCallback::REMOTE_FILE; |
|---|
| 525 | } |
|---|
| 526 | else if (fileCache.valid() && fileCache->isFileAppropriateForFileCache(databaseRequest->_fileName)) |
|---|
| 527 | { |
|---|
| 528 | isHighLatencyFileRequest = true; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | if (isHighLatencyFileRequest) |
|---|
| 532 | { |
|---|
| 533 | if (fileCache.valid() && fileCache->existsInCache(databaseRequest->_fileName)) |
|---|
| 534 | { |
|---|
| 535 | readFromFileCache = true; |
|---|
| 536 | } |
|---|
| 537 | else |
|---|
| 538 | { |
|---|
| 539 | osg::notify(osg::INFO)<<_name<<": Passing http requests over "<<databaseRequest->_fileName<<std::endl; |
|---|
| 540 | out_queue->add(databaseRequest.get()); |
|---|
| 541 | databaseRequest = 0; |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | break; |
|---|
| 545 | } |
|---|
| 546 | case(HANDLE_ONLY_HTTP): |
|---|
| 547 | { |
|---|
| 548 | |
|---|
| 549 | break; |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | else |
|---|
| 554 | { |
|---|
| 555 | databaseRequest = 0; |
|---|
| 556 | } |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | if (databaseRequest.valid()) |
|---|
| 561 | { |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | ReaderWriter::ReadResult rr = readFromFileCache ? |
|---|
| 571 | fileCache->readNode(databaseRequest->_fileName, databaseRequest->_loadOptions.get(), false) : |
|---|
| 572 | Registry::instance()->readNode(databaseRequest->_fileName, databaseRequest->_loadOptions.get(), false); |
|---|
| 573 | |
|---|
| 574 | if (rr.validNode()) databaseRequest->_loadedModel = rr.getNode(); |
|---|
| 575 | if (rr.error()) osg::notify(osg::WARN)<<"Error in reading file "<<databaseRequest->_fileName<<" : "<<rr.message() << std::endl; |
|---|
| 576 | |
|---|
| 577 | if (databaseRequest->_loadedModel.valid() && |
|---|
| 578 | fileCache.valid() && |
|---|
| 579 | fileCache->isFileAppropriateForFileCache(databaseRequest->_fileName) && |
|---|
| 580 | !readFromFileCache) |
|---|
| 581 | { |
|---|
| 582 | fileCache->writeNode(*(databaseRequest->_loadedModel), databaseRequest->_fileName, databaseRequest->_loadOptions.get()); |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | if ((_pager->_frameNumber-databaseRequest->_frameNumberLastRequest)>1) |
|---|
| 586 | { |
|---|
| 587 | osg::notify(osg::INFO)<<_name<<": Warning DatabaseRquest no longer required."<<std::endl; |
|---|
| 588 | databaseRequest->_loadedModel = 0; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | osg::ref_ptr<osg::Group> groupForAddingLoadedSubgraph = databaseRequest->_groupForAddingLoadedSubgraph.get(); |
|---|
| 592 | |
|---|
| 593 | if (!groupForAddingLoadedSubgraph) |
|---|
| 594 | { |
|---|
| 595 | osg::notify(osg::INFO)<<_name<<": Warning parent of loaded subgraph, deleted."<<std::endl; |
|---|
| 596 | databaseRequest->_loadedModel = 0; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | bool loadedObjectsNeedToBeCompiled = false; |
|---|
| 602 | |
|---|
| 603 | if (databaseRequest->_loadedModel.valid()) |
|---|
| 604 | { |
|---|
| 605 | databaseRequest->_loadedModel->getBound(); |
|---|
| 606 | |
|---|
| 607 | osg::NodePath nodePath; |
|---|
| 608 | osg::NodePathList nodePathList = groupForAddingLoadedSubgraph->getParentalNodePaths(); |
|---|
| 609 | if (!nodePathList.empty()) nodePath = nodePathList.front(); |
|---|
| 610 | nodePath.push_back(groupForAddingLoadedSubgraph.get()); |
|---|
| 611 | |
|---|
| 612 | #if 1 |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | ActiveGraphicsContexts::iterator itr = _pager->_activeGraphicsContexts.begin(); |
|---|
| 616 | |
|---|
| 617 | DataToCompile* dtc = 0; |
|---|
| 618 | if (itr != _pager->_activeGraphicsContexts.end()) |
|---|
| 619 | { |
|---|
| 620 | dtc = &(databaseRequest->_dataToCompileMap[*itr]); |
|---|
| 621 | ++itr; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | DatabasePager::FindCompileableGLObjectsVisitor frov(dtc, |
|---|
| 626 | _pager->_changeAutoUnRef, _pager->_valueAutoUnRef, |
|---|
| 627 | _pager->_changeAnisotropy, _pager->_valueAnisotropy, |
|---|
| 628 | _pager->_drawablePolicy, |
|---|
| 629 | _pager); |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | for(osg::NodePath::iterator nitr = nodePath.begin(); |
|---|
| 634 | nitr != nodePath.end(); |
|---|
| 635 | ++nitr) |
|---|
| 636 | { |
|---|
| 637 | frov.pushOntoNodePath(*nitr); |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | databaseRequest->_loadedModel->accept(frov); |
|---|
| 641 | |
|---|
| 642 | if (_pager->_doPreCompile && |
|---|
| 643 | !_pager->_activeGraphicsContexts.empty()) |
|---|
| 644 | { |
|---|
| 645 | if (!dtc->first.empty() || !dtc->second.empty()) |
|---|
| 646 | { |
|---|
| 647 | loadedObjectsNeedToBeCompiled = true; |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | for(; |
|---|
| 651 | itr != _pager->_activeGraphicsContexts.end(); |
|---|
| 652 | ++itr) |
|---|
| 653 | { |
|---|
| 654 | databaseRequest->_dataToCompileMap[*itr] = *dtc; |
|---|
| 655 | } |
|---|
| 656 | } |
|---|
| 657 | } |
|---|
| 658 | #else |
|---|
| 659 | if (_pager->_doPreCompile && |
|---|
| 660 | !_pager->_activeGraphicsContexts.empty()) |
|---|
| 661 | { |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | ActiveGraphicsContexts::iterator itr = _pager->_activeGraphicsContexts.begin(); |
|---|
| 665 | |
|---|
| 666 | DataToCompile& dtc = databaseRequest->_dataToCompileMap[*itr]; |
|---|
| 667 | ++itr; |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | DatabasePager::FindCompileableGLObjectsVisitor frov(&dtc, |
|---|
| 671 | _pager->_changeAutoUnRef, _pager->_valueAutoUnRef, |
|---|
| 672 | _pager->_changeAnisotropy, _pager->_valueAnisotropy, |
|---|
| 673 | _pager->_drawablePolicy, |
|---|
| 674 | _pager); |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | for(osg::NodePath::iterator nitr = nodePath.begin(); |
|---|
| 679 | nitr != nodePath.end(); |
|---|
| 680 | ++nitr) |
|---|
| 681 | { |
|---|
| 682 | frov.pushOntoNodePath(*nitr); |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | databaseRequest->_loadedModel->accept(frov); |
|---|
| 686 | |
|---|
| 687 | if (!dtc.first.empty() || !dtc.second.empty()) |
|---|
| 688 | { |
|---|
| 689 | loadedObjectsNeedToBeCompiled = true; |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | for(; |
|---|
| 693 | itr != _pager->_activeGraphicsContexts.end(); |
|---|
| 694 | ++itr) |
|---|
| 695 | { |
|---|
| 696 | databaseRequest->_dataToCompileMap[*itr] = dtc; |
|---|
| 697 | } |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | else |
|---|
| 701 | { |
|---|
| 702 | |
|---|
| 703 | if (osgDB::Registry::instance()->getBuildKdTreesHint()==osgDB::Options::BUILD_KDTREES && |
|---|
| 704 | osgDB::Registry::instance()->getKdTreeBuilder()) |
|---|
| 705 | { |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | osg::ref_ptr<osg::KdTreeBuilder> builder = osgDB::Registry::instance()->getKdTreeBuilder()->clone(); |
|---|
| 709 | |
|---|
| 710 | for(osg::NodePath::iterator nitr = nodePath.begin(); |
|---|
| 711 | nitr != nodePath.end(); |
|---|
| 712 | ++nitr) |
|---|
| 713 | { |
|---|
| 714 | builder->pushOntoNodePath(*nitr); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | databaseRequest->_loadedModel->accept(*builder); |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | } |
|---|
| 721 | } |
|---|
| 722 | #endif |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | if (loadedObjectsNeedToBeCompiled) |
|---|
| 727 | { |
|---|
| 728 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_pager->_dataToCompileList->_requestMutex); |
|---|
| 729 | databaseRequest->_requestQueue = _pager->_dataToCompileList.get(); |
|---|
| 730 | _pager->_dataToCompileList->_requestList.push_back(databaseRequest); |
|---|
| 731 | } |
|---|
| 732 | else |
|---|
| 733 | { |
|---|
| 734 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_pager->_dataToMergeList->_requestMutex); |
|---|
| 735 | databaseRequest->_requestQueue = _pager->_dataToMergeList.get(); |
|---|
| 736 | _pager->_dataToMergeList->_requestList.push_back(databaseRequest); |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | if (loadedObjectsNeedToBeCompiled) |
|---|
| 744 | { |
|---|
| 745 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_pager->_dataToCompileList->_requestMutex); |
|---|
| 746 | |
|---|
| 747 | _pager->_dataToCompileList->sort(); |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | RequestQueue::RequestList::iterator tooOld |
|---|
| 751 | = std::find_if(_pager->_dataToCompileList->_requestList.begin(), |
|---|
| 752 | _pager->_dataToCompileList->_requestList.end(), |
|---|
| 753 | refPtrAdapt(std::not1(std::bind2nd(std::mem_fun(&DatabaseRequest::isRequestCurrent), |
|---|
| 754 | _pager->_frameNumber)))); |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | for(RequestQueue::RequestList::iterator citr = tooOld; |
|---|
| 758 | citr != _pager->_dataToCompileList->_requestList.end(); |
|---|
| 759 | ++citr) |
|---|
| 760 | { |
|---|
| 761 | osg::notify(osg::INFO)<<_name<<": pruning from compile list"<<std::endl; |
|---|
| 762 | (*citr)->_loadedModel = 0; |
|---|
| 763 | (*citr)->_requestQueue = 0; |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | _pager->_dataToCompileList->_requestList.erase(tooOld, _pager->_dataToCompileList->_requestList.end()); |
|---|
| 767 | |
|---|
| 768 | loadedObjectsNeedToBeCompiled = !_pager->_dataToCompileList->_requestList.empty(); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | if (loadedObjectsNeedToBeCompiled && !_pager->_activeGraphicsContexts.empty()) |
|---|
| 772 | { |
|---|
| 773 | for(ActiveGraphicsContexts::iterator itr = _pager->_activeGraphicsContexts.begin(); |
|---|
| 774 | itr != _pager->_activeGraphicsContexts.end(); |
|---|
| 775 | ++itr) |
|---|
| 776 | { |
|---|
| 777 | osg::GraphicsContext* gc = osg::GraphicsContext::getCompileContext(*itr); |
|---|
| 778 | if (gc) |
|---|
| 779 | { |
|---|
| 780 | osg::GraphicsThread* gt = gc->getGraphicsThread(); |
|---|
| 781 | if (gt) |
|---|
| 782 | { |
|---|
| 783 | gt->add(new DatabasePager::CompileOperation(_pager)); |
|---|
| 784 | } |
|---|
| 785 | else |
|---|
| 786 | { |
|---|
| 787 | gc->makeCurrent(); |
|---|
| 788 | |
|---|
| 789 | _pager->compileAllGLObjects(*(gc->getState())); |
|---|
| 790 | |
|---|
| 791 | gc->releaseContext(); |
|---|
| 792 | } |
|---|
| 793 | } |
|---|
| 794 | } |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | } |
|---|
| 798 | } |
|---|
| 799 | else |
|---|
| 800 | { |
|---|
| 801 | OpenThreads::Thread::YieldCurrentThread(); |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | if (firstTime) |
|---|
| 808 | { |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | YieldCurrentThread(); |
|---|
| 812 | firstTime = false; |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | } while (!testCancel() && !_done); |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | DatabasePager::DatabasePager() |
|---|
| 820 | { |
|---|
| 821 | |
|---|
| 822 | |
|---|
| 823 | _startThreadCalled = false; |
|---|
| 824 | |
|---|
| 825 | _done = false; |
|---|
| 826 | _acceptNewRequests = true; |
|---|
| 827 | _databasePagerThreadPaused = false; |
|---|
| 828 | |
|---|
| 829 | _numFramesActive = 0; |
|---|
| 830 | _frameNumber = 0; |
|---|
| 831 | |
|---|
| 832 | const char* str = getenv("OSG_DATABASE_PAGER_PRIORITY"); |
|---|
| 833 | if (str) |
|---|
| 834 | { |
|---|
| 835 | if (strcmp(str,"DEFAULT")==0) |
|---|
| 836 | { |
|---|
| 837 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_DEFAULT); |
|---|
| 838 | } |
|---|
| 839 | else if (strcmp(str,"MIN")==0) |
|---|
| 840 | { |
|---|
| 841 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_MIN); |
|---|
| 842 | } |
|---|
| 843 | else if (strcmp(str,"LOW")==0) |
|---|
| 844 | { |
|---|
| 845 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_LOW); |
|---|
| 846 | } |
|---|
| 847 | else if (strcmp(str,"NOMINAL")==0) |
|---|
| 848 | { |
|---|
| 849 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_NOMINAL); |
|---|
| 850 | } |
|---|
| 851 | else if (strcmp(str,"HIGH")==0) |
|---|
| 852 | { |
|---|
| 853 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_HIGH); |
|---|
| 854 | } |
|---|
| 855 | else if (strcmp(str,"MAX")==0) |
|---|
| 856 | { |
|---|
| 857 | setSchedulePriority(OpenThreads::Thread::THREAD_PRIORITY_MAX); |
|---|
| 858 | } |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | #if __APPLE__ |
|---|
| 862 | |
|---|
| 863 | |
|---|
| 864 | _drawablePolicy = USE_VERTEX_ARRAYS; |
|---|
| 865 | #else |
|---|
| 866 | _drawablePolicy = DO_NOT_MODIFY_DRAWABLE_SETTINGS; |
|---|
| 867 | #endif |
|---|
| 868 | |
|---|
| 869 | str = getenv("OSG_DATABASE_PAGER_GEOMETRY"); |
|---|
| 870 | if (!str) str = getenv("OSG_DATABASE_PAGER_DRAWABLE"); |
|---|
| 871 | if (str) |
|---|
| 872 | { |
|---|
| 873 | if (strcmp(str,"DoNotModify")==0) |
|---|
| 874 | { |
|---|
| 875 | _drawablePolicy = DO_NOT_MODIFY_DRAWABLE_SETTINGS; |
|---|
| 876 | } |
|---|
| 877 | else if (strcmp(str,"DisplayList")==0 || strcmp(str,"DL")==0) |
|---|
| 878 | { |
|---|
| 879 | _drawablePolicy = USE_DISPLAY_LISTS; |
|---|
| 880 | } |
|---|
| 881 | else if (strcmp(str,"VBO")==0) |
|---|
| 882 | { |
|---|
| 883 | _drawablePolicy = USE_VERTEX_BUFFER_OBJECTS; |
|---|
| 884 | } |
|---|
| 885 | else if (strcmp(str,"VertexArrays")==0 || strcmp(str,"VA")==0 ) |
|---|
| 886 | { |
|---|
| 887 | _drawablePolicy = USE_VERTEX_ARRAYS; |
|---|
| 888 | } |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | _changeAutoUnRef = true; |
|---|
| 892 | _valueAutoUnRef = false; |
|---|
| 893 | |
|---|
| 894 | _changeAnisotropy = false; |
|---|
| 895 | _valueAnisotropy = 1.0f; |
|---|
| 896 | |
|---|
| 897 | const char* ptr=0; |
|---|
| 898 | |
|---|
| 899 | _deleteRemovedSubgraphsInDatabaseThread = true; |
|---|
| 900 | if( (ptr = getenv("OSG_DELETE_IN_DATABASE_THREAD")) != 0) |
|---|
| 901 | { |
|---|
| 902 | _deleteRemovedSubgraphsInDatabaseThread = strcmp(ptr,"yes")==0 || strcmp(ptr,"YES")==0 || |
|---|
| 903 | strcmp(ptr,"on")==0 || strcmp(ptr,"ON")==0; |
|---|
| 904 | |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | _expiryDelay = 10.0; |
|---|
| 908 | if( (ptr = getenv("OSG_EXPIRY_DELAY")) != 0) |
|---|
| 909 | { |
|---|
| 910 | _expiryDelay = osg::asciiToDouble(ptr); |
|---|
| 911 | osg::notify(osg::NOTICE)<<"Expiry delay = "<<_expiryDelay<<std::endl; |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | _expiryFrames = 1; |
|---|
| 915 | if( (ptr = getenv("OSG_EXPIRY_FRAMES")) != 0) |
|---|
| 916 | { |
|---|
| 917 | _expiryFrames = atoi(ptr); |
|---|
| 918 | osg::notify(osg::NOTICE)<<"Expiry frames = "<<_expiryFrames<<std::endl; |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | if( (ptr = getenv("OSG_RELEASE_DELAY")) != 0) |
|---|
| 922 | { |
|---|
| 923 | if (strcmp(ptr,"OFF")==0 || strcmp(ptr,"Off")==0 || strcmp(ptr,"off")==0) |
|---|
| 924 | { |
|---|
| 925 | setReleaseDelay(DBL_MAX); |
|---|
| 926 | } |
|---|
| 927 | else |
|---|
| 928 | { |
|---|
| 929 | setReleaseDelay(osg::asciiToDouble(ptr)); |
|---|
| 930 | } |
|---|
| 931 | |
|---|
| 932 | osg::notify(osg::NOTICE)<<"Release delay = "<<_releaseDelay<<std::endl; |
|---|
| 933 | } |
|---|
| 934 | else |
|---|
| 935 | { |
|---|
| 936 | setReleaseDelay(DBL_MAX); |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | _releaseFrames = 1; |
|---|
| 941 | if( (ptr = getenv("OSG_RELEASE_FRAMES")) != 0) |
|---|
| 942 | { |
|---|
| 943 | _releaseFrames = atoi(ptr); |
|---|
| 944 | osg::notify(osg::NOTICE)<<"Release frames = "<<_releaseFrames<<std::endl; |
|---|
| 945 | } |
|---|
| 946 | |
|---|
| 947 | |
|---|
| 948 | _targetMaximumNumberOfPageLOD = 300; |
|---|
| 949 | if( (ptr = getenv("OSG_MAX_PAGEDLOD")) != 0) |
|---|
| 950 | { |
|---|
| 951 | _targetMaximumNumberOfPageLOD = atoi(ptr); |
|---|
| 952 | osg::notify(osg::NOTICE)<<"_targetMaximumNumberOfPageLOD = "<<_targetMaximumNumberOfPageLOD<<std::endl; |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | _doPreCompile = false; |
|---|
| 957 | if( (ptr = getenv("OSG_DO_PRE_COMPILE")) != 0) |
|---|
| 958 | { |
|---|
| 959 | _doPreCompile = strcmp(ptr,"yes")==0 || strcmp(ptr,"YES")==0 || |
|---|
| 960 | strcmp(ptr,"on")==0 || strcmp(ptr,"ON")==0; |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | _targetFrameRate = 100.0; |
|---|
| 964 | _minimumTimeAvailableForGLCompileAndDeletePerFrame = 0.001; |
|---|
| 965 | _maximumNumOfObjectsToCompilePerFrame = 4; |
|---|
| 966 | if( (ptr = getenv("OSG_MINIMUM_COMPILE_TIME_PER_FRAME")) != 0) |
|---|
| 967 | { |
|---|
| 968 | _minimumTimeAvailableForGLCompileAndDeletePerFrame = osg::asciiToDouble(ptr); |
|---|
| 969 | } |
|---|
| 970 | |
|---|
| 971 | if( (ptr = getenv("OSG_MAXIMUM_OBJECTS_TO_COMPILE_PER_FRAME")) != 0) |
|---|
| 972 | { |
|---|
| 973 | _maximumNumOfObjectsToCompilePerFrame = atoi(ptr); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | resetStats(); |
|---|
| 978 | |
|---|
| 979 | |
|---|
| 980 | |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | |
|---|
| 985 | _fileRequestQueue = new ReadQueue(this,"fileRequestQueue"); |
|---|
| 986 | _httpRequestQueue = new ReadQueue(this,"httpRequestQueue"); |
|---|
| 987 | |
|---|
| 988 | _dataToCompileList = new RequestQueue; |
|---|
| 989 | _dataToMergeList = new RequestQueue; |
|---|
| 990 | |
|---|
| 991 | setUpThreads( |
|---|
| 992 | osg::DisplaySettings::instance()->getNumOfDatabaseThreadsHint(), |
|---|
| 993 | osg::DisplaySettings::instance()->getNumOfHttpDatabaseThreadsHint()); |
|---|
| 994 | |
|---|
| 995 | } |
|---|
| 996 | |
|---|
| 997 | DatabasePager::DatabasePager(const DatabasePager& rhs) |
|---|
| 998 | { |
|---|
| 999 | |
|---|
| 1000 | |
|---|
| 1001 | _startThreadCalled = false; |
|---|
| 1002 | |
|---|
| 1003 | _done = false; |
|---|
| 1004 | _acceptNewRequests = true; |
|---|
| 1005 | _databasePagerThreadPaused = false; |
|---|
| 1006 | |
|---|
| 1007 | _numFramesActive = 0; |
|---|
| 1008 | _frameNumber = 0; |
|---|
| 1009 | |
|---|
| 1010 | _drawablePolicy = rhs._drawablePolicy; |
|---|
| 1011 | |
|---|
| 1012 | _changeAutoUnRef = rhs._changeAutoUnRef; |
|---|
| 1013 | _valueAutoUnRef = rhs._valueAutoUnRef; |
|---|
| 1014 | _changeAnisotropy = rhs._changeAnisotropy; |
|---|
| 1015 | _valueAnisotropy = rhs._valueAnisotropy; |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | _deleteRemovedSubgraphsInDatabaseThread = rhs._deleteRemovedSubgraphsInDatabaseThread; |
|---|
| 1019 | |
|---|
| 1020 | _expiryDelay = rhs._expiryDelay; |
|---|
| 1021 | _expiryFrames = rhs._expiryFrames; |
|---|
| 1022 | |
|---|
| 1023 | _releaseDelay = rhs._releaseDelay; |
|---|
| 1024 | _releaseFrames = rhs._releaseFrames; |
|---|
| 1025 | |
|---|
| 1026 | _targetMaximumNumberOfPageLOD = rhs._targetMaximumNumberOfPageLOD; |
|---|
| 1027 | |
|---|
| 1028 | _doPreCompile = rhs._doPreCompile; |
|---|
| 1029 | _targetFrameRate = rhs._targetFrameRate; |
|---|
| 1030 | _minimumTimeAvailableForGLCompileAndDeletePerFrame = rhs._minimumTimeAvailableForGLCompileAndDeletePerFrame; |
|---|
| 1031 | _maximumNumOfObjectsToCompilePerFrame = rhs._maximumNumOfObjectsToCompilePerFrame; |
|---|
| 1032 | |
|---|
| 1033 | _fileRequestQueue = new ReadQueue(this,"fileRequestQueue"); |
|---|
| 1034 | _httpRequestQueue = new ReadQueue(this,"httpRequestQueue"); |
|---|
| 1035 | |
|---|
| 1036 | _dataToCompileList = new RequestQueue; |
|---|
| 1037 | _dataToMergeList = new RequestQueue; |
|---|
| 1038 | |
|---|
| 1039 | for(DatabaseThreadList::const_iterator dt_itr = rhs._databaseThreads.begin(); |
|---|
| 1040 | dt_itr != rhs._databaseThreads.end(); |
|---|
| 1041 | ++dt_itr) |
|---|
| 1042 | { |
|---|
| 1043 | _databaseThreads.push_back(new DatabaseThread(**dt_itr,this)); |
|---|
| 1044 | } |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | resetStats(); |
|---|
| 1048 | } |
|---|
| 1049 | |
|---|
| 1050 | |
|---|
| 1051 | DatabasePager::~DatabasePager() |
|---|
| 1052 | { |
|---|
| 1053 | cancel(); |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | osg::ref_ptr<DatabasePager>& DatabasePager::prototype() |
|---|
| 1057 | { |
|---|
| 1058 | static osg::ref_ptr<DatabasePager> s_DatabasePager = new DatabasePager; |
|---|
| 1059 | return s_DatabasePager; |
|---|
| 1060 | } |
|---|
| 1061 | |
|---|
| 1062 | DatabasePager* DatabasePager::create() |
|---|
| 1063 | { |
|---|
| 1064 | return DatabasePager::prototype().valid() ? |
|---|
| 1065 | DatabasePager::prototype()->clone() : |
|---|
| 1066 | new DatabasePager; |
|---|
| 1067 | } |
|---|
| 1068 | |
|---|
| 1069 | void DatabasePager::setUpThreads(unsigned int totalNumThreads, unsigned int numHttpThreads) |
|---|
| 1070 | { |
|---|
| 1071 | _databaseThreads.clear(); |
|---|
| 1072 | |
|---|
| 1073 | unsigned int numGeneralThreads = numHttpThreads < totalNumThreads ? |
|---|
| 1074 | totalNumThreads - numHttpThreads : |
|---|
| 1075 | 1; |
|---|
| 1076 | |
|---|
| 1077 | if (numHttpThreads==0) |
|---|
| 1078 | { |
|---|
| 1079 | for(unsigned int i=0; i<numGeneralThreads; ++i) |
|---|
| 1080 | { |
|---|
| 1081 | addDatabaseThread(DatabaseThread::HANDLE_ALL_REQUESTS,"HANDLE_ALL_REQUESTS"); |
|---|
| 1082 | } |
|---|
| 1083 | } |
|---|
| 1084 | else |
|---|
| 1085 | { |
|---|
| 1086 | for(unsigned int i=0; i<numGeneralThreads; ++i) |
|---|
| 1087 | { |
|---|
| 1088 | addDatabaseThread(DatabaseThread::HANDLE_NON_HTTP, "HANDLE_NON_HTTP"); |
|---|
| 1089 | } |
|---|
| 1090 | |
|---|
| 1091 | for(unsigned int i=0; i<numHttpThreads; ++i) |
|---|
| 1092 | { |
|---|
| 1093 | addDatabaseThread(DatabaseThread::HANDLE_ONLY_HTTP, "HANDLE_ONLY_HTTP"); |
|---|
| 1094 | } |
|---|
| 1095 | } |
|---|
| 1096 | } |
|---|
| 1097 | |
|---|
| 1098 | unsigned int DatabasePager::addDatabaseThread(DatabaseThread::Mode mode, const std::string& name) |
|---|
| 1099 | { |
|---|
| 1100 | osg::notify(osg::INFO)<<"DatabasePager::addDatabaseThread() "<<name<<std::endl; |
|---|
| 1101 | |
|---|
| 1102 | unsigned int pos = _databaseThreads.size(); |
|---|
| 1103 | |
|---|
| 1104 | DatabaseThread* thread = new DatabaseThread(this, mode,name); |
|---|
| 1105 | _databaseThreads.push_back(thread); |
|---|
| 1106 | |
|---|
| 1107 | if (_startThreadCalled) |
|---|
| 1108 | { |
|---|
| 1109 | osg::notify(osg::DEBUG_INFO)<<"DatabasePager::startThread()"<<std::endl; |
|---|
| 1110 | thread->startThread(); |
|---|
| 1111 | } |
|---|
| 1112 | |
|---|
| 1113 | return pos; |
|---|
| 1114 | } |
|---|
| 1115 | |
|---|
| 1116 | void DatabasePager::setReleaseDelay(double releaseDelay) |
|---|
| 1117 | { |
|---|
| 1118 | _releaseDelay = releaseDelay; |
|---|
| 1119 | |
|---|
| 1120 | if (_releaseDelay==DBL_MAX) |
|---|
| 1121 | { |
|---|
| 1122 | _changeAutoUnRef = true; |
|---|
| 1123 | _valueAutoUnRef = true; |
|---|
| 1124 | } |
|---|
| 1125 | else |
|---|
| 1126 | { |
|---|
| 1127 | |
|---|
| 1128 | _changeAutoUnRef = true; |
|---|
| 1129 | _valueAutoUnRef = false; |
|---|
| 1130 | } |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | int DatabasePager::setSchedulePriority(OpenThreads::Thread::ThreadPriority priority) |
|---|
| 1134 | { |
|---|
| 1135 | int result = 0; |
|---|
| 1136 | for(DatabaseThreadList::iterator dt_itr = _databaseThreads.begin(); |
|---|
| 1137 | dt_itr != _databaseThreads.end(); |
|---|
| 1138 | ++dt_itr) |
|---|
| 1139 | { |
|---|
| 1140 | result = (*dt_itr)->setSchedulePriority(priority); |
|---|
| 1141 | } |
|---|
| 1142 | return result; |
|---|
| 1143 | } |
|---|
| 1144 | |
|---|
| 1145 | bool DatabasePager::isRunning() const |
|---|
| 1146 | { |
|---|
| 1147 | for(DatabaseThreadList::const_iterator dt_itr = _databaseThreads.begin(); |
|---|
| 1148 | dt_itr != _databaseThreads.end(); |
|---|
| 1149 | ++dt_itr) |
|---|
| 1150 | { |
|---|
| 1151 | if ((*dt_itr)->isRunning()) return true; |
|---|
| 1152 | } |
|---|
| 1153 | |
|---|
| 1154 | return false; |
|---|
| 1155 | } |
|---|
| 1156 | |
|---|
| 1157 | int DatabasePager::cancel() |
|---|
| 1158 | { |
|---|
| 1159 | int result = 0; |
|---|
| 1160 | |
|---|
| 1161 | for(DatabaseThreadList::iterator dt_itr = _databaseThreads.begin(); |
|---|
| 1162 | dt_itr != _databaseThreads.end(); |
|---|
| 1163 | ++dt_itr) |
|---|
| 1164 | { |
|---|
| 1165 | (*dt_itr)->setDone(true); |
|---|
| 1166 | } |
|---|
| 1167 | |
|---|
| 1168 | |
|---|
| 1169 | _fileRequestQueue->release(); |
|---|
| 1170 | _httpRequestQueue->release(); |
|---|
| 1171 | |
|---|
| 1172 | for(DatabaseThreadList::iterator dt_itr = _databaseThreads.begin(); |
|---|
| 1173 | dt_itr != _databaseThreads.end(); |
|---|
| 1174 | ++dt_itr) |
|---|
| 1175 | { |
|---|
| 1176 | (*dt_itr)->cancel(); |
|---|
| 1177 | } |
|---|
| 1178 | |
|---|
| 1179 | _done = true; |
|---|
| 1180 | _startThreadCalled = false; |
|---|
| 1181 | |
|---|
| 1182 | |
|---|
| 1183 | return result; |
|---|
| 1184 | } |
|---|
| 1185 | |
|---|
| 1186 | void DatabasePager::clear() |
|---|
| 1187 | { |
|---|
| 1188 | _fileRequestQueue->clear(); |
|---|
| 1189 | _httpRequestQueue->clear(); |
|---|
| 1190 | |
|---|
| 1191 | { |
|---|
| 1192 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToCompileList->_requestMutex); |
|---|
| 1193 | for(RequestQueue::RequestList::iterator citr = _dataToCompileList->_requestList.begin(); |
|---|
| 1194 | citr != _dataToCompileList->_requestList.end(); |
|---|
| 1195 | ++citr) |
|---|
| 1196 | { |
|---|
| 1197 | (*citr)->_loadedModel = 0; |
|---|
| 1198 | (*citr)->_requestQueue = 0; |
|---|
| 1199 | } |
|---|
| 1200 | _dataToCompileList->_requestList.clear(); |
|---|
| 1201 | } |
|---|
| 1202 | |
|---|
| 1203 | { |
|---|
| 1204 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToMergeList->_requestMutex); |
|---|
| 1205 | for(RequestQueue::RequestList::iterator citr = _dataToMergeList->_requestList.begin(); |
|---|
| 1206 | citr != _dataToMergeList->_requestList.end(); |
|---|
| 1207 | ++citr) |
|---|
| 1208 | { |
|---|
| 1209 | (*citr)->_loadedModel = 0; |
|---|
| 1210 | (*citr)->_requestQueue = 0; |
|---|
| 1211 | } |
|---|
| 1212 | _dataToMergeList->_requestList.clear(); |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | |
|---|
| 1216 | _activePagedLODList.clear(); |
|---|
| 1217 | _inactivePagedLODList.clear(); |
|---|
| 1218 | |
|---|
| 1219 | |
|---|
| 1220 | |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | void DatabasePager::resetStats() |
|---|
| 1224 | { |
|---|
| 1225 | |
|---|
| 1226 | _minimumTimeToMergeTile = DBL_MAX; |
|---|
| 1227 | _maximumTimeToMergeTile = -DBL_MAX; |
|---|
| 1228 | _totalTimeToMergeTiles = 0.0; |
|---|
| 1229 | _numTilesMerges = 0; |
|---|
| 1230 | } |
|---|
| 1231 | |
|---|
| 1232 | bool DatabasePager::getRequestsInProgress() const |
|---|
| 1233 | { |
|---|
| 1234 | if (getFileRequestListSize()>0) return true; |
|---|
| 1235 | |
|---|
| 1236 | if (getDataToCompileListSize()>0) |
|---|
| 1237 | { |
|---|
| 1238 | return true; |
|---|
| 1239 | } |
|---|
| 1240 | |
|---|
| 1241 | if (getDataToMergeListSize()>0) return true; |
|---|
| 1242 | |
|---|
| 1243 | for(DatabaseThreadList::const_iterator itr = _databaseThreads.begin(); |
|---|
| 1244 | itr != _databaseThreads.end(); |
|---|
| 1245 | ++itr) |
|---|
| 1246 | { |
|---|
| 1247 | if ((*itr)->getActive()) return true; |
|---|
| 1248 | } |
|---|
| 1249 | return false; |
|---|
| 1250 | } |
|---|
| 1251 | |
|---|
| 1252 | |
|---|
| 1253 | void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* group, |
|---|
| 1254 | float priority, const osg::FrameStamp* framestamp, |
|---|
| 1255 | osg::ref_ptr<osg::Referenced>& databaseRequestRef, |
|---|
| 1256 | const osg::Referenced* options) |
|---|
| 1257 | { |
|---|
| 1258 | osgDB::Options* loadOptions = dynamic_cast<osgDB::Options*>(const_cast<osg::Referenced*>(options)); |
|---|
| 1259 | if (!loadOptions) |
|---|
| 1260 | { |
|---|
| 1261 | loadOptions = Registry::instance()->getOptions(); |
|---|
| 1262 | |
|---|
| 1263 | |
|---|
| 1264 | } |
|---|
| 1265 | else |
|---|
| 1266 | { |
|---|
| 1267 | |
|---|
| 1268 | } |
|---|
| 1269 | |
|---|
| 1270 | |
|---|
| 1271 | if (!_acceptNewRequests) return; |
|---|
| 1272 | |
|---|
| 1273 | |
|---|
| 1274 | double timestamp = framestamp?framestamp->getReferenceTime():0.0; |
|---|
| 1275 | int frameNumber = framestamp?framestamp->getFrameNumber():_frameNumber; |
|---|
| 1276 | |
|---|
| 1277 | |
|---|
| 1278 | #ifdef WITH_REQUESTNODEFILE_TIMING |
|---|
| 1279 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 1280 | static int previousFrame = -1; |
|---|
| 1281 | static double totalTime = 0.0; |
|---|
| 1282 | |
|---|
| 1283 | if (previousFrame!=frameNumber) |
|---|
| 1284 | { |
|---|
| 1285 | osg::notify(osg::NOTICE)<<"requestNodeFiles for "<<previousFrame<<" time = "<<totalTime<<std::endl; |
|---|
| 1286 | |
|---|
| 1287 | previousFrame = frameNumber; |
|---|
| 1288 | totalTime = 0.0; |
|---|
| 1289 | } |
|---|
| 1290 | #endif |
|---|
| 1291 | |
|---|
| 1292 | |
|---|
| 1293 | bool foundEntry = false; |
|---|
| 1294 | |
|---|
| 1295 | if (databaseRequestRef.valid()) |
|---|
| 1296 | { |
|---|
| 1297 | DatabaseRequest* databaseRequest = dynamic_cast<DatabaseRequest*>(databaseRequestRef.get()); |
|---|
| 1298 | if (databaseRequest) |
|---|
| 1299 | { |
|---|
| 1300 | osg::notify(osg::INFO)<<"DatabasePager::requestNodeFile("<<fileName<<") updating already assigned."<<std::endl; |
|---|
| 1301 | |
|---|
| 1302 | RequestQueue* requestQueue = databaseRequest->_requestQueue; |
|---|
| 1303 | if (requestQueue) |
|---|
| 1304 | { |
|---|
| 1305 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(requestQueue->_requestMutex); |
|---|
| 1306 | |
|---|
| 1307 | databaseRequest->_frameNumberLastRequest = frameNumber; |
|---|
| 1308 | databaseRequest->_timestampLastRequest = timestamp; |
|---|
| 1309 | databaseRequest->_priorityLastRequest = priority; |
|---|
| 1310 | ++(databaseRequest->_numOfRequests); |
|---|
| 1311 | } |
|---|
| 1312 | else |
|---|
| 1313 | { |
|---|
| 1314 | databaseRequest->_frameNumberLastRequest = frameNumber; |
|---|
| 1315 | databaseRequest->_timestampLastRequest = timestamp; |
|---|
| 1316 | databaseRequest->_priorityLastRequest = priority; |
|---|
| 1317 | ++(databaseRequest->_numOfRequests); |
|---|
| 1318 | } |
|---|
| 1319 | |
|---|
| 1320 | foundEntry = true; |
|---|
| 1321 | |
|---|
| 1322 | if (databaseRequestRef->referenceCount()==1) |
|---|
| 1323 | { |
|---|
| 1324 | osg::notify(osg::INFO)<<"DatabasePager::requestNodeFile("<<fileName<<") orphaned, resubmitting."<<std::endl; |
|---|
| 1325 | |
|---|
| 1326 | databaseRequest->_frameNumberFirstRequest = frameNumber; |
|---|
| 1327 | databaseRequest->_timestampFirstRequest = timestamp; |
|---|
| 1328 | databaseRequest->_priorityFirstRequest = priority; |
|---|
| 1329 | databaseRequest->_frameNumberLastRequest = frameNumber; |
|---|
| 1330 | databaseRequest->_timestampLastRequest = timestamp; |
|---|
| 1331 | databaseRequest->_priorityLastRequest = priority; |
|---|
| 1332 | databaseRequest->_groupForAddingLoadedSubgraph = group; |
|---|
| 1333 | databaseRequest->_loadOptions = loadOptions; |
|---|
| 1334 | databaseRequest->_requestQueue = _fileRequestQueue.get(); |
|---|
| 1335 | |
|---|
| 1336 | _fileRequestQueue->add(databaseRequest); |
|---|
| 1337 | } |
|---|
| 1338 | |
|---|
| 1339 | } |
|---|
| 1340 | } |
|---|
| 1341 | |
|---|
| 1342 | if (!foundEntry) |
|---|
| 1343 | { |
|---|
| 1344 | osg::notify(osg::INFO)<<"In DatabasePager::requestNodeFile("<<fileName<<")"<<std::endl; |
|---|
| 1345 | |
|---|
| 1346 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_fileRequestQueue->_requestMutex); |
|---|
| 1347 | |
|---|
| 1348 | if (!databaseRequestRef.valid() || databaseRequestRef->referenceCount()==1) |
|---|
| 1349 | { |
|---|
| 1350 | osg::ref_ptr<DatabaseRequest> databaseRequest = new DatabaseRequest; |
|---|
| 1351 | |
|---|
| 1352 | databaseRequestRef = databaseRequest.get(); |
|---|
| 1353 | |
|---|
| 1354 | databaseRequest->_fileName = fileName; |
|---|
| 1355 | databaseRequest->_frameNumberFirstRequest = frameNumber; |
|---|
| 1356 | databaseRequest->_timestampFirstRequest = timestamp; |
|---|
| 1357 | databaseRequest->_priorityFirstRequest = priority; |
|---|
| 1358 | databaseRequest->_frameNumberLastRequest = frameNumber; |
|---|
| 1359 | databaseRequest->_timestampLastRequest = timestamp; |
|---|
| 1360 | databaseRequest->_priorityLastRequest = priority; |
|---|
| 1361 | databaseRequest->_groupForAddingLoadedSubgraph = group; |
|---|
| 1362 | databaseRequest->_loadOptions = loadOptions; |
|---|
| 1363 | databaseRequest->_requestQueue = _fileRequestQueue.get(); |
|---|
| 1364 | |
|---|
| 1365 | _fileRequestQueue->_requestList.push_back(databaseRequest.get()); |
|---|
| 1366 | |
|---|
| 1367 | _fileRequestQueue->updateBlock(); |
|---|
| 1368 | } |
|---|
| 1369 | |
|---|
| 1370 | } |
|---|
| 1371 | |
|---|
| 1372 | if (!_startThreadCalled) |
|---|
| 1373 | { |
|---|
| 1374 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_run_mutex); |
|---|
| 1375 | |
|---|
| 1376 | if (!_startThreadCalled) |
|---|
| 1377 | { |
|---|
| 1378 | _startThreadCalled = true; |
|---|
| 1379 | _done = false; |
|---|
| 1380 | osg::notify(osg::DEBUG_INFO)<<"DatabasePager::startThread()"<<std::endl; |
|---|
| 1381 | |
|---|
| 1382 | if (_databaseThreads.empty()) |
|---|
| 1383 | { |
|---|
| 1384 | setUpThreads( |
|---|
| 1385 | osg::DisplaySettings::instance()->getNumOfDatabaseThreadsHint(), |
|---|
| 1386 | osg::DisplaySettings::instance()->getNumOfHttpDatabaseThreadsHint()); |
|---|
| 1387 | } |
|---|
| 1388 | |
|---|
| 1389 | for(DatabaseThreadList::const_iterator dt_itr = _databaseThreads.begin(); |
|---|
| 1390 | dt_itr != _databaseThreads.end(); |
|---|
| 1391 | ++dt_itr) |
|---|
| 1392 | { |
|---|
| 1393 | (*dt_itr)->startThread(); |
|---|
| 1394 | } |
|---|
| 1395 | } |
|---|
| 1396 | } |
|---|
| 1397 | |
|---|
| 1398 | #ifdef WITH_REQUESTNODEFILE_TIMING |
|---|
| 1399 | totalTime += osg::Timer::instance()->delta_m(start_tick, osg::Timer::instance()->tick()); |
|---|
| 1400 | #endif |
|---|
| 1401 | } |
|---|
| 1402 | |
|---|
| 1403 | void DatabasePager::signalBeginFrame(const osg::FrameStamp* framestamp) |
|---|
| 1404 | { |
|---|
| 1405 | if (framestamp) |
|---|
| 1406 | { |
|---|
| 1407 | |
|---|
| 1408 | _frameNumber = framestamp->getFrameNumber(); |
|---|
| 1409 | |
|---|
| 1410 | } |
|---|
| 1411 | } |
|---|
| 1412 | |
|---|
| 1413 | void DatabasePager::signalEndFrame() |
|---|
| 1414 | { |
|---|
| 1415 | |
|---|
| 1416 | } |
|---|
| 1417 | |
|---|
| 1418 | void DatabasePager::setDatabasePagerThreadPause(bool pause) |
|---|
| 1419 | { |
|---|
| 1420 | if (_databasePagerThreadPaused == pause) return; |
|---|
| 1421 | |
|---|
| 1422 | _databasePagerThreadPaused = pause; |
|---|
| 1423 | _fileRequestQueue->updateBlock(); |
|---|
| 1424 | _httpRequestQueue->updateBlock(); |
|---|
| 1425 | } |
|---|
| 1426 | |
|---|
| 1427 | |
|---|
| 1428 | bool DatabasePager::requiresUpdateSceneGraph() const |
|---|
| 1429 | { |
|---|
| 1430 | { |
|---|
| 1431 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToMergeList->_requestMutex); |
|---|
| 1432 | if (!_dataToMergeList->_requestList.empty()) return true; |
|---|
| 1433 | } |
|---|
| 1434 | |
|---|
| 1435 | return false; |
|---|
| 1436 | |
|---|
| 1437 | } |
|---|
| 1438 | |
|---|
| 1439 | |
|---|
| 1440 | void DatabasePager::addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp) |
|---|
| 1441 | { |
|---|
| 1442 | double timeStamp = frameStamp.getReferenceTime(); |
|---|
| 1443 | int frameNumber = frameStamp.getFrameNumber(); |
|---|
| 1444 | |
|---|
| 1445 | osg::Timer_t before = osg::Timer::instance()->tick(); |
|---|
| 1446 | |
|---|
| 1447 | RequestQueue::RequestList localFileLoadedList; |
|---|
| 1448 | |
|---|
| 1449 | |
|---|
| 1450 | { |
|---|
| 1451 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToMergeList->_requestMutex); |
|---|
| 1452 | localFileLoadedList.swap(_dataToMergeList->_requestList); |
|---|
| 1453 | } |
|---|
| 1454 | |
|---|
| 1455 | osg::Timer_t mid = osg::Timer::instance()->tick(); |
|---|
| 1456 | |
|---|
| 1457 | |
|---|
| 1458 | for(RequestQueue::RequestList::iterator itr=localFileLoadedList.begin(); |
|---|
| 1459 | itr!=localFileLoadedList.end(); |
|---|
| 1460 | ++itr) |
|---|
| 1461 | { |
|---|
| 1462 | DatabaseRequest* databaseRequest = itr->get(); |
|---|
| 1463 | |
|---|
| 1464 | |
|---|
| 1465 | |
|---|
| 1466 | if (osgDB::Registry::instance()->getSharedStateManager()) |
|---|
| 1467 | osgDB::Registry::instance()->getSharedStateManager()->share(databaseRequest->_loadedModel.get()); |
|---|
| 1468 | |
|---|
| 1469 | |
|---|
| 1470 | registerPagedLODs(databaseRequest->_loadedModel.get(), frameStamp.getFrameNumber()); |
|---|
| 1471 | |
|---|
| 1472 | osg::ref_ptr<osg::Group> group = databaseRequest->_groupForAddingLoadedSubgraph.get(); |
|---|
| 1473 | if (group.valid()) |
|---|
| 1474 | { |
|---|
| 1475 | osg::PagedLOD* plod = dynamic_cast<osg::PagedLOD*>(group.get()); |
|---|
| 1476 | if (plod) |
|---|
| 1477 | { |
|---|
| 1478 | plod->setTimeStamp(plod->getNumChildren(), timeStamp); |
|---|
| 1479 | plod->setFrameNumber(plod->getNumChildren(), frameNumber); |
|---|
| 1480 | plod->getDatabaseRequest(plod->getNumChildren()) = 0; |
|---|
| 1481 | } |
|---|
| 1482 | else |
|---|
| 1483 | { |
|---|
| 1484 | osg::ProxyNode* proxyNode = dynamic_cast<osg::ProxyNode*>(group.get()); |
|---|
| 1485 | if (proxyNode) |
|---|
| 1486 | { |
|---|
| 1487 | proxyNode->getDatabaseRequest(proxyNode->getNumChildren()) = 0; |
|---|
| 1488 | } |
|---|
| 1489 | } |
|---|
| 1490 | |
|---|
| 1491 | group->addChild(databaseRequest->_loadedModel.get()); |
|---|
| 1492 | |
|---|
| 1493 | |
|---|
| 1494 | |
|---|
| 1495 | double timeToMerge = timeStamp-databaseRequest->_timestampFirstRequest; |
|---|
| 1496 | |
|---|
| 1497 | if (timeToMerge<_minimumTimeToMergeTile) _minimumTimeToMergeTile = timeToMerge; |
|---|
| 1498 | if (timeToMerge>_maximumTimeToMergeTile) _maximumTimeToMergeTile = timeToMerge; |
|---|
| 1499 | |
|---|
| 1500 | _totalTimeToMergeTiles += timeToMerge; |
|---|
| 1501 | ++_numTilesMerges; |
|---|
| 1502 | } |
|---|
| 1503 | |
|---|
| 1504 | |
|---|
| 1505 | databaseRequest->_loadedModel = 0; |
|---|
| 1506 | |
|---|
| 1507 | |
|---|
| 1508 | } |
|---|
| 1509 | |
|---|
| 1510 | osg::Timer_t last = osg::Timer::instance()->tick(); |
|---|
| 1511 | |
|---|
| 1512 | if (!localFileLoadedList.empty()) |
|---|
| 1513 | { |
|---|
| 1514 | osg::notify(osg::DEBUG_INFO)<<"Done DatabasePager::addLoadedDataToSceneGraph"<< |
|---|
| 1515 | osg::Timer::instance()->delta_m(before,mid)<<"ms,\t"<< |
|---|
| 1516 | osg::Timer::instance()->delta_m(mid,last)<<"ms"<< |
|---|
| 1517 | " objects"<<localFileLoadedList.size()<<std::endl<<std::endl; |
|---|
| 1518 | } |
|---|
| 1519 | } |
|---|
| 1520 | |
|---|
| 1521 | class DatabasePager::MarkPagedLODsVisitor : public osg::NodeVisitor |
|---|
| 1522 | { |
|---|
| 1523 | public: |
|---|
| 1524 | MarkPagedLODsVisitor(const std::string& marker): |
|---|
| 1525 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 1526 | _marker(marker), |
|---|
| 1527 | _numPagedLODsMarked(0) |
|---|
| 1528 | { |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | META_NodeVisitor("osgDB","MarkPagedLODsVisitor") |
|---|
| 1532 | |
|---|
| 1533 | virtual void apply(osg::PagedLOD& plod) |
|---|
| 1534 | { |
|---|
| 1535 | if (plod.getName()!=_marker) |
|---|
| 1536 | { |
|---|
| 1537 | ++_numPagedLODsMarked; |
|---|
| 1538 | plod.setName(_marker); |
|---|
| 1539 | |
|---|
| 1540 | traverse(plod); |
|---|
| 1541 | } |
|---|
| 1542 | } |
|---|
| 1543 | |
|---|
| 1544 | std::string _marker; |
|---|
| 1545 | int _numPagedLODsMarked; |
|---|
| 1546 | }; |
|---|
| 1547 | |
|---|
| 1548 | void DatabasePager::removeExpiredSubgraphs(const osg::FrameStamp& frameStamp) |
|---|
| 1549 | { |
|---|
| 1550 | if (_targetMaximumNumberOfPageLOD>0) |
|---|
| 1551 | { |
|---|
| 1552 | capped_removeExpiredSubgraphs(frameStamp); |
|---|
| 1553 | } |
|---|
| 1554 | else |
|---|
| 1555 | { |
|---|
| 1556 | expiry_removeExpiredSubgraphs(frameStamp); |
|---|
| 1557 | } |
|---|
| 1558 | } |
|---|
| 1559 | |
|---|
| 1560 | void DatabasePager::capped_removeExpiredSubgraphs(const osg::FrameStamp& frameStamp) |
|---|
| 1561 | { |
|---|
| 1562 | static double s_total_iter_stage_a = 0.0; |
|---|
| 1563 | static double s_total_time_stage_a = 0.0; |
|---|
| 1564 | static double s_total_max_stage_a = 0.0; |
|---|
| 1565 | |
|---|
| 1566 | static double s_total_iter_stage_b = 0.0; |
|---|
| 1567 | static double s_total_time_stage_b = 0.0; |
|---|
| 1568 | static double s_total_max_stage_b = 0.0; |
|---|
| 1569 | |
|---|
| 1570 | static double s_total_iter_stage_c = 0.0; |
|---|
| 1571 | static double s_total_time_stage_c = 0.0; |
|---|
| 1572 | static double s_total_max_stage_c = 0.0; |
|---|
| 1573 | |
|---|
| 1574 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 1575 | |
|---|
| 1576 | |
|---|
| 1577 | unsigned int numPagedLODs = _activePagedLODList.size() + _inactivePagedLODList.size(); |
|---|
| 1578 | |
|---|
| 1579 | |
|---|
| 1580 | PagedLODList::iterator itr = _activePagedLODList.begin(); |
|---|
| 1581 | for(PagedLODList::iterator itr = _activePagedLODList.begin(); |
|---|
| 1582 | itr != _activePagedLODList.end(); |
|---|
| 1583 | ) |
|---|
| 1584 | { |
|---|
| 1585 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1586 | |
|---|
| 1587 | int delta = frameStamp.getFrameNumber() - plod->getFrameNumberOfLastTraversal(); |
|---|
| 1588 | if (delta>1) |
|---|
| 1589 | { |
|---|
| 1590 | if (_releaseDelay!=DBL_MAX) |
|---|
| 1591 | { |
|---|
| 1592 | plod->releaseGLObjects(); |
|---|
| 1593 | osg::notify(osg::INFO)<<"DatabasePager::removeExpiredSubgraphs(), releasing gl objects"<<std::endl; |
|---|
| 1594 | } |
|---|
| 1595 | |
|---|
| 1596 | _inactivePagedLODList.push_back(plod); |
|---|
| 1597 | |
|---|
| 1598 | itr = _activePagedLODList.erase(itr); |
|---|
| 1599 | } |
|---|
| 1600 | else |
|---|
| 1601 | { |
|---|
| 1602 | ++itr; |
|---|
| 1603 | } |
|---|
| 1604 | } |
|---|
| 1605 | |
|---|
| 1606 | for(PagedLODList::iterator itr = _inactivePagedLODList.begin(); |
|---|
| 1607 | itr != _inactivePagedLODList.end(); |
|---|
| 1608 | ) |
|---|
| 1609 | { |
|---|
| 1610 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1611 | |
|---|
| 1612 | int delta = frameStamp.getFrameNumber() - plod->getFrameNumberOfLastTraversal(); |
|---|
| 1613 | if (delta>1) |
|---|
| 1614 | { |
|---|
| 1615 | ++itr; |
|---|
| 1616 | } |
|---|
| 1617 | else |
|---|
| 1618 | { |
|---|
| 1619 | _activePagedLODList.push_back(plod); |
|---|
| 1620 | |
|---|
| 1621 | itr = _inactivePagedLODList.erase(itr); |
|---|
| 1622 | } |
|---|
| 1623 | } |
|---|
| 1624 | |
|---|
| 1625 | int inactivePLOD = _inactivePagedLODList.size(); |
|---|
| 1626 | |
|---|
| 1627 | |
|---|
| 1628 | osg::Timer_t end_a_Tick = osg::Timer::instance()->tick(); |
|---|
| 1629 | double time_a = osg::Timer::instance()->delta_m(startTick,end_a_Tick); |
|---|
| 1630 | |
|---|
| 1631 | s_total_iter_stage_a += 1.0; |
|---|
| 1632 | s_total_time_stage_a += time_a; |
|---|
| 1633 | if (s_total_max_stage_a<time_a) s_total_max_stage_a = time_a; |
|---|
| 1634 | |
|---|
| 1635 | |
|---|
| 1636 | if (numPagedLODs <= _targetMaximumNumberOfPageLOD) |
|---|
| 1637 | { |
|---|
| 1638 | |
|---|
| 1639 | return; |
|---|
| 1640 | } |
|---|
| 1641 | |
|---|
| 1642 | int numToPrune = numPagedLODs - _targetMaximumNumberOfPageLOD; |
|---|
| 1643 | if (numToPrune > inactivePLOD) |
|---|
| 1644 | { |
|---|
| 1645 | numToPrune = inactivePLOD; |
|---|
| 1646 | } |
|---|
| 1647 | |
|---|
| 1648 | |
|---|
| 1649 | osg::NodeList childrenRemoved; |
|---|
| 1650 | |
|---|
| 1651 | double expiryTime = frameStamp.getReferenceTime() - 0.1; |
|---|
| 1652 | int expiryFrame = frameStamp.getFrameNumber() - 1; |
|---|
| 1653 | |
|---|
| 1654 | MarkPagedLODsVisitor markerVistor("NeedToRemove"); |
|---|
| 1655 | |
|---|
| 1656 | for(PagedLODList::iterator itr = _inactivePagedLODList.begin(); |
|---|
| 1657 | itr!=_inactivePagedLODList.end() && markerVistor._numPagedLODsMarked<numToPrune; |
|---|
| 1658 | ++itr) |
|---|
| 1659 | { |
|---|
| 1660 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1661 | |
|---|
| 1662 | osg::NodeList localChildrenRemoved; |
|---|
| 1663 | plod->removeExpiredChildren(expiryTime, expiryFrame, localChildrenRemoved); |
|---|
| 1664 | if (!localChildrenRemoved.empty()) |
|---|
| 1665 | { |
|---|
| 1666 | for(osg::NodeList::iterator critr = localChildrenRemoved.begin(); |
|---|
| 1667 | critr!=localChildrenRemoved.end(); |
|---|
| 1668 | ++critr) |
|---|
| 1669 | { |
|---|
| 1670 | (*critr)->accept(markerVistor); |
|---|
| 1671 | } |
|---|
| 1672 | |
|---|
| 1673 | std::copy(localChildrenRemoved.begin(),localChildrenRemoved.end(),std::back_inserter(childrenRemoved)); |
|---|
| 1674 | } |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | for(PagedLODList::iterator itr = _activePagedLODList.begin(); |
|---|
| 1678 | itr!=_activePagedLODList.end() && markerVistor._numPagedLODsMarked<numToPrune; |
|---|
| 1679 | ++itr) |
|---|
| 1680 | { |
|---|
| 1681 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1682 | |
|---|
| 1683 | osg::NodeList localChildrenRemoved; |
|---|
| 1684 | plod->removeExpiredChildren(expiryTime, expiryFrame, localChildrenRemoved); |
|---|
| 1685 | if (!localChildrenRemoved.empty()) |
|---|
| 1686 | { |
|---|
| 1687 | for(osg::NodeList::iterator critr = localChildrenRemoved.begin(); |
|---|
| 1688 | critr!=localChildrenRemoved.end(); |
|---|
| 1689 | ++critr) |
|---|
| 1690 | { |
|---|
| 1691 | (*critr)->accept(markerVistor); |
|---|
| 1692 | } |
|---|
| 1693 | |
|---|
| 1694 | std::copy(localChildrenRemoved.begin(),localChildrenRemoved.end(),std::back_inserter(childrenRemoved)); |
|---|
| 1695 | } |
|---|
| 1696 | } |
|---|
| 1697 | |
|---|
| 1698 | osg::Timer_t end_b_Tick = osg::Timer::instance()->tick(); |
|---|
| 1699 | double time_b = osg::Timer::instance()->delta_m(end_a_Tick,end_b_Tick); |
|---|
| 1700 | |
|---|
| 1701 | s_total_iter_stage_b += 1.0; |
|---|
| 1702 | s_total_time_stage_b += time_b; |
|---|
| 1703 | if (s_total_max_stage_b<time_b) s_total_max_stage_b = time_b; |
|---|
| 1704 | |
|---|
| 1705 | |
|---|
| 1706 | |
|---|
| 1707 | |
|---|
| 1708 | |
|---|
| 1709 | if (!childrenRemoved.empty()) |
|---|
| 1710 | { |
|---|
| 1711 | bool updateBlock = false; |
|---|
| 1712 | |
|---|
| 1713 | |
|---|
| 1714 | if (_deleteRemovedSubgraphsInDatabaseThread) |
|---|
| 1715 | { |
|---|
| 1716 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_fileRequestQueue->_childrenToDeleteListMutex); |
|---|
| 1717 | for (osg::NodeList::iterator critr = childrenRemoved.begin(); |
|---|
| 1718 | critr!=childrenRemoved.end(); |
|---|
| 1719 | ++critr) |
|---|
| 1720 | { |
|---|
| 1721 | _fileRequestQueue->_childrenToDeleteList.push_back(critr->get()); |
|---|
| 1722 | } |
|---|
| 1723 | |
|---|
| 1724 | updateBlock = true; |
|---|
| 1725 | |
|---|
| 1726 | } |
|---|
| 1727 | |
|---|
| 1728 | int numRemoved = 0; |
|---|
| 1729 | int numToRemove = markerVistor._numPagedLODsMarked; |
|---|
| 1730 | |
|---|
| 1731 | |
|---|
| 1732 | |
|---|
| 1733 | |
|---|
| 1734 | |
|---|
| 1735 | int numSkipped = 0; |
|---|
| 1736 | for(PagedLODList::iterator itr = _inactivePagedLODList.begin(); |
|---|
| 1737 | itr!=_inactivePagedLODList.end() && numRemoved<numToRemove; |
|---|
| 1738 | ) |
|---|
| 1739 | { |
|---|
| 1740 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1741 | if (plod && plod->getName() != markerVistor._marker) |
|---|
| 1742 | { |
|---|
| 1743 | ++itr; |
|---|
| 1744 | |
|---|
| 1745 | ++numSkipped; |
|---|
| 1746 | |
|---|
| 1747 | |
|---|
| 1748 | } |
|---|
| 1749 | else |
|---|
| 1750 | { |
|---|
| 1751 | |
|---|
| 1752 | |
|---|
| 1753 | ++numRemoved; |
|---|
| 1754 | |
|---|
| 1755 | itr = _inactivePagedLODList.erase(itr); |
|---|
| 1756 | } |
|---|
| 1757 | } |
|---|
| 1758 | |
|---|
| 1759 | osg::notify(osg::INFO)<<"Number of PagedLODs skipped="<<numSkipped<<" removed "<<numRemoved<<std::endl; |
|---|
| 1760 | |
|---|
| 1761 | |
|---|
| 1762 | childrenRemoved.clear(); |
|---|
| 1763 | |
|---|
| 1764 | |
|---|
| 1765 | if (updateBlock) |
|---|
| 1766 | { |
|---|
| 1767 | _fileRequestQueue->updateBlock(); |
|---|
| 1768 | } |
|---|
| 1769 | } |
|---|
| 1770 | |
|---|
| 1771 | osg::Timer_t end_c_Tick = osg::Timer::instance()->tick(); |
|---|
| 1772 | double time_c = osg::Timer::instance()->delta_m(end_b_Tick,end_c_Tick); |
|---|
| 1773 | |
|---|
| 1774 | s_total_iter_stage_c += 1.0; |
|---|
| 1775 | s_total_time_stage_c += time_c; |
|---|
| 1776 | if (s_total_max_stage_c<time_c) s_total_max_stage_c = time_c; |
|---|
| 1777 | |
|---|
| 1778 | |
|---|
| 1779 | |
|---|
| 1780 | osg::notify(osg::INFO)<<"active="<<_activePagedLODList.size()<<" inactive="<<_inactivePagedLODList.size()<<" overall = "<<osg::Timer::instance()->delta_m(startTick,end_c_Tick)<< |
|---|
| 1781 | " A="<<time_a<<" avg="<<s_total_time_stage_a/s_total_iter_stage_a<<" max = "<<s_total_max_stage_a<< |
|---|
| 1782 | " B="<<time_b<<" avg="<<s_total_time_stage_b/s_total_iter_stage_b<<" max = "<<s_total_max_stage_b<< |
|---|
| 1783 | " C="<<time_c<<" avg="<<s_total_time_stage_c/s_total_iter_stage_c<<" max = "<<s_total_max_stage_c<<std::endl; |
|---|
| 1784 | |
|---|
| 1785 | |
|---|
| 1786 | if (osgDB::Registry::instance()->getSharedStateManager()) |
|---|
| 1787 | osgDB::Registry::instance()->getSharedStateManager()->prune(); |
|---|
| 1788 | |
|---|
| 1789 | |
|---|
| 1790 | osgDB::Registry::instance()->updateTimeStampOfObjectsInCacheWithExternalReferences(frameStamp.getReferenceTime()); |
|---|
| 1791 | osgDB::Registry::instance()->removeExpiredObjectsInCache(expiryTime); |
|---|
| 1792 | } |
|---|
| 1793 | |
|---|
| 1794 | void DatabasePager::expiry_removeExpiredSubgraphs(const osg::FrameStamp& frameStamp) |
|---|
| 1795 | { |
|---|
| 1796 | |
|---|
| 1797 | |
|---|
| 1798 | static double s_total_iter = 0.0; |
|---|
| 1799 | static double s_total_time = 0.0; |
|---|
| 1800 | static double s_total_max = 0.0; |
|---|
| 1801 | |
|---|
| 1802 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 1803 | |
|---|
| 1804 | double expiryTime = frameStamp.getReferenceTime() - _expiryDelay; |
|---|
| 1805 | int expiryFrame = frameStamp.getFrameNumber() - _expiryFrames; |
|---|
| 1806 | |
|---|
| 1807 | double releaseTime = frameStamp.getReferenceTime() - _releaseDelay; |
|---|
| 1808 | int releaseFrame = frameStamp.getFrameNumber() - _releaseFrames; |
|---|
| 1809 | |
|---|
| 1810 | osg::NodeList childrenRemoved; |
|---|
| 1811 | |
|---|
| 1812 | for(PagedLODList::iterator itr = _activePagedLODList.begin(); |
|---|
| 1813 | itr!=_activePagedLODList.end(); |
|---|
| 1814 | ++itr) |
|---|
| 1815 | { |
|---|
| 1816 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1817 | |
|---|
| 1818 | |
|---|
| 1819 | if (_releaseDelay!=DBL_MAX && plod->releaseGLObjectsOnExpiredChildren(releaseTime, releaseFrame)) |
|---|
| 1820 | { |
|---|
| 1821 | osg::notify(osg::INFO)<<"DatabasePager::removeExpiredSubgraphs(), releasing gl objects"<<std::endl; |
|---|
| 1822 | } |
|---|
| 1823 | |
|---|
| 1824 | plod->removeExpiredChildren(expiryTime, expiryFrame, childrenRemoved); |
|---|
| 1825 | } |
|---|
| 1826 | |
|---|
| 1827 | if (!childrenRemoved.empty()) |
|---|
| 1828 | { |
|---|
| 1829 | MarkPagedLODsVisitor markerVistor("NeedToRemove"); |
|---|
| 1830 | for(osg::NodeList::iterator critr = childrenRemoved.begin(); |
|---|
| 1831 | critr!=childrenRemoved.end(); |
|---|
| 1832 | ++critr) |
|---|
| 1833 | { |
|---|
| 1834 | (*critr)->accept(markerVistor); |
|---|
| 1835 | } |
|---|
| 1836 | |
|---|
| 1837 | |
|---|
| 1838 | |
|---|
| 1839 | |
|---|
| 1840 | if (_deleteRemovedSubgraphsInDatabaseThread) |
|---|
| 1841 | { |
|---|
| 1842 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_fileRequestQueue->_childrenToDeleteListMutex); |
|---|
| 1843 | for (osg::NodeList::iterator critr = childrenRemoved.begin(); |
|---|
| 1844 | critr!=childrenRemoved.end(); |
|---|
| 1845 | ++critr) |
|---|
| 1846 | { |
|---|
| 1847 | _fileRequestQueue->_childrenToDeleteList.push_back(critr->get()); |
|---|
| 1848 | } |
|---|
| 1849 | |
|---|
| 1850 | _fileRequestQueue->updateBlock(); |
|---|
| 1851 | } |
|---|
| 1852 | |
|---|
| 1853 | |
|---|
| 1854 | for(PagedLODList::iterator itr = _activePagedLODList.begin(); |
|---|
| 1855 | itr!=_activePagedLODList.end(); |
|---|
| 1856 | ) |
|---|
| 1857 | { |
|---|
| 1858 | osg::PagedLOD* plod = itr->get(); |
|---|
| 1859 | if (plod && plod->getName() != markerVistor._marker) |
|---|
| 1860 | { |
|---|
| 1861 | ++itr; |
|---|
| 1862 | } |
|---|
| 1863 | else |
|---|
| 1864 | { |
|---|
| 1865 | PagedLODList::iterator itr_to_erase = itr; |
|---|
| 1866 | ++itr; |
|---|
| 1867 | |
|---|
| 1868 | _activePagedLODList.erase(itr_to_erase); |
|---|
| 1869 | } |
|---|
| 1870 | } |
|---|
| 1871 | |
|---|
| 1872 | childrenRemoved.clear(); |
|---|
| 1873 | |
|---|
| 1874 | |
|---|
| 1875 | } |
|---|
| 1876 | |
|---|
| 1877 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 1878 | double time = osg::Timer::instance()->delta_m(startTick,endTick); |
|---|
| 1879 | |
|---|
| 1880 | s_total_iter += 1.0; |
|---|
| 1881 | s_total_time += time; |
|---|
| 1882 | if (s_total_max<time) s_total_max = time; |
|---|
| 1883 | |
|---|
| 1884 | osg::notify(osg::INFO)<<"_activePagedLODList.size()="<<_activePagedLODList.size()<<" overall = "<<time<< |
|---|
| 1885 | " avg="<<s_total_time/s_total_iter<<" max = "<<s_total_max<<std::endl; |
|---|
| 1886 | |
|---|
| 1887 | |
|---|
| 1888 | if (osgDB::Registry::instance()->getSharedStateManager()) |
|---|
| 1889 | osgDB::Registry::instance()->getSharedStateManager()->prune(); |
|---|
| 1890 | |
|---|
| 1891 | |
|---|
| 1892 | osgDB::Registry::instance()->updateTimeStampOfObjectsInCacheWithExternalReferences(frameStamp.getReferenceTime()); |
|---|
| 1893 | osgDB::Registry::instance()->removeExpiredObjectsInCache(expiryTime); |
|---|
| 1894 | |
|---|
| 1895 | |
|---|
| 1896 | } |
|---|
| 1897 | |
|---|
| 1898 | class DatabasePager::FindPagedLODsVisitor : public osg::NodeVisitor |
|---|
| 1899 | { |
|---|
| 1900 | public: |
|---|
| 1901 | |
|---|
| 1902 | FindPagedLODsVisitor(DatabasePager::PagedLODList& pagedLODList, int frameNumber): |
|---|
| 1903 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 1904 | _activePagedLODList(pagedLODList), |
|---|
| 1905 | _frameNumber(frameNumber) |
|---|
| 1906 | { |
|---|
| 1907 | } |
|---|
| 1908 | |
|---|
| 1909 | META_NodeVisitor("osgDB","FindPagedLODsVisitor") |
|---|
| 1910 | |
|---|
| 1911 | virtual void apply(osg::PagedLOD& plod) |
|---|
| 1912 | { |
|---|
| 1913 | plod.setFrameNumberOfLastTraversal(_frameNumber); |
|---|
| 1914 | |
|---|
| 1915 | _activePagedLODList.push_back(&plod); |
|---|
| 1916 | |
|---|
| 1917 | traverse(plod); |
|---|
| 1918 | } |
|---|
| 1919 | |
|---|
| 1920 | DatabasePager::PagedLODList& _activePagedLODList; |
|---|
| 1921 | int _frameNumber; |
|---|
| 1922 | |
|---|
| 1923 | protected: |
|---|
| 1924 | |
|---|
| 1925 | FindPagedLODsVisitor& operator = (const FindPagedLODsVisitor&) { return *this; } |
|---|
| 1926 | }; |
|---|
| 1927 | |
|---|
| 1928 | void DatabasePager::registerPagedLODs(osg::Node* subgraph, int frameNumber) |
|---|
| 1929 | { |
|---|
| 1930 | if (!subgraph) return; |
|---|
| 1931 | |
|---|
| 1932 | FindPagedLODsVisitor fplv(_activePagedLODList, frameNumber); |
|---|
| 1933 | subgraph->accept(fplv); |
|---|
| 1934 | } |
|---|
| 1935 | |
|---|
| 1936 | bool DatabasePager::requiresCompileGLObjects() const |
|---|
| 1937 | { |
|---|
| 1938 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToCompileList->_requestMutex); |
|---|
| 1939 | return !_dataToCompileList->_requestList.empty(); |
|---|
| 1940 | } |
|---|
| 1941 | |
|---|
| 1942 | void DatabasePager::setCompileGLObjectsForContextID(unsigned int contextID, bool on) |
|---|
| 1943 | { |
|---|
| 1944 | if (on) |
|---|
| 1945 | { |
|---|
| 1946 | _activeGraphicsContexts.insert(contextID); |
|---|
| 1947 | } |
|---|
| 1948 | else |
|---|
| 1949 | { |
|---|
| 1950 | _activeGraphicsContexts.erase(contextID); |
|---|
| 1951 | } |
|---|
| 1952 | } |
|---|
| 1953 | |
|---|
| 1954 | bool DatabasePager::getCompileGLObjectsForContextID(unsigned int contextID) |
|---|
| 1955 | { |
|---|
| 1956 | return _activeGraphicsContexts.count(contextID)!=0; |
|---|
| 1957 | } |
|---|
| 1958 | |
|---|
| 1959 | |
|---|
| 1960 | DatabasePager::CompileOperation::CompileOperation(osgDB::DatabasePager* databasePager): |
|---|
| 1961 | osg::GraphicsOperation("DatabasePager::CompileOperation",false), |
|---|
| 1962 | _databasePager(databasePager) |
|---|
| 1963 | { |
|---|
| 1964 | } |
|---|
| 1965 | |
|---|
| 1966 | void DatabasePager::CompileOperation::operator () (osg::GraphicsContext* context) |
|---|
| 1967 | { |
|---|
| 1968 | |
|---|
| 1969 | |
|---|
| 1970 | if (_databasePager.valid()) _databasePager->compileAllGLObjects(*(context->getState())); |
|---|
| 1971 | |
|---|
| 1972 | } |
|---|
| 1973 | |
|---|
| 1974 | bool DatabasePager::requiresExternalCompileGLObjects(unsigned int contextID) const |
|---|
| 1975 | { |
|---|
| 1976 | if (_activeGraphicsContexts.count(contextID)==0) return false; |
|---|
| 1977 | |
|---|
| 1978 | return osg::GraphicsContext::getCompileContext(contextID)==0; |
|---|
| 1979 | } |
|---|
| 1980 | |
|---|
| 1981 | void DatabasePager::compileAllGLObjects(osg::State& state) |
|---|
| 1982 | { |
|---|
| 1983 | double availableTime = DBL_MAX; |
|---|
| 1984 | compileGLObjects(state, availableTime); |
|---|
| 1985 | } |
|---|
| 1986 | |
|---|
| 1987 | void DatabasePager::compileGLObjects(osg::State& state, double& availableTime) |
|---|
| 1988 | { |
|---|
| 1989 | |
|---|
| 1990 | |
|---|
| 1991 | bool compileAll = (availableTime==DBL_MAX); |
|---|
| 1992 | |
|---|
| 1993 | SharedStateManager *sharedManager |
|---|
| 1994 | = Registry::instance()->getSharedStateManager(); |
|---|
| 1995 | osg::RenderInfo renderInfo; |
|---|
| 1996 | renderInfo.setState(&state); |
|---|
| 1997 | |
|---|
| 1998 | if (availableTime>0.0) |
|---|
| 1999 | { |
|---|
| 2000 | |
|---|
| 2001 | const osg::Timer& timer = *osg::Timer::instance(); |
|---|
| 2002 | osg::Timer_t start_tick = timer.tick(); |
|---|
| 2003 | double elapsedTime = 0.0; |
|---|
| 2004 | double estimatedTextureDuration = 0.0001; |
|---|
| 2005 | double estimatedDrawableDuration = 0.0001; |
|---|
| 2006 | |
|---|
| 2007 | osg::ref_ptr<DatabaseRequest> databaseRequest; |
|---|
| 2008 | |
|---|
| 2009 | |
|---|
| 2010 | { |
|---|
| 2011 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToCompileList->_requestMutex); |
|---|
| 2012 | |
|---|
| 2013 | |
|---|
| 2014 | databaseRequest = _dataToCompileList->_requestList.empty() ? 0 : _dataToCompileList->_requestList.front().get(); |
|---|
| 2015 | }; |
|---|
| 2016 | |
|---|
| 2017 | unsigned int numObjectsCompiled = 0; |
|---|
| 2018 | |
|---|
| 2019 | |
|---|
| 2020 | |
|---|
| 2021 | while (databaseRequest.valid() && (compileAll || (elapsedTime<availableTime && numObjectsCompiled<_maximumNumOfObjectsToCompilePerFrame)) ) |
|---|
| 2022 | { |
|---|
| 2023 | DataToCompileMap& dcm = databaseRequest->_dataToCompileMap; |
|---|
| 2024 | DataToCompile& dtc = dcm[state.getContextID()]; |
|---|
| 2025 | if (!dtc.first.empty() && (elapsedTime+estimatedTextureDuration)<availableTime && numObjectsCompiled<_maximumNumOfObjectsToCompilePerFrame) |
|---|
| 2026 | { |
|---|
| 2027 | |
|---|
| 2028 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0); |
|---|
| 2029 | |
|---|
| 2030 | |
|---|
| 2031 | StateSetList& sslist = dtc.first; |
|---|
| 2032 | |
|---|
| 2033 | StateSetList::iterator itr=sslist.begin(); |
|---|
| 2034 | unsigned int objTemp = numObjectsCompiled; |
|---|
| 2035 | for(; |
|---|
| 2036 | itr!=sslist.end() && (compileAll || ((elapsedTime+estimatedTextureDuration)<availableTime && numObjectsCompiled<_maximumNumOfObjectsToCompilePerFrame)); |
|---|
| 2037 | ++itr) |
|---|
| 2038 | { |
|---|
| 2039 | |
|---|
| 2040 | if (isCompiled(itr->get(), state.getContextID()) |
|---|
| 2041 | || (sharedManager && sharedManager->isShared(itr->get()))) |
|---|
| 2042 | { |
|---|
| 2043 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2044 | continue; |
|---|
| 2045 | } |
|---|
| 2046 | double startCompileTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2047 | |
|---|
| 2048 | (*itr)->compileGLObjects(state); |
|---|
| 2049 | |
|---|
| 2050 | GLint p; |
|---|
| 2051 | glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_RESIDENT, &p); |
|---|
| 2052 | |
|---|
| 2053 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2054 | |
|---|
| 2055 | |
|---|
| 2056 | |
|---|
| 2057 | estimatedTextureDuration = (elapsedTime-startCompileTime); |
|---|
| 2058 | |
|---|
| 2059 | ++numObjectsCompiled; |
|---|
| 2060 | } |
|---|
| 2061 | if (osg::getNotifyLevel() >= osg::DEBUG_INFO |
|---|
| 2062 | && numObjectsCompiled > objTemp) |
|---|
| 2063 | osg::notify(osg::DEBUG_INFO)<< _frameNumber << " compiled " |
|---|
| 2064 | << numObjectsCompiled - objTemp |
|---|
| 2065 | << " StateSets" << std::endl; |
|---|
| 2066 | |
|---|
| 2067 | sslist.erase(sslist.begin(),itr); |
|---|
| 2068 | } |
|---|
| 2069 | |
|---|
| 2070 | if (!dtc.second.empty() && (compileAll || ((elapsedTime+estimatedDrawableDuration)<availableTime && numObjectsCompiled<_maximumNumOfObjectsToCompilePerFrame))) |
|---|
| 2071 | { |
|---|
| 2072 | |
|---|
| 2073 | |
|---|
| 2074 | DrawableList& dwlist = dtc.second; |
|---|
| 2075 | DrawableList::iterator itr=dwlist.begin(); |
|---|
| 2076 | unsigned int objTemp = numObjectsCompiled; |
|---|
| 2077 | for(; |
|---|
| 2078 | itr!=dwlist.end() && (compileAll || ((elapsedTime+estimatedDrawableDuration)<availableTime && numObjectsCompiled<_maximumNumOfObjectsToCompilePerFrame)); |
|---|
| 2079 | ++itr) |
|---|
| 2080 | { |
|---|
| 2081 | |
|---|
| 2082 | if (isCompiled(itr->get(), state.getContextID())) |
|---|
| 2083 | { |
|---|
| 2084 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2085 | continue; |
|---|
| 2086 | } |
|---|
| 2087 | double startCompileTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2088 | (*itr)->compileGLObjects(renderInfo); |
|---|
| 2089 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2090 | |
|---|
| 2091 | |
|---|
| 2092 | estimatedDrawableDuration = (elapsedTime-startCompileTime); |
|---|
| 2093 | |
|---|
| 2094 | ++numObjectsCompiled; |
|---|
| 2095 | |
|---|
| 2096 | } |
|---|
| 2097 | if (osg::getNotifyLevel() >= osg::DEBUG_INFO |
|---|
| 2098 | && numObjectsCompiled > objTemp) |
|---|
| 2099 | osg::notify(osg::DEBUG_INFO)<< _frameNumber << " compiled " |
|---|
| 2100 | << numObjectsCompiled - objTemp |
|---|
| 2101 | << " Drawables" << std::endl; |
|---|
| 2102 | |
|---|
| 2103 | dwlist.erase(dwlist.begin(),itr); |
|---|
| 2104 | } |
|---|
| 2105 | |
|---|
| 2106 | |
|---|
| 2107 | |
|---|
| 2108 | |
|---|
| 2109 | |
|---|
| 2110 | |
|---|
| 2111 | |
|---|
| 2112 | bool allCompiled = true; |
|---|
| 2113 | for(DataToCompileMap::iterator itr=dcm.begin(); |
|---|
| 2114 | itr!=dcm.end() && allCompiled; |
|---|
| 2115 | ++itr) |
|---|
| 2116 | { |
|---|
| 2117 | if (!(itr->second.first.empty())) allCompiled=false; |
|---|
| 2118 | if (!(itr->second.second.empty())) allCompiled=false; |
|---|
| 2119 | } |
|---|
| 2120 | |
|---|
| 2121 | |
|---|
| 2122 | |
|---|
| 2123 | |
|---|
| 2124 | if (allCompiled) |
|---|
| 2125 | { |
|---|
| 2126 | |
|---|
| 2127 | |
|---|
| 2128 | |
|---|
| 2129 | |
|---|
| 2130 | |
|---|
| 2131 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToCompileList->_requestMutex); |
|---|
| 2132 | |
|---|
| 2133 | |
|---|
| 2134 | |
|---|
| 2135 | |
|---|
| 2136 | |
|---|
| 2137 | |
|---|
| 2138 | |
|---|
| 2139 | |
|---|
| 2140 | |
|---|
| 2141 | |
|---|
| 2142 | RequestQueue::RequestList::iterator requestIter |
|---|
| 2143 | = std::find(_dataToCompileList->_requestList.begin(), _dataToCompileList->_requestList.end(), |
|---|
| 2144 | databaseRequest); |
|---|
| 2145 | if (requestIter != _dataToCompileList->_requestList.end()) |
|---|
| 2146 | { |
|---|
| 2147 | { |
|---|
| 2148 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToMergeList->_requestMutex); |
|---|
| 2149 | databaseRequest->_requestQueue = _dataToMergeList.get(); |
|---|
| 2150 | _dataToMergeList->_requestList.push_back(databaseRequest); |
|---|
| 2151 | } |
|---|
| 2152 | _dataToCompileList->_requestList.erase(requestIter); |
|---|
| 2153 | } |
|---|
| 2154 | |
|---|
| 2155 | if (!_dataToCompileList->_requestList.empty()) databaseRequest = _dataToCompileList->_requestList.front(); |
|---|
| 2156 | else databaseRequest = 0; |
|---|
| 2157 | |
|---|
| 2158 | } |
|---|
| 2159 | else |
|---|
| 2160 | { |
|---|
| 2161 | |
|---|
| 2162 | databaseRequest = 0; |
|---|
| 2163 | } |
|---|
| 2164 | |
|---|
| 2165 | elapsedTime = timer.delta_s(start_tick,timer.tick()); |
|---|
| 2166 | } |
|---|
| 2167 | |
|---|
| 2168 | availableTime -= elapsedTime; |
|---|
| 2169 | |
|---|
| 2170 | |
|---|
| 2171 | |
|---|
| 2172 | |
|---|
| 2173 | } |
|---|
| 2174 | else |
|---|
| 2175 | { |
|---|
| 2176 | availableTime = 0.0f; |
|---|
| 2177 | } |
|---|
| 2178 | } |
|---|