Skip to content

Commit

Permalink
Refactor ownership of terrain views
Browse files Browse the repository at this point in the history
  • Loading branch information
scrawl committed Mar 14, 2017
1 parent 28fd492 commit 3c29e2d
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 33 deletions.
1 change: 0 additions & 1 deletion apps/openmw/mwworld/cellpreloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ namespace MWWorld
mTerrainPreloadItem = NULL;
}

mTerrain->removeView(mTerrainView);
mTerrainView = NULL;

for (PreloadMap::iterator it = mPreloadCells.begin(); it != mPreloadCells.end();++it)
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwworld/cellpreloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace MWWorld
// Cells that are currently being preloaded, or have already finished preloading
PreloadMap mPreloadCells;

Terrain::View* mTerrainView;
osg::ref_ptr<Terrain::View> mTerrainView;
std::vector<osg::Vec3f> mTerrainPreloadPositions;
osg::ref_ptr<SceneUtil::WorkItem> mTerrainPreloadItem;
};
Expand Down
9 changes: 1 addition & 8 deletions components/terrain/quadtreeworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,7 @@ void QuadTreeWorld::enable(bool enabled)

View* QuadTreeWorld::createView()
{
ViewData* vd = mViewDataMap->createOrReuseView();
vd->setPersistent(true);
return vd;
}

void QuadTreeWorld::removeView(View *view)
{
mViewDataMap->removeView(static_cast<ViewData*>(view));
return new ViewData;
}

void QuadTreeWorld::preload(View *view, const osg::Vec3f &eyePoint)
Expand Down
1 change: 0 additions & 1 deletion components/terrain/quadtreeworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace Terrain
virtual void enable(bool enabled);

View* createView();
void removeView(View* view);
void preload(View* view, const osg::Vec3f& eyePoint);

void reportStats(unsigned int frameNumber, osg::Stats* stats);
Expand Down
11 changes: 1 addition & 10 deletions components/terrain/viewdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ViewData::ViewData()
: mNumEntries(0)
, mFrameLastUsed(0)
, mChanged(false)
, mPersistent(false)
{

}
Expand Down Expand Up @@ -126,20 +125,12 @@ ViewData *ViewDataMap::createOrReuseView()
}
}

void ViewDataMap::removeView(ViewData* vd)
{
vd->setPersistent(false);
vd->clear();
mUnusedViews.push_back(vd);
}

void ViewDataMap::clearUnusedViews(unsigned int frame)
{
for (Map::iterator it = mViews.begin(); it != mViews.end(); )
{
ViewData* vd = it->second;
if (!vd->getPersistent() &&
(!vd->getViewer() // if no ref was held, always need to clear to avoid holding a dangling ref.
if ((!vd->getViewer() // if no ref was held, always need to clear to avoid holding a dangling ref.
|| vd->getFrameLastUsed() + 2 < frame))
{
vd->setViewer(NULL);
Expand Down
5 changes: 0 additions & 5 deletions components/terrain/viewdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ namespace Terrain

Entry& getEntry(unsigned int i);

bool getPersistent() const { return mPersistent; }
void setPersistent(bool persistent) { mPersistent = persistent; }

osg::Object* getViewer() const { return mViewer.get(); }
void setViewer(osg::Object* viewer) { mViewer = viewer; }

Expand All @@ -60,7 +57,6 @@ namespace Terrain
unsigned int mNumEntries;
unsigned int mFrameLastUsed;
bool mChanged;
bool mPersistent;
osg::ref_ptr<osg::Object> mViewer;
};

Expand All @@ -70,7 +66,6 @@ namespace Terrain
ViewData* getViewData(osg::Object* viewer, bool ref);

ViewData* createOrReuseView();
void removeView(ViewData* view);

void clearUnusedViews(unsigned int frame);

Expand Down
11 changes: 4 additions & 7 deletions components/terrain/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COMPONENTS_TERRAIN_WORLD_H

#include <osg/ref_ptr>
#include <osg/Referenced>
#include <osg/Vec3f>

#include <memory>
Expand Down Expand Up @@ -32,7 +33,7 @@ namespace Terrain
* @brief A View is a collection of rendering objects that are visible from a given camera/intersection.
* The base View class is part of the interface for usage in conjunction with preload feature.
*/
class View
class View : public osg::Referenced
{
public:
virtual ~View() {}
Expand Down Expand Up @@ -79,14 +80,10 @@ namespace Terrain

virtual void enable(bool enabled) {}

/// Create a View to use with preload feature. If a View is returned, it will remain valid until the user calls 'removeView' or the World is destroyed.
/// @note Not thread safe.
/// Create a View to use with preload feature. The caller is responsible for deleting the view.
/// @note Thread safe.
virtual View* createView() { return NULL; }

/// Remove a View that was previously created with 'createView'.
/// @note Not thread safe.
virtual void removeView(View* view) {}

/// @note Thread safe, as long as you do not attempt to load into the same view from multiple threads.
virtual void preload(View* view, const osg::Vec3f& eyePoint) {}

Expand Down

0 comments on commit 3c29e2d

Please sign in to comment.