]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/SceneryPager.cxx
Improve timing statistics
[flightgear.git] / src / Scenery / SceneryPager.cxx
1 // SceneryPager.cxx -- Interface to OSG database pager
2 //
3 // Copyright (C) 2007 Tim Moore timoore@redhat.com
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 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <simgear/scene/model/SGPagedLOD.hxx>
24 #include <simgear/math/SGMath.hxx>
25 #include "SceneryPager.hxx"
26 #include <algorithm>
27 #include <functional>
28
29 using namespace osg;
30 using namespace flightgear;
31
32 SceneryPager::SceneryPager()
33 {
34     _pagerRequests.reserve(48);
35     _deleteRequests.reserve(16);
36 }
37
38 SceneryPager::SceneryPager(const SceneryPager& rhs) :
39     DatabasePager(rhs)
40 {
41 }
42
43 SceneryPager::~SceneryPager()
44 {
45 }
46
47 #if SG_PAGEDLOD_HAS_OPTIONS
48 #else
49 void SceneryPager::requestNodeFile(const std::string& fileName, Group* group,
50                                    float priority, const FrameStamp* framestamp,
51                                    ref_ptr<Referenced>& databaseRequest,
52                                    osgDB::ReaderWriter::Options* options)
53 {
54     simgear::SGPagedLOD *sgplod = dynamic_cast<simgear::SGPagedLOD*>(group);
55     if(sgplod)
56         DatabasePager::requestNodeFile(fileName, group, priority, framestamp,
57                                        databaseRequest,
58                                        sgplod->getReaderWriterOptions());
59     else
60         DatabasePager::requestNodeFile(fileName, group, priority, framestamp,
61                                        databaseRequest,
62                                        options);
63 }
64 #endif
65
66 void SceneryPager::queueRequest(const std::string& fileName, Group* group,
67                                 float priority, FrameStamp* frameStamp,
68                                 ref_ptr<Referenced>& databaseRequest,
69                                 osgDB::ReaderWriter::Options* options)
70 {
71     _pagerRequests.push_back(PagerRequest(fileName, group, priority,
72                                           frameStamp,
73                                           databaseRequest,
74                                           options));
75 }
76
77 void SceneryPager::queueDeleteRequest(osg::ref_ptr<osg::Object>& objptr)
78 {
79     _deleteRequests.push_back(objptr);
80     objptr = 0;
81 }
82
83 // Work around interface change in
84 // osgDB::DatabasePager::requestNodeFile
85 namespace
86 {
87 struct NodePathProxy
88 {
89     NodePathProxy(NodePath& nodePath)
90         : _nodePath(nodePath)
91     {
92     }
93     operator Group* () { return static_cast<Group*>(_nodePath.back()); }
94     operator NodePath& () { return _nodePath; }
95     NodePath& _nodePath;
96 };
97 }
98
99 void SceneryPager::PagerRequest::doRequest(SceneryPager* pager)
100 {
101     if (_group->getNumChildren() == 0) {
102         NodePath path;
103         path.push_back(_group.get());
104         pager->requestNodeFile(_fileName, NodePathProxy(path), _priority,
105                                _frameStamp.get(),
106                                *_databaseRequest,
107                                _options.get());
108     }
109 }
110
111 void SceneryPager::signalEndFrame()
112 {
113     using namespace std;
114     bool areDeleteRequests = false;
115     bool arePagerRequests = false;
116     if (!_deleteRequests.empty()) {
117         areDeleteRequests = true;
118         OpenThreads::ScopedLock<OpenThreads::Mutex>
119             lock(_fileRequestQueue->_childrenToDeleteListMutex);
120         ObjectList& deleteList = _fileRequestQueue->_childrenToDeleteList;
121         deleteList.insert(deleteList.end(),
122                           _deleteRequests.begin(),
123                           _deleteRequests.end());
124         _deleteRequests.clear();
125     }
126     if (!_pagerRequests.empty()) {
127         arePagerRequests = true;
128         for_each(_pagerRequests.begin(), _pagerRequests.end(),
129                  bind2nd(mem_fun_ref(&PagerRequest::doRequest), this));
130         _pagerRequests.clear();
131     }
132     if (areDeleteRequests && !arePagerRequests) {
133         _fileRequestQueue->updateBlock();
134     }
135     DatabasePager::signalEndFrame();
136 }
137