forked from teamnwah/openmw-tes3coop
Introduce UnrefQueue to handle the deleting of no longer needed objects in the background thread
parent
f6f9eff9a6
commit
d11c2864df
@ -0,0 +1,46 @@
|
||||
#include "unrefqueue.hpp"
|
||||
|
||||
#include <osg/Object>
|
||||
//#include <osg/Timer>
|
||||
//#include <iostream>
|
||||
|
||||
#include <components/sceneutil/workqueue.hpp>
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
|
||||
class UnrefWorkItem : public SceneUtil::WorkItem
|
||||
{
|
||||
public:
|
||||
std::vector<osg::ref_ptr<osg::Object> > mObjects;
|
||||
|
||||
virtual void doWork()
|
||||
{
|
||||
//osg::Timer timer;
|
||||
//size_t objcount = mObjects.size();
|
||||
mObjects.clear();
|
||||
//std::cout << "cleared " << objcount << " objects in " << timer.time_m() << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
UnrefQueue::UnrefQueue()
|
||||
{
|
||||
mWorkItem = new UnrefWorkItem;
|
||||
}
|
||||
|
||||
void UnrefQueue::push(osg::Object *obj)
|
||||
{
|
||||
mWorkItem->mObjects.push_back(obj);
|
||||
}
|
||||
|
||||
void UnrefQueue::flush(SceneUtil::WorkQueue *workQueue)
|
||||
{
|
||||
if (mWorkItem->mObjects.empty())
|
||||
return;
|
||||
|
||||
workQueue->addWorkItem(mWorkItem);
|
||||
|
||||
mWorkItem = new UnrefWorkItem;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
#ifndef OPENMW_COMPONENTS_UNREFQUEUE_H
|
||||
#define OPENMW_COMPONENTS_UNREFQUEUE_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
#include <osg/Referenced>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Object;
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
class WorkQueue;
|
||||
class UnrefWorkItem;
|
||||
|
||||
/// @brief Handles unreferencing of objects through the WorkQueue. Typical use scenario
|
||||
/// would be the main thread pushing objects that are no longer needed, and the background thread deleting them.
|
||||
class UnrefQueue : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
UnrefQueue();
|
||||
|
||||
/// Adds an object to the list of objects to be unreferenced. Call from the main thread.
|
||||
void push(osg::Object* obj);
|
||||
|
||||
/// Adds a WorkItem to the given WorkQueue that will clear the list of objects in a worker thread, thus unreferencing them.
|
||||
/// Call from the main thread.
|
||||
void flush(SceneUtil::WorkQueue* workQueue);
|
||||
|
||||
private:
|
||||
osg::ref_ptr<UnrefWorkItem> mWorkItem;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue