From c5963c3c23d3ffc66bf5b1d257ff2edc6354f74f Mon Sep 17 00:00:00 2001 From: "wrenczes@gmail.com" Date: Thu, 7 Jul 2011 07:37:17 +0000 Subject: [PATCH] Three improvements involving image caching: 1) prevent the textureQuad map from containing references to cards when we're returning a temp (card backdrop) image in the threaded version. 2) we had two copies of the pspicon image in the cache (ie the regular & managed). Changed the reference in the DeckMenu code to use the managed instance. 3) Fixed a problem with a default constructor param that would set RESOURCE_LOCKED on certain images. I noticed that the 'NEW' image was locking itself in the cache because no resource modes was being passed in, so the default constructor param was setting it to locked. This seems like a bad idea - if you're not explicitly locking a resource in the call, that shouldn't be the default behaviour. Also added an assert safeguard to catch the possibility of 'overlocking' an image resource, as this probably means the image fetching routine is being abused somehow. --- projects/mtg/include/WResourceManager.h | 5 ++++- projects/mtg/src/DeckMenu.cpp | 2 +- projects/mtg/src/WCachedResource.cpp | 19 +++++++++++++++++-- projects/mtg/src/WResourceManager.cpp | 3 --- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/projects/mtg/include/WResourceManager.h b/projects/mtg/include/WResourceManager.h index e0a4925f1..43e1940f9 100644 --- a/projects/mtg/include/WResourceManager.h +++ b/projects/mtg/include/WResourceManager.h @@ -8,6 +8,9 @@ const std::string kGenericCardID = "back"; const std::string kGenericCardThumbnailID = "back_thumb"; +const std::string kGenericCard("back.jpg"); +const std::string kGenericThumbCard("back_thumb.jpg"); + enum ENUM_WRES_INFO { WRES_UNLOCKED = 0, //Resource is unlocked. @@ -74,7 +77,7 @@ public: virtual JQuadPtr RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL) = 0; virtual JSample * RetrieveSample(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0; virtual JTexture * RetrieveTexture(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0; - virtual JQuadPtr RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0) = 0; + virtual JQuadPtr RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL, int id = 0) = 0; virtual JQuadPtr RetrieveTempQuad(const string& filename, int submode = CACHE_NORMAL) = 0; virtual hgeParticleSystemInfo * RetrievePSI(const string& filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL) = 0; virtual int RetrieveError() = 0; diff --git a/projects/mtg/src/DeckMenu.cpp b/projects/mtg/src/DeckMenu.cpp index 25098457f..c62fc8735 100644 --- a/projects/mtg/src/DeckMenu.cpp +++ b/projects/mtg/src/DeckMenu.cpp @@ -184,7 +184,7 @@ void DeckMenu::initMenuItems() mSelectionTargetY = selectionY = sY; //Grab a texture in VRAM. - pspIconsTexture = WResourceManager::Instance()->RetrieveTexture("iconspsp.png", RETRIEVE_LOCK); + pspIconsTexture = WResourceManager::Instance()->RetrieveTexture("iconspsp.png", RETRIEVE_MANAGE); char buf[512]; for (int i = 0; i < 8; i++) diff --git a/projects/mtg/src/WCachedResource.cpp b/projects/mtg/src/WCachedResource.cpp index ddc52983d..6166aa3c8 100644 --- a/projects/mtg/src/WCachedResource.cpp +++ b/projects/mtg/src/WCachedResource.cpp @@ -39,6 +39,9 @@ void WResource::deadbolt() void WResource::lock() { if (locks < WRES_MAX_LOCK) locks++; + // watch out for this - if the lock count increases beyond a reasonable threshold, + // someone is probably not releasing the texture correctly + assert(locks < 50); } void WResource::unlock(bool force) @@ -97,7 +100,19 @@ JQuadPtr WCachedTexture::GetQuad(float offX, float offY, float width, float heig if (width == 0.0f || width > static_cast (texture->mWidth)) width = static_cast (texture->mWidth); if (height == 0.0f || height > static_cast (texture->mHeight)) height = static_cast (texture->mHeight); - std::map::iterator iter = mTrackedQuads.find(resname); + // If we're fetching a card resource, but it's not available yet, we'll be attempting to get the Quad from the temporary back image. + // If that's the case, don't stash a separate tracked quad entry for each card name in the the Back/BackThumbnail's resource + string resource(resname); + if (mFilename == kGenericCard) + { + resource = kGenericCardID; + } + else if (mFilename == kGenericThumbCard) + { + resource = kGenericCardThumbnailID; + } + + std::map::iterator iter = mTrackedQuads.find(resource); if (iter != mTrackedQuads.end()) return iter->second; @@ -105,7 +120,7 @@ JQuadPtr WCachedTexture::GetQuad(float offX, float offY, float width, float heig //Update JQ's values to what we called this with. quad->SetTextureRect(offX, offY, width, height); - mTrackedQuads.insert(std::pair(resname, quad)); + mTrackedQuads.insert(std::pair(resource, quad)); return quad; } diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index 83bd055d2..33d5a1637 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -23,9 +23,6 @@ namespace const std::string kExtension_gbk(".gbk"); const std::string kExtension_font(".font"); - const std::string kGenericCard("back.jpg"); - const std::string kGenericThumbCard("back_thumb.jpg"); - // mutex meant for the cache map boost::mutex sCacheMutex; // mutex meant to protect against unthread-safe calls into JFileSystem, etc.