| 1 | // SceneryPager.cxx -- Interface to OSG database pager |
| 2 | // |
| 3 | // Copyright (C) 2007 Tim Moore [email protected] |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include "SceneryPager.hxx" |
| 22 | #include <algorithm> |
| 23 | #include <functional> |
| 24 | #include <simgear/debug/logstream.hxx> |
| 25 | |
| 26 | using namespace osg; |
| 27 | using namespace flightgear; |
| 28 | |
| 29 | SceneryPager::SceneryPager() |
| 30 | { |
| 31 | _pagerRequests.reserve(48); |
| 32 | _deleteRequests.reserve(16); |
| 33 | } |
| 34 | |
| 35 | SceneryPager::SceneryPager(const SceneryPager& rhs) : |
| 36 | DatabasePager(rhs) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | SceneryPager::~SceneryPager() |
| 41 | { |
| 42 | SG_LOG(SG_TERRAIN, SG_INFO, "Destroying scenery pager"); |
| 43 | } |
| 44 | |
| 45 | void SceneryPager::clearRequests() |
| 46 | { |
| 47 | _pagerRequests.clear(); |
| 48 | _deleteRequests.clear(); |
| 49 | } |
| 50 | |
| 51 | void SceneryPager::queueRequest(const std::string& fileName, Group* group, |
| 52 | float priority, FrameStamp* frameStamp, |
| 53 | ref_ptr<Referenced>& databaseRequest, |
| 54 | osgDB::ReaderWriter::Options* options) |
| 55 | { |
| 56 | _pagerRequests.push_back(PagerRequest(fileName, group, priority, |
| 57 | frameStamp, |
| 58 | databaseRequest, |
| 59 | options)); |
| 60 | } |
| 61 | |
| 62 | void SceneryPager::queueDeleteRequest(osg::ref_ptr<osg::Object>& objptr) |
| 63 | { |
| 64 | _deleteRequests.push_back(objptr); |
| 65 | objptr = 0; |
| 66 | } |
| 67 | |
| 68 | // Work around interface change in |
| 69 | // osgDB::DatabasePager::requestNodeFile |
| 70 | namespace |
| 71 | { |
| 72 | struct NodePathProxy |
| 73 | { |
| 74 | NodePathProxy(NodePath& nodePath) |
| 75 | : _nodePath(nodePath) |
| 76 | { |
| 77 | } |
| 78 | operator Group* () { return static_cast<Group*>(_nodePath.back()); } |
| 79 | operator NodePath& () { return _nodePath; } |
| 80 | NodePath& _nodePath; |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | void SceneryPager::PagerRequest::doRequest(SceneryPager* pager) |
| 85 | { |
| 86 | if (_group->getNumChildren() == 0) { |
| 87 | NodePath path; |
| 88 | path.push_back(_group.get()); |
| 89 | pager->requestNodeFile(_fileName, NodePathProxy(path), _priority, |
| 90 | _frameStamp.get(), |
| 91 | *_databaseRequest, |
| 92 | _options.get()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void SceneryPager::signalEndFrame() |
| 97 | { |
| 98 | using namespace std; |
| 99 | bool areDeleteRequests = false; |
| 100 | bool arePagerRequests = false; |
| 101 | if (!_deleteRequests.empty()) { |
| 102 | areDeleteRequests = true; |
| 103 | OpenThreads::ScopedLock<OpenThreads::Mutex> |
| 104 | lock(_fileRequestQueue->_childrenToDeleteListMutex); |
| 105 | ObjectList& deleteList = _fileRequestQueue->_childrenToDeleteList; |
| 106 | deleteList.insert(deleteList.end(), |
| 107 | _deleteRequests.begin(), |
| 108 | _deleteRequests.end()); |
| 109 | _deleteRequests.clear(); |
| 110 | } |
| 111 | if (!_pagerRequests.empty()) { |
| 112 | arePagerRequests = true; |
| 113 | for (auto req : _pagerRequests) { |
| 114 | req.doRequest(this); |
| 115 | } |
| 116 | _pagerRequests.clear(); |
| 117 | } |
| 118 | if (areDeleteRequests && !arePagerRequests) { |
| 119 | _fileRequestQueue->updateBlock(); |
| 120 | } |
| 121 | DatabasePager::signalEndFrame(); |
| 122 | } |
| 123 |