2003-07-08 22:44:00 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
2003-07-21 16:19:36 +08:00
#ifndef OSGDB_DATABASEPAGER
#define OSGDB_DATABASEPAGER 1
2003-07-08 22:44:00 +08:00
#include <osg/NodeVisitor>
#include <osg/Group>
#include <osg/PagedLOD>
2003-07-10 03:48:04 +08:00
#include <osg/Drawable>
2003-07-08 22:44:00 +08:00
2003-07-19 08:18:07 +08:00
#include <OpenThreads/Thread>
#include <OpenThreads/Mutex>
2004-05-08 03:55:12 +08:00
#include <OpenThreads/ScopedLock>
2003-10-12 22:51:54 +08:00
#include <OpenThreads/Condition>
2003-10-12 20:13:58 +08:00
2004-01-11 01:13:20 +08:00
#include <osgDB/SharedStateManager>
2003-07-21 16:19:36 +08:00
#include <osgDB/Export>
2003-07-08 22:44:00 +08:00
2003-07-10 03:48:04 +08:00
#include <map>
2003-07-10 19:10:39 +08:00
#include <set>
2003-07-10 03:48:04 +08:00
2003-07-21 16:19:36 +08:00
namespace osgDB {
2003-07-08 22:44:00 +08:00
2003-10-12 22:51:54 +08:00
class Block: public osg::Referenced {
public:
Block():_released(false) {}
void block()
{
2004-05-08 03:55:12 +08:00
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
2003-10-12 22:51:54 +08:00
if( !_released )
_cond.wait(&_mut);
}
void release()
{
2004-05-08 03:55:12 +08:00
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
2004-01-11 01:13:20 +08:00
if (!_released)
{
_released = true;
_cond.broadcast();
}
2003-10-12 22:51:54 +08:00
}
void reset()
{
2004-05-08 03:55:12 +08:00
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
2003-10-12 22:51:54 +08:00
_released = false;
}
protected:
~Block()
{
release();
}
private:
OpenThreads::Mutex _mut;
OpenThreads::Condition _cond;
bool _released;
};
2003-07-08 22:44:00 +08:00
/** Database paging class which manages the loading of files in a background thread,
* and syncronizing of loaded models with the main scene graph.*/
2003-07-21 16:19:36 +08:00
class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandler, public OpenThreads::Thread
2003-07-08 22:44:00 +08:00
{
public :
DatabasePager();
2003-07-09 22:55:39 +08:00
/** Add a request to load a node file to end the the database request list.*/
2003-10-11 03:25:14 +08:00
virtual void requestNodeFile(const std::string& fileName,osg::Group* group, float priority, const osg::FrameStamp* framestamp);
2003-07-08 22:44:00 +08:00
/** run does the database paging.*/
virtual void run();
2004-01-07 20:37:44 +08:00
/** Set the use of the frame block which, if enabled, blocks the DatabasePager
* from executing which the current frame is being drawn.
* When a single processor machine is being used it can be useful to block on
* frame to help prevent the database paging thread from slowing the cull and draw
* traversals which in turn can cause frame drops.*/
void setUseFrameBlock(bool useFrameBlock) { _useFrameBlock = useFrameBlock; }
/** Get the whether UseFrameBlock is on or off.*/
bool getUseFrameBlock() const { return _useFrameBlock; }
2004-01-23 21:25:45 +08:00
Block* getFrameBlock() { return _frameBlock.get(); }
2004-01-22 18:42:32 +08:00
/** Set the priority of the database pager thread during the frame (i.e. while cull and draw are running.)*/
void setThreadPriorityDuringFrame(ThreadPriority duringFrame) { _threadPriorityDuringFrame = duringFrame; }
/** Get the priority of the database pager thread during the frame*/
ThreadPriority getThreadPriorityDuringFrame() const { return _threadPriorityDuringFrame; }
/** Set the priority of the database pager thread when the frame is not being exectuted (i.e. before or after cull and draw have run.)*/
void setThreadPriorityOutwithFrame(ThreadPriority outwithFrame) { _threadPriorityOutwithFrame = outwithFrame; }
/** Get the priority of the database pager thread when the frame is not being exectuted.*/
ThreadPriority getThreadPriorityOutwithFrame() const { return _threadPriorityOutwithFrame; }
2003-10-12 20:13:58 +08:00
/** Signal the database thread that the update, cull and draw has begun for a new frame.
* Note, this is called by the application so that the database pager can go to sleep while the CPU is busy on the main rendering threads. */
void signalBeginFrame(const osg::FrameStamp* framestamp);
/** Signal the database thread that the update, cull and draw dispatch has completed.
* Note, this is called by the application so that the database pager can go to wake back up now the main rendering threads are iddle waiting for the next frame.*/
void signalEndFrame();
2003-07-09 22:55:39 +08:00
/** Find all PagedLOD nodes in a subgraph and register them with
* the DatabasePager so it can keep track of expired nodes.
* note, should be only be called from the update thread. */
2003-07-08 22:44:00 +08:00
void registerPagedLODs(osg::Node* subgraph);
2003-07-09 22:55:39 +08:00
2003-07-08 22:44:00 +08:00
/** Set the amount of time that a subgraph will be kept without being visited in the cull traversal
* before being removed.*/
void setExpiryDelay(double expiryDelay) { _expiryDelay = expiryDelay; }
/** Get the amount of time that a subgraph will be kept without being visited in the cull traversal
* before being removed.*/
double getExpiryDelay() const { return _expiryDelay; }
2003-07-09 22:55:39 +08:00
/** set whether the removed subgraphs should be deleted in the database thread or not.*/
void setDeleteRemovedSubgraphsInDatabaseThread(bool flag) { _deleteRemovedSubgraphsInDatabaseThread = flag; }
/** get whether the removed subgraphs should be deleted in the database thread or not.*/
bool getDeleteRemovedSubgraphsInDatabaseThread() const { return _deleteRemovedSubgraphsInDatabaseThread; }
2004-07-20 13:37:59 +08:00
/** set whether newly loaded textures should have their UnrefImageDataAfterApply set to a specified value.*/
void setUnrefImageDataAfterApplyPolicy(bool changeAutoUnRef, bool valueAutoUnRef) { _changeAutoUnRef = changeAutoUnRef; _valueAutoUnRef = valueAutoUnRef; }
/** get whether newly loaded textures should have their UnrefImageDataAfterApply set to a specified value.*/
void getUnrefImageDataAfterApplyPolicy(bool& changeAutoUnRef, bool& valueAutoUnRef) const { changeAutoUnRef = _changeAutoUnRef; valueAutoUnRef = _valueAutoUnRef; }
/** set whether newly loaded textures should have their MaxAnisotopy set to a specified value.*/
void setMaxAnisotropyPolicy(bool changeAnisotropy, float valueAnisotropy) { _changeAnisotropy = changeAnisotropy; _valueAnisotropy = valueAnisotropy; }
/** set whether newly loaded textures should have their MaxAnisotopy set to a specified value.*/
void getMaxAnisotropyPolicy(bool& changeAnisotropy, float& valueAnisotropy) const { changeAnisotropy = _changeAnisotropy; valueAnisotropy = _valueAnisotropy; }
2003-07-09 22:55:39 +08:00
/** Iterate through the active PagedLOD nodes children removing
* children which havn't been visited since specified expiryTime.
* note, should be only be called from the update thread. */
2003-07-08 22:44:00 +08:00
void removeExpiredSubgraphs(double currentFrameTime);
2004-01-07 05:18:36 +08:00
/** Add the loaded data to the scene graph.*/
void addLoadedDataToSceneGraph(double currentFrameTime);
/** Merge the changes to the scene graph by calling calling removeExpiredSubgraphs then addLoadedDataToSceneGraph*/
void updateSceneGraph(double currentFrameTime)
{
removeExpiredSubgraphs(currentFrameTime);
addLoadedDataToSceneGraph(currentFrameTime);
}
2003-07-08 22:44:00 +08:00
2003-07-09 22:55:39 +08:00
2003-07-10 19:10:39 +08:00
/** Turn the compilation of rendering objects for specfied graphics context on (true) or off(false).*/
2004-01-07 05:18:36 +08:00
void setCompileGLObjectsForContexID(unsigned int contextID, bool on);
2003-07-10 19:10:39 +08:00
/** Get whether the compilation of rendering objects for specfied graphics context on (true) or off(false).*/
2004-01-07 05:18:36 +08:00
bool getCompileGLObjectsForContexID(unsigned int contextID);
2003-07-10 19:10:39 +08:00
2003-07-09 22:55:39 +08:00
/** Compile the rendering objects (display lists,texture objects, VBO's) on loaded subgraph.
* note, should only be called from the draw thread.*/
2004-01-07 05:18:36 +08:00
void compileGLObjects(osg::State& state,double& availableTime);
2003-07-08 22:44:00 +08:00
2003-07-15 17:39:45 +08:00
public:
2003-07-10 03:48:04 +08:00
typedef std::vector< osg::ref_ptr<osg::PagedLOD> > PagedLODList;
2003-07-09 22:55:39 +08:00
typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
typedef std::vector< osg::ref_ptr<osg::Drawable> > DrawableList;
2003-07-10 03:48:04 +08:00
typedef std::pair<StateSetList,DrawableList> DataToCompile;
typedef std::map< unsigned int, DataToCompile > DataToCompileMap;
2003-07-08 22:44:00 +08:00
2003-07-10 19:10:39 +08:00
typedef std::set<unsigned int> ActiveGraphicsContexts;
2003-07-15 17:39:45 +08:00
2003-07-10 19:10:39 +08:00
2003-07-08 22:44:00 +08:00
struct DatabaseRequest : public osg::Referenced
{
DatabaseRequest():
_numOfRequests(0)
{}
2003-10-11 03:25:14 +08:00
2003-07-08 22:44:00 +08:00
std::string _fileName;
2003-10-12 20:13:58 +08:00
int _frameNumberFirstRequest;
2003-10-11 03:25:14 +08:00
double _timestampFirstRequest;
float _priorityFirstRequest;
2003-10-12 20:13:58 +08:00
int _frameNumberLastRequest;
2003-10-11 03:25:14 +08:00
double _timestampLastRequest;
float _priorityLastRequest;
2003-07-08 22:44:00 +08:00
unsigned int _numOfRequests;
osg::ref_ptr<osg::Group> _groupForAddingLoadedSubgraph;
osg::ref_ptr<osg::Node> _loadedModel;
2003-07-10 03:48:04 +08:00
DataToCompileMap _dataToCompileMap;
2003-07-08 22:44:00 +08:00
};
typedef std::vector< osg::ref_ptr<DatabaseRequest> > DatabaseRequestList;
2004-01-27 22:49:59 +08:00
typedef std::vector< osg::ref_ptr<osg::Object> > ObjectList;
2003-07-08 22:44:00 +08:00
2003-10-11 03:25:14 +08:00
protected :
virtual ~DatabasePager();
2004-01-07 20:37:44 +08:00
bool _useFrameBlock;
2003-10-12 22:51:54 +08:00
osg::ref_ptr<Block> _frameBlock;
2003-10-12 20:13:58 +08:00
int _frameNumber;
2004-01-22 18:42:32 +08:00
ThreadPriority _threadPriorityDuringFrame;
ThreadPriority _threadPriorityOutwithFrame;
2003-10-12 20:13:58 +08:00
DatabaseRequestList _fileRequestList;
OpenThreads::Mutex _fileRequestListMutex;
2003-10-12 22:51:54 +08:00
osg::ref_ptr<Block> _fileRequestListEmptyBlock;
2003-07-08 22:44:00 +08:00
2003-10-12 20:13:58 +08:00
DatabaseRequestList _dataToCompileList;
OpenThreads::Mutex _dataToCompileListMutex;
2004-07-20 13:37:59 +08:00
bool _changeAutoUnRef;
bool _valueAutoUnRef;
bool _changeAnisotropy;
float _valueAnisotropy;
2004-01-27 22:49:59 +08:00
2003-10-12 20:13:58 +08:00
bool _deleteRemovedSubgraphsInDatabaseThread;
2004-01-27 22:49:59 +08:00
ObjectList _childrenToDeleteList;
2003-10-12 20:13:58 +08:00
OpenThreads::Mutex _childrenToDeleteListMutex;
2003-07-09 22:55:39 +08:00
2003-10-12 20:13:58 +08:00
DatabaseRequestList _dataToMergeList;
OpenThreads::Mutex _dataToMergeListMutex;
2003-07-09 22:55:39 +08:00
2003-07-08 22:44:00 +08:00
2003-10-12 20:13:58 +08:00
PagedLODList _pagedLODList;
2003-07-08 22:44:00 +08:00
2003-10-12 20:13:58 +08:00
double _expiryDelay;
2003-07-08 22:44:00 +08:00
2003-10-12 20:13:58 +08:00
ActiveGraphicsContexts _activeGraphicsContexts;
2003-07-08 22:44:00 +08:00
};
}
#endif