OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DatabasePager.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSGDB_DATABASEPAGER
15 #define OSGDB_DATABASEPAGER 1
16 
17 #include <osg/NodeVisitor>
18 #include <osg/Group>
19 #include <osg/PagedLOD>
20 #include <osg/Drawable>
21 #include <osg/GraphicsThread>
22 #include <osg/FrameStamp>
23 #include <osg/ObserverNodePath>
24 #include <osg/observer_ptr>
25 
26 #include <OpenThreads/Thread>
27 #include <OpenThreads/Mutex>
28 #include <OpenThreads/ScopedLock>
29 #include <OpenThreads/Condition>
30 
31 #include <osgUtil/IncrementalCompileOperation>
32 
33 #include <osgDB/SharedStateManager>
34 #include <osgDB/ReaderWriter>
35 #include <osgDB/Options>
36 
37 
38 #include <map>
39 #include <list>
40 #include <algorithm>
41 #include <functional>
42 
43 namespace osgDB {
44 
45 
46 
50 {
51  public :
52 
54 
55  DatabasePager();
56 
57  DatabasePager(const DatabasePager& rhs);
58 
59  virtual const char* className() const { return "DatabasePager"; }
60 
62  virtual DatabasePager* clone() const { return new DatabasePager(*this); }
63 
65  static osg::ref_ptr<DatabasePager>& prototype();
66 
68  static DatabasePager* create();
69 
70 
71 
73  virtual void requestNodeFile(const std::string& fileName, osg::NodePath& nodePath,
74  float priority, const osg::FrameStamp* framestamp,
75  osg::ref_ptr<osg::Referenced>& databaseRequest,
76  const osg::Referenced* options);
77 
79  int setSchedulePriority(OpenThreads::Thread::ThreadPriority priority);
80 
82  virtual int cancel();
83 
84  virtual bool isRunning() const;
85 
87  virtual void clear();
88 
90  {
91  public:
92 
93  enum Mode
94  {
97  HANDLE_ONLY_HTTP
98  };
99 
100  DatabaseThread(DatabasePager* pager, Mode mode, const std::string& name);
101 
102  DatabaseThread(const DatabaseThread& dt, DatabasePager* pager);
103 
104  void setName(const std::string& name) { _name = name; }
105  const std::string& getName() const { return _name; }
106 
107  void setDone(bool done) { _done.exchange(done?1:0); }
108  bool getDone() const { return _done!=0; }
109 
110  void setActive(bool active) { _active = active; }
111  bool getActive() const { return _active; }
112 
113  virtual int cancel();
114 
115  virtual void run();
116 
117  protected:
118 
119  virtual ~DatabaseThread();
120 
122  volatile bool _active;
125  std::string _name;
126 
127  };
128 
129  void setUpThreads(unsigned int totalNumThreads=2, unsigned int numHttpThreads=1);
130 
131  virtual unsigned int addDatabaseThread(DatabaseThread::Mode mode, const std::string& name);
132 
133  DatabaseThread* getDatabaseThread(unsigned int i) { return _databaseThreads[i].get(); }
134 
135  const DatabaseThread* getDatabaseThread(unsigned int i) const { return _databaseThreads[i].get(); }
136 
137  unsigned int getNumDatabaseThreads() const { return static_cast<unsigned int>(_databaseThreads.size()); }
138 
140  void setDatabasePagerThreadPause(bool pause);
141 
143  bool getDatabasePagerThreadPause() const { return _databasePagerThreadPaused; }
144 
146  void setAcceptNewDatabaseRequests(bool acceptNewRequests) { _acceptNewRequests = acceptNewRequests; }
147 
149  bool getAcceptNewDatabaseRequests() const { return _acceptNewRequests; }
150 
152  int getNumFramesActive() const { return _numFramesActive; }
153 
156  virtual void signalBeginFrame(const osg::FrameStamp* framestamp);
157 
160  virtual void signalEndFrame();
161 
162 
166  virtual void registerPagedLODs(osg::Node* subgraph, unsigned int frameNumber = 0);
167 
171  void setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico);
172 
174  osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); }
175 
176 
181  void setDoPreCompile(bool flag) { _doPreCompile = flag; }
182 
185  bool getDoPreCompile() const { return _doPreCompile; }
186 
187 
188 
192  void setTargetMaximumNumberOfPageLOD(unsigned int target) { _targetMaximumNumberOfPageLOD = target; }
193 
195  unsigned int getTargetMaximumNumberOfPageLOD() const { return _targetMaximumNumberOfPageLOD; }
196 
197 
199  void setDeleteRemovedSubgraphsInDatabaseThread(bool flag) { _deleteRemovedSubgraphsInDatabaseThread = flag; }
200 
202  bool getDeleteRemovedSubgraphsInDatabaseThread() const { return _deleteRemovedSubgraphsInDatabaseThread; }
203 
205  {
209  USE_VERTEX_ARRAYS
210  };
211 
213  void setDrawablePolicy(DrawablePolicy policy) { _drawablePolicy = policy; }
214 
216  DrawablePolicy getDrawablePolicy() const { return _drawablePolicy; }
217 
218 
220  void setApplyPBOToImages(bool assignPBOToImages) { _assignPBOToImages = assignPBOToImages; }
221 
223  bool getApplyPBOToImages() const { return _assignPBOToImages; }
224 
225 
227  void setUnrefImageDataAfterApplyPolicy(bool changeAutoUnRef, bool valueAutoUnRef) { _changeAutoUnRef = changeAutoUnRef; _valueAutoUnRef = valueAutoUnRef; }
228 
230  void getUnrefImageDataAfterApplyPolicy(bool& changeAutoUnRef, bool& valueAutoUnRef) const { changeAutoUnRef = _changeAutoUnRef; valueAutoUnRef = _valueAutoUnRef; }
231 
232 
234  void setMaxAnisotropyPolicy(bool changeAnisotropy, float valueAnisotropy) { _changeAnisotropy = changeAnisotropy; _valueAnisotropy = valueAnisotropy; }
235 
237  void getMaxAnisotropyPolicy(bool& changeAnisotropy, float& valueAnisotropy) const { changeAnisotropy = _changeAnisotropy; valueAnisotropy = _valueAnisotropy; }
238 
239 
241  bool requiresUpdateSceneGraph() const;
242 
245  virtual void updateSceneGraph(const osg::FrameStamp& frameStamp);
246 
248  unsigned int getFileRequestListSize() const { return static_cast<unsigned int>(_fileRequestQueue->size() + _httpRequestQueue->size()); }
249 
251  unsigned int getDataToCompileListSize() const { return static_cast<unsigned int>(_dataToCompileList->size()); }
252 
254  unsigned int getDataToMergeListSize() const { return static_cast<unsigned int>(_dataToMergeList->size()); }
255 
257  bool getRequestsInProgress() const;
258 
260  double getMinimumTimeToMergeTile() const { return _minimumTimeToMergeTile; }
261 
263  double getMaximumTimeToMergeTile() const { return _maximumTimeToMergeTile; }
264 
266  double getAverageTimeToMergeTiles() const { return (_numTilesMerges > 0) ? _totalTimeToMergeTiles/static_cast<double>(_numTilesMerges) : 0; }
267 
269  void resetStats();
270 
271  typedef std::set< osg::ref_ptr<osg::StateSet> > StateSetList;
272  typedef std::vector< osg::ref_ptr<osg::Drawable> > DrawableList;
273 
274  class ExpirePagedLODsVisitor;
275 
276  typedef std::list< osg::ref_ptr<osg::Object> > ObjectList;
277 
279  {
280  virtual PagedLODList* clone() = 0;
281  virtual void clear() = 0;
282  virtual unsigned int size() = 0;
283  virtual void removeExpiredChildren(int numberChildrenToRemove, double expiryTime, unsigned int expiryFrame, ObjectList& childrenRemoved, bool visitActive) = 0;
284  virtual void removeNodes(osg::NodeList& nodesToRemove) = 0;
285  virtual void insertPagedLOD(const osg::observer_ptr<osg::PagedLOD>& plod) = 0;
286  virtual bool containsPagedLOD(const osg::observer_ptr<osg::PagedLOD>& plod) const = 0;
287  };
288 
289  void setMarkerObject(osg::Object* mo) { _markerObject = mo; }
290  osg::Object* getMarkerObject() { return _markerObject.get(); }
291  const osg::Object* getMarkerObject() const { return _markerObject.get(); }
292 
293  protected:
294 
295  virtual ~DatabasePager();
296 
297  friend class DatabaseThread;
298  friend struct DatabaseRequest;
299 
300  struct RequestQueue;
301 
303  {
305  osg::Referenced(true),
306  _valid(false),
307  _frameNumberFirstRequest(0),
308  _timestampFirstRequest(0.0),
309  _priorityFirstRequest(0.f),
310  _frameNumberLastRequest(0),
311  _timestampLastRequest(0.0),
312  _priorityLastRequest(0.0f),
313  _numOfRequests(0),
314  _groupExpired(false)
315  {}
316 
317  void invalidate();
318 
319  bool valid() const { return _valid; }
320 
321  bool isRequestCurrent (int frameNumber) const
322  {
323  return _valid && (frameNumber - _frameNumberLastRequest <= 1);
324  }
325 
326  bool _valid;
327  std::string _fileName;
334  unsigned int _numOfRequests;
335 
338 
342 
344  bool _groupExpired; // flag used only in update thread
345  };
346 
347 
349  {
350  public:
351 
352  RequestQueue(DatabasePager* pager);
353 
354  void add(DatabaseRequest* databaseRequest);
355  void remove(DatabaseRequest* databaseRequest);
356 
357  void addNoLock(DatabaseRequest* databaseRequest);
358 
359  void takeFirst(osg::ref_ptr<DatabaseRequest>& databaseRequest);
360 
362  bool pruneOldRequestsAndCheckIfEmpty();
363 
364  virtual void updateBlock() {}
365 
366  void invalidate(DatabaseRequest* dr);
367 
368  bool empty();
369 
370  unsigned int size();
371 
372  void clear();
373 
374 
375  typedef std::list< osg::ref_ptr<DatabaseRequest> > RequestList;
376  void swap(RequestList& requestList);
377 
379  RequestList _requestList;
382 
383  protected:
384  virtual ~RequestQueue();
385  };
386 
387 
388  typedef std::vector< osg::ref_ptr<DatabaseThread> > DatabaseThreadList;
389 
391  {
392  ReadQueue(DatabasePager* pager, const std::string& name);
393 
394  void block() { _block->block(); }
395 
396  void release() { _block->release(); }
397 
398  virtual void updateBlock();
399 
400 
402 
403  std::string _name;
404 
407  };
408 
409  // forward declare inner helper classes
410  class FindCompileableGLObjectsVisitor;
411  friend class FindCompileableGLObjectsVisitor;
412 
413  struct DatabasePagerCompileCompletedCallback;
414  friend struct DatabasePagerCompileCompletedCallback;
415 
416  class FindPagedLODsVisitor;
417  friend class FindPagedLODsVisitor;
418 
419  struct SortFileRequestFunctor;
420  friend struct SortFileRequestFunctor;
421 
422 
426 
427  void compileCompleted(DatabaseRequest* databaseRequest);
428 
432  virtual void removeExpiredSubgraphs(const osg::FrameStamp &frameStamp);
433 
435  void addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp);
436 
437 
438  bool _done;
441 
442  DatabaseThreadList _databaseThreads;
443 
447 
452 
454 
460 
462 
463 
465 
467 
470 
471 
475  unsigned int _numTilesMerges;
476 
478 };
479 
480 }
481 
482 #endif
unsigned int getFileRequestListSize() const
virtual const char * className() const
Definition: DatabasePager.h:59
std::set< osg::ref_ptr< osg::StateSet > > StateSetList
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
unsigned int _targetMaximumNumberOfPageLOD
unsigned int getDataToMergeListSize() const
bool _deleteRemovedSubgraphsInDatabaseThread
bool getDatabasePagerThreadPause() const
double getMinimumTimeToMergeTile() const
osg::Object * getMarkerObject()
void setAcceptNewDatabaseRequests(bool acceptNewRequests)
unsigned int _numTilesMerges
bool getApplyPBOToImages() const
osg::ref_ptr< RequestQueue > _dataToCompileList
void setDeleteRemovedSubgraphsInDatabaseThread(bool flag)
void getUnrefImageDataAfterApplyPolicy(bool &changeAutoUnRef, bool &valueAutoUnRef) const
void setName(const std::string &name)
DrawablePolicy getDrawablePolicy() const
std::vector< osg::ref_ptr< osg::Drawable > > DrawableList
int getNumFramesActive() const
osg::ref_ptr< RequestQueue > _dataToMergeList
osg::ref_ptr< ReadQueue > _fileRequestQueue
osg::observer_ptr< osg::Group > _group
unsigned int getDataToCompileListSize() const
const std::string & getName() const
const osg::Object * getMarkerObject() const
void setDoPreCompile(bool flag)
void swap(MixinVector< ValueT > &left, MixinVector< ValueT > &right)
Definition: MixinVector.h:174
This class provides an atomic increment and decrement operation.
Definition: Atomic.h:48
osg::observer_ptr< osg::Node > _terrain
OpenThreads::Atomic _frameNumber
double getAverageTimeToMergeTiles() const
void setMarkerObject(osg::Object *mo)
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition: Object.h:242
osg::observer_ptr< osgUtil::IncrementalCompileOperation::CompileSet > _compileSet
void setDrawablePolicy(DrawablePolicy policy)
const DatabaseThread * getDatabaseThread(unsigned int i) const
std::list< osg::ref_ptr< DatabaseRequest > > RequestList
osg::ref_ptr< ObjectCache > _objectCache
osg::ref_ptr< Options > _loadOptions
void getMaxAnisotropyPolicy(bool &changeAnisotropy, float &valueAnisotropy) const
osg::ref_ptr< PagedLODList > _activePagedLODList
void setApplyPBOToImages(bool assignPBOToImages)
bool getDoPreCompile() const
osg::ref_ptr< osgUtil::IncrementalCompileOperation > _incrementalCompileOperation
This class provides an object-oriented thread interface.
Definition: Thread.h:49
void setTargetMaximumNumberOfPageLOD(unsigned int target)
Definition: Archive.h:24
DatabaseThreadList _databaseThreads
bool isRequestCurrent(int frameNumber) const
unsigned int getTargetMaximumNumberOfPageLOD() const
OpenThreads::Mutex _run_mutex
osg::ref_ptr< ReadQueue > _httpRequestQueue
std::vector< osg::ref_ptr< DatabaseThread > > DatabaseThreadList
OpenThreads::Mutex _childrenToDeleteListMutex
std::list< osg::ref_ptr< osg::Object > > ObjectList
#define OSGDB_EXPORT
Definition: Export.h:39
double getMaximumTimeToMergeTile() const
bool getAcceptNewDatabaseRequests() const
Definition: Node.h:71
Definition: AlphaFunc.h:19
std::vector< ref_ptr< Node > > NodeList
Definition: Group.h:22
OpenThreads::Thread::ThreadPriority ThreadPriority
Definition: DatabasePager.h:53
DrawablePolicy _drawablePolicy
OpenThreads::Mutex _numFramesActiveMutex
osgUtil::IncrementalCompileOperation * getIncrementalCompileOperation()
void setUnrefImageDataAfterApplyPolicy(bool changeAutoUnRef, bool valueAutoUnRef)
osg::ref_ptr< osg::Object > _markerObject
void setMaxAnisotropyPolicy(bool changeAnisotropy, float valueAnisotropy)
OpenThreads::Mutex _dr_mutex
bool getDeleteRemovedSubgraphsInDatabaseThread() const
osg::ref_ptr< osg::RefBlock > _block
osg::ref_ptr< osg::Node > _loadedModel
DatabaseThread * getDatabaseThread(unsigned int i)
virtual DatabasePager * clone() const
Definition: DatabasePager.h:62
std::vector< Node * > NodePath
Definition: Node.h:44
unsigned int getNumDatabaseThreads() const