From 3c29e2dbeb140d8aeb8f467452069ce15c3715ba Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 9 Mar 2017 20:42:19 +0100 Subject: [PATCH] Refactor ownership of terrain views --- apps/openmw/mwworld/cellpreloader.cpp | 1 - apps/openmw/mwworld/cellpreloader.hpp | 2 +- components/terrain/quadtreeworld.cpp | 9 +-------- components/terrain/quadtreeworld.hpp | 1 - components/terrain/viewdata.cpp | 11 +---------- components/terrain/viewdata.hpp | 5 ----- components/terrain/world.hpp | 11 ++++------- 7 files changed, 7 insertions(+), 33 deletions(-) diff --git a/apps/openmw/mwworld/cellpreloader.cpp b/apps/openmw/mwworld/cellpreloader.cpp index e633f5825..5d4a2717f 100644 --- a/apps/openmw/mwworld/cellpreloader.cpp +++ b/apps/openmw/mwworld/cellpreloader.cpp @@ -207,7 +207,6 @@ namespace MWWorld mTerrainPreloadItem = NULL; } - mTerrain->removeView(mTerrainView); mTerrainView = NULL; for (PreloadMap::iterator it = mPreloadCells.begin(); it != mPreloadCells.end();++it) diff --git a/apps/openmw/mwworld/cellpreloader.hpp b/apps/openmw/mwworld/cellpreloader.hpp index 9bd062547..bc1227b2d 100644 --- a/apps/openmw/mwworld/cellpreloader.hpp +++ b/apps/openmw/mwworld/cellpreloader.hpp @@ -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 mTerrainView; std::vector mTerrainPreloadPositions; osg::ref_ptr mTerrainPreloadItem; }; diff --git a/components/terrain/quadtreeworld.cpp b/components/terrain/quadtreeworld.cpp index 00311711a..f17fa8bbe 100644 --- a/components/terrain/quadtreeworld.cpp +++ b/components/terrain/quadtreeworld.cpp @@ -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(view)); + return new ViewData; } void QuadTreeWorld::preload(View *view, const osg::Vec3f &eyePoint) diff --git a/components/terrain/quadtreeworld.hpp b/components/terrain/quadtreeworld.hpp index e5ab097b3..d24b9b511 100644 --- a/components/terrain/quadtreeworld.hpp +++ b/components/terrain/quadtreeworld.hpp @@ -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); diff --git a/components/terrain/viewdata.cpp b/components/terrain/viewdata.cpp index 1e7245a15..7b3df56b9 100644 --- a/components/terrain/viewdata.cpp +++ b/components/terrain/viewdata.cpp @@ -7,7 +7,6 @@ ViewData::ViewData() : mNumEntries(0) , mFrameLastUsed(0) , mChanged(false) - , mPersistent(false) { } @@ -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); diff --git a/components/terrain/viewdata.hpp b/components/terrain/viewdata.hpp index c06e1dfff..30563d566 100644 --- a/components/terrain/viewdata.hpp +++ b/components/terrain/viewdata.hpp @@ -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; } @@ -60,7 +57,6 @@ namespace Terrain unsigned int mNumEntries; unsigned int mFrameLastUsed; bool mChanged; - bool mPersistent; osg::ref_ptr mViewer; }; @@ -70,7 +66,6 @@ namespace Terrain ViewData* getViewData(osg::Object* viewer, bool ref); ViewData* createOrReuseView(); - void removeView(ViewData* view); void clearUnusedViews(unsigned int frame); diff --git a/components/terrain/world.hpp b/components/terrain/world.hpp index f8613b443..f7454c60a 100644 --- a/components/terrain/world.hpp +++ b/components/terrain/world.hpp @@ -2,6 +2,7 @@ #define COMPONENTS_TERRAIN_WORLD_H #include +#include #include #include @@ -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() {} @@ -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) {}