Added find, lock to GrContext & GrResourceCache interfaces
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 30 Aug 2012 11:06:31 +0000 (11:06 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 30 Aug 2012 11:06:31 +0000 (11:06 +0000)
https://codereview.appspot.com/6499052/

git-svn-id: http://skia.googlecode.com/svn/trunk@5343 2bbb7eff-a529-9590-31e7-b0007b416f81

include/gpu/GrContext.h
src/gpu/GrContext.cpp
src/gpu/GrResourceCache.cpp
src/gpu/GrResourceCache.h

index 7ee1dd3..d4657a7 100644 (file)
@@ -19,6 +19,7 @@
 #include "SkClipStack.h"
 
 class GrAutoScratchTexture;
+class GrCacheKey;
 class GrDrawState;
 class GrDrawTarget;
 class GrFontCache;
@@ -114,6 +115,12 @@ public:
                                     void* srcData, size_t rowBytes);
 
     /**
+     * Look for a texture that matches 'key' in the cache. If not found,
+     * return NULL.
+     */
+    GrTexture* findTexture(const GrCacheKey& key);
+
+    /**
      *  Search for an entry based on key and dimensions. If found, "lock" it and
      *  return it. The return value will be NULL if not found.
      *  Must be balanced with an unlockTexture() call.
@@ -173,6 +180,11 @@ public:
                                   ScratchTexMatch match);
 
     /**
+     * Make a texture un-purgeable in the cache
+     */
+    void lockTexture(GrTexture* texture);
+
+    /**
      *  When done with an entry, call unlockTexture(entry) on it, which returns
      *  it to the cache, where it may be purged.
      */
index 13ada9a..6a4a557 100644 (file)
@@ -207,6 +207,11 @@ void convolve_gaussian(GrDrawTarget* target,
 
 }
 
+
+GrTexture* GrContext::findTexture(const GrCacheKey& key) {
+    return static_cast<GrTexture*>(fTextureCache->find(key.key()));
+}
+
 GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
                                          const GrCacheData& cacheData,
                                          const GrTextureParams* params) {
@@ -484,6 +489,17 @@ void GrContext::addExistingTextureToCache(GrTexture* texture) {
     fTextureCache->unlock(texture->getCacheEntry());
 }
 
+void GrContext::lockTexture(GrTexture* texture) {
+
+    if (NULL == texture->getCacheEntry()) {
+        // not in the cache
+        GrAssert(0);
+        return;
+    }
+
+    fTextureCache->lock(texture->getCacheEntry());
+}
+
 void GrContext::unlockTexture(GrTexture* texture) {
     ASSERT_OWNED_RESOURCE(texture);
     GrAssert(NULL != texture->getCacheEntry());
index 777470b..ca69ce4 100644 (file)
@@ -184,6 +184,17 @@ void GrResourceCache::attachToHead(GrResourceEntry* entry,
     }
 }
 
+GrResource* GrResourceCache::find(const GrResourceKey& key) {
+    GrAutoResourceCacheValidate atcv(this);
+
+    GrResourceEntry* entry = fCache.find(key);
+    if (NULL == entry) {
+        return NULL;
+    }
+
+    return entry->fResource;
+}
+
 GrResource* GrResourceCache::findAndLock(const GrResourceKey& key,
                                          LockType type) {
     GrAutoResourceCacheValidate atcv(this);
@@ -256,7 +267,7 @@ void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
     GrAutoResourceCacheValidate atcv(this);
 
     this->internalDetach(entry, true);
-    fCache.remove(entry->fKey, entry);
+    fCache.remove(entry->key(), entry);
 
 #if GR_DEBUG
     fExclusiveList.addToHead(entry);
@@ -291,6 +302,19 @@ void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
     }
 }
 
+void GrResourceCache::lock(GrResourceEntry* entry) {
+    GrAutoResourceCacheValidate atcv(this);
+
+    GrAssert(entry);
+    GrAssert(fCache.find(entry->key()));
+
+    if (!entry->isLocked()) {
+        --fUnlockedEntryCount;
+    }
+
+    entry->lock();
+}
+
 void GrResourceCache::unlock(GrResourceEntry* entry) {
     GrAutoResourceCacheValidate atcv(this);
 
@@ -343,7 +367,7 @@ void GrResourceCache::purgeAsNeeded() {
                 GrResourceEntry* prev = iter.prev();
                 if (!entry->isLocked()) {
                     // remove from our cache
-                    fCache.remove(entry->fKey, entry);
+                    fCache.remove(entry->key(), entry);
 
                     // remove from our llist
                     this->internalDetach(entry, false);
index 5741e47..52e7c5d 100644 (file)
@@ -117,6 +117,27 @@ private:
     friend class GrContext;
 };
 
+
+class GrCacheKey {
+public:
+    GrCacheKey(const GrTextureDesc& desc, const GrResourceKey& key)
+        : fDesc(desc)
+        , fKey(key) {
+    }
+
+    void set(const GrTextureDesc& desc, const GrResourceKey& key) {
+        fDesc = desc;
+        fKey = key;
+    }
+
+    const GrTextureDesc& desc() const { return fDesc; }
+    const GrResourceKey& key() const { return fKey; }
+
+protected:
+    GrTextureDesc fDesc;
+    GrResourceKey fKey;
+};
+
 ///////////////////////////////////////////////////////////////////////////////
 
 class GrResourceEntry {
@@ -218,6 +239,12 @@ public:
     };
 
     /**
+     *  Search for an entry with the same Key. If found, return it.
+     *  If not found, return null.
+     */
+    GrResource* find(const GrResourceKey& key);
+
+    /**
      *  Search for an entry with the same Key. If found, "lock" it and return it.
      *  If not found, return null.
      */
@@ -270,6 +297,11 @@ public:
     void unlock(GrResourceEntry*);
 
     /**
+     * Make a resource un-purgeable.
+     */
+    void lock(GrResourceEntry* entry);
+
+    /**
      * Removes every resource in the cache that isn't locked.
      */
     void purgeAllUnlocked();