Split cache-specific fields out of GrTextureDesc

http://codereview.appspot.com/6448143/



git-svn-id: http://skia.googlecode.com/svn/trunk@5065 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrCacheID.h b/include/gpu/GrCacheID.h
index 231304d..e6f5f75 100644
--- a/include/gpu/GrCacheID.h
+++ b/include/gpu/GrCacheID.h
@@ -75,7 +75,7 @@
 
     GrCacheID(uint8_t resourceType)
         : fPublicID(kDefaultPublicCacheID)
-        , fDomain(kUnrestricted_ResourceDomain)
+        , fDomain(GrCacheData::kScratch_ResourceDomain)
         , fResourceType(resourceType) {
     }
 
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 52dafd0..abe7453 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -125,12 +125,14 @@
      *                  for different wrap modes on GPUs with limited NPOT
      *                  texture support). NULL implies clamp wrap modes.
      * @param desc      Description of the texture properties.
+     * @param cacheData Cache-specific properties (e.g., texture gen ID)
      * @param srcData   Pointer to the pixel values.
      * @param rowBytes  The number of bytes between rows of the texture. Zero
      *                  implies tightly packed rows.
      */
     TextureCacheEntry createAndLockTexture(const GrTextureParams* params,
                                            const GrTextureDesc& desc,
+                                           const GrCacheData& cacheData,
                                            void* srcData, size_t rowBytes);
 
     /**
@@ -139,12 +141,14 @@
      *  Must be balanced with an unlockTexture() call.
      *
      *  @param desc     Description of the texture properties.
+     *  @param cacheData Cache-specific properties (e.g., texture gen ID)
      *  @param params   The tex params used to draw a texture may help determine
      *                  the cache entry used. (e.g. different versions may exist
      *                  for different wrap modes on GPUs with limited NPOT
      *                  texture support). NULL implies clamp wrap modes.
      */
     TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
+                                         const GrCacheData& cacheData,
                                          const GrTextureParams* params);
     /**
      * Determines whether a texture is in the cache. If the texture is found it
@@ -152,6 +156,7 @@
      * the texture for deletion.
      */
     bool isTextureInCache(const GrTextureDesc& desc,
+                          const GrCacheData& cacheData,
                           const GrTextureParams* params) const;
 
     /**
diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h
index 2ccd301..a1b7eab 100644
--- a/include/gpu/GrTexture.h
+++ b/include/gpu/GrTexture.h
@@ -16,17 +16,6 @@
 class GrResourceKey;
 class GrTextureParams;
 
-/*
- * All uncached textures should have this value as their fClientCacheID
- */
-static const uint64_t kUncached_CacheID = 0xAAAAAAAA;
-
-/*
- * Scratch textures should all have this value as their fClientCacheID
- */
-static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
-
-
 class GrTexture : public GrSurface {
 
 public:
@@ -166,6 +155,7 @@
     static GrResourceKey ComputeKey(const GrGpu* gpu,
                                     const GrTextureParams* sampler,
                                     const GrTextureDesc& desc,
+                                    const GrCacheData& cacheData,
                                     bool scratch);
 
     static bool NeedsResizing(const GrResourceKey& key);
diff --git a/include/gpu/GrTypes.h b/include/gpu/GrTypes.h
index a3f0059..9fbdb74 100644
--- a/include/gpu/GrTypes.h
+++ b/include/gpu/GrTypes.h
@@ -468,17 +468,6 @@
     kGrColorTableSize = 256 * 4 //sizeof(GrColor)
 };
 
-/*
- * Default value for fClientCacheID
- */
-static const uint64_t kDefault_CacheID = 0;
-
-/**
-  * All scratch resources should be Unrestricted so they can be used 
-  * by any domain.
-  */
-static const uint8_t kUnrestricted_ResourceDomain = 0;
-
 
 /**
  * Describes a texture to be created.
@@ -489,9 +478,7 @@
     , fWidth(0)
     , fHeight(0)
     , fConfig(kUnknown_GrPixelConfig)
-    , fSampleCnt(0)
-    , fClientCacheID(kDefault_CacheID)
-    , fResourceDomain(kUnrestricted_ResourceDomain) {
+    , fSampleCnt(0) {
     }
 
     GrTextureFlags         fFlags;  //!< bitfield of TextureFlags
@@ -512,6 +499,33 @@
      * max supportex count.
      */
     int                    fSampleCnt;
+};
+
+/**
+ * GrCacheData holds user-provided cache-specific data. It is used in 
+ * combination with the GrTextureDesc to construct a cache key for texture
+ * resources.
+ */
+struct GrCacheData {
+    /*
+     * Scratch textures should all have this value as their fClientCacheID
+     */
+    static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
+
+    /**
+      * Resources in the "scratch" domain can be used by any domain. All
+      * scratch textures will have this as their domain.
+      */
+    static const uint8_t kScratch_ResourceDomain = 0;
+
+
+    // No default constructor is provided since, if you are creating one
+    // of these, you should definitely have a key (or be using the scratch
+    // key).
+    GrCacheData(uint64_t key) 
+    : fClientCacheID(key)
+    , fResourceDomain(kScratch_ResourceDomain) {
+    }
 
     /**
      * A user-provided texture ID. It should be unique to the texture data and
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 6ea8531..2dc43bb 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -238,15 +238,17 @@
 }
 
 GrContext::TextureCacheEntry GrContext::findAndLockTexture(const GrTextureDesc& desc,
+                                                           const GrCacheData& cacheData,
                                                            const GrTextureParams* params) {
-    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
+    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
     return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
                                             GrResourceCache::kNested_LockType));
 }
 
 bool GrContext::isTextureInCache(const GrTextureDesc& desc,
+                                 const GrCacheData& cacheData,
                                  const GrTextureParams* params) const {
-    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
+    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
     return fTextureCache->hasKey(resourceKey);
 }
 
@@ -309,6 +311,7 @@
 GrContext::TextureCacheEntry GrContext::createAndLockTexture(
         const GrTextureParams* params,
         const GrTextureDesc& desc,
+        const GrCacheData& cacheData,
         void* srcData,
         size_t rowBytes) {
     SK_TRACE_EVENT0("GrContext::createAndLockTexture");
@@ -319,16 +322,16 @@
 
     TextureCacheEntry entry;
 
-    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
+    GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
 
     if (GrTexture::NeedsResizing(resourceKey)) {
         // The desired texture is NPOT and tiled but that isn't supported by 
         // the current hardware. Resize the texture to be a POT
         GrAssert(NULL != params);
-        TextureCacheEntry clampEntry = this->findAndLockTexture(desc, NULL);
+        TextureCacheEntry clampEntry = this->findAndLockTexture(desc, cacheData, NULL);
 
         if (NULL == clampEntry.texture()) {
-            clampEntry = this->createAndLockTexture(NULL, desc, srcData, rowBytes);
+            clampEntry = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
             GrAssert(NULL != clampEntry.texture());
             if (NULL == clampEntry.texture()) {
                 return entry;
@@ -411,7 +414,7 @@
                                                 const GrTextureDesc& inDesc,
                                                 ScratchTexMatch match) {
     GrTextureDesc desc = inDesc;
-    desc.fClientCacheID = kScratch_CacheID;
+    GrCacheData cacheData(GrCacheData::kScratch_CacheID);
 
     if (kExact_ScratchTexMatch != match) {
         // bin by pow2 with a reasonable min
@@ -427,7 +430,7 @@
     bool doubledH = false;
 
     do {
-        GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
+        GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
         entry = fTextureCache->findAndLock(key,
                                            GrResourceCache::kNested_LockType);
         // if we miss, relax the fit of the flags...
@@ -462,6 +465,7 @@
         if (NULL != texture) {
             GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
                                                       texture->desc(),
+                                                      cacheData,
                                                       true);
             entry = fTextureCache->createAndLock(key, texture);
         }
@@ -482,8 +486,12 @@
         return;
     }
 
+    // 'texture' is a scratch texture returning to the fold
+    GrCacheData cacheData(GrCacheData::kScratch_CacheID);
+
     GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
                                               texture->desc(),
+                                              cacheData,
                                               true);
     fTextureCache->attach(key, texture);
 }
@@ -510,7 +518,6 @@
                                             void* srcData,
                                             size_t rowBytes) {
     GrTextureDesc descCopy = descIn;
-    descCopy.fClientCacheID = kUncached_CacheID;
     return fGpu->createTexture(descCopy, srcData, rowBytes);
 }
 
diff --git a/src/gpu/GrStencilBuffer.cpp b/src/gpu/GrStencilBuffer.cpp
index 07ee661..53a4a65 100644
--- a/src/gpu/GrStencilBuffer.cpp
+++ b/src/gpu/GrStencilBuffer.cpp
@@ -66,7 +66,7 @@
                             GrCacheID* cacheID) {
     cacheID->fPublicID = GrCacheID::kDefaultPublicCacheID;
     cacheID->fResourceSpecific32 = width | (height << 16);
-    cacheID->fDomain = kUnrestricted_ResourceDomain;
+    cacheID->fDomain = GrCacheData::kScratch_ResourceDomain;
 
     GrAssert(sampleCnt >= 0 && sampleCnt < 256);
     cacheID->fResourceSpecific16 = sampleCnt << 8;
diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp
index 208bd2e..d2e2736 100644
--- a/src/gpu/GrTexture.cpp
+++ b/src/gpu/GrTexture.cpp
@@ -138,20 +138,21 @@
 void gen_texture_key_values(const GrGpu* gpu,
                             const GrTextureParams* params,
                             const GrTextureDesc& desc,
+                            const GrCacheData& cacheData,
                             bool scratch,
                             GrCacheID* cacheID) {
 
-    uint64_t clientKey = desc.fClientCacheID;
+    uint64_t clientKey = cacheData.fClientCacheID;
 
     if (scratch) {
         // Instead of a client-provided key of the texture contents
         // we create a key from the descriptor.
-        GrAssert(kScratch_CacheID == clientKey);
+        GrAssert(GrCacheData::kScratch_CacheID == clientKey);
         clientKey = (desc.fFlags << 8) | ((uint64_t) desc.fConfig << 32);
     }
 
     cacheID->fPublicID = clientKey;
-    cacheID->fDomain = desc.fResourceDomain;
+    cacheID->fDomain = cacheData.fResourceDomain;
 
     // we assume we only need 16 bits of width and height
     // assert that texture creation will fail anyway if this assumption
@@ -184,9 +185,10 @@
 GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
                                     const GrTextureParams* params,
                                     const GrTextureDesc& desc,
+                                    const GrCacheData& cacheData,
                                     bool scratch) {
     GrCacheID id(GrTexture::GetResourceType());
-    gen_texture_key_values(gpu, params, desc, scratch, &id);
+    gen_texture_key_values(gpu, params, desc, cacheData, scratch, &id);
 
     uint32_t v[4];
     id.toRaw(v);
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 2d6d3a2..496c096 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1907,9 +1907,10 @@
     desc.fWidth = bitmap.width();
     desc.fHeight = bitmap.height();
     desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
-    desc.fClientCacheID = key;
 
-    return this->context()->isTextureInCache(desc, &params);
+    GrCacheData cacheData(key);
+
+    return this->context()->isTextureInCache(desc, cacheData, &params);
 }
 
 
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index f6ee7ef..8240eb1 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -75,7 +75,8 @@
     desc.fWidth = bitmap->width();
     desc.fHeight = bitmap->height();
     desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
-    desc.fClientCacheID = key;
+
+    GrCacheData cacheData(key);
 
     if (SkBitmap::kIndex8_Config == bitmap->config()) {
         // build_compressed_data doesn't do npot->pot expansion
@@ -91,8 +92,9 @@
             // our compressed data will be trimmed, so pass width() for its
             // "rowBytes", since they are the same now.
             
-            if (kUncached_CacheID != key) {
-                return ctx->createAndLockTexture(params, desc, storage.get(),
+            if (GrCacheData::kScratch_CacheID != key) {
+                return ctx->createAndLockTexture(params, desc, cacheData, 
+                                                 storage.get(),
                                                  bitmap->width());
             } else {
                 entry = ctx->lockScratchTexture(desc,
@@ -111,11 +113,18 @@
     }
 
     desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
-    if (kUncached_CacheID != key) {
-        return ctx->createAndLockTexture(params, desc,
+    if (GrCacheData::kScratch_CacheID != key) {
+        // This texture is likely to be used again so leave it in the cache
+        // but locked.
+        return ctx->createAndLockTexture(params, desc, cacheData,
                                          bitmap->getPixels(),
                                          bitmap->rowBytes());
     } else {
+        // This texture is unlikely to be used again (in its present form) so 
+        // just use a scratch texture. This will remove the texture from the 
+        // cache so no one else can find it. Additionally, once unlocked, the 
+        // scratch texture will go to the end of the list for purging so will 
+        // likely be available for this volatile bitmap the next time around.
         entry = ctx->lockScratchTexture(desc,
                                         GrContext::kExact_ScratchTexMatch);
         entry.texture()->writePixels(0, 0,
@@ -135,6 +144,7 @@
     GrContext::TextureCacheEntry entry;
 
     if (!bitmap.isVolatile()) {
+        // If the bitmap isn't changing try to find a cached copy first
         uint64_t key = bitmap.getGenerationID();
         key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
 
@@ -142,14 +152,15 @@
         desc.fWidth = bitmap.width();
         desc.fHeight = bitmap.height();
         desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
-        desc.fClientCacheID = key;
 
-        entry = ctx->findAndLockTexture(desc, params);
+        GrCacheData cacheData(key);
+
+        entry = ctx->findAndLockTexture(desc, cacheData, params);
         if (NULL == entry.texture()) {
             entry = sk_gr_create_bitmap_texture(ctx, key, params, bitmap);
         }
     } else {
-        entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID, params, bitmap);
+        entry = sk_gr_create_bitmap_texture(ctx, GrCacheData::kScratch_CacheID, params, bitmap);
     }
     if (NULL == entry.texture()) {
         GrPrintf("---- failed to create texture for cache [%d %d]\n",
diff --git a/src/gpu/gl/GrGLRenderTarget.cpp b/src/gpu/gl/GrGLRenderTarget.cpp
index 4193519..16d7f1b 100644
--- a/src/gpu/gl/GrGLRenderTarget.cpp
+++ b/src/gpu/gl/GrGLRenderTarget.cpp
@@ -28,15 +28,13 @@
 namespace {
 GrTextureDesc MakeDesc(GrTextureFlags flags,
                        int width, int height,
-                       GrPixelConfig config, int sampleCnt,
-                       uint64_t clientCacheID) {
+                       GrPixelConfig config, int sampleCnt) {
     GrTextureDesc temp;
     temp.fFlags = flags;
     temp.fWidth = width;
     temp.fHeight = height;
     temp.fConfig = config;
     temp.fSampleCnt = sampleCnt;
-    temp.fClientCacheID = clientCacheID;
     return temp;
 }
 
@@ -51,8 +49,7 @@
                 texture,
                 MakeDesc(kNone_GrTextureFlags,
                          viewport.fWidth, viewport.fHeight,
-                         desc.fConfig, desc.fSampleCnt, 
-                         kDefault_CacheID)) {
+                         desc.fConfig, desc.fSampleCnt)) {
     GrAssert(NULL != texID);
     GrAssert(NULL != texture);
     // FBO 0 can't also be a texture, right?
@@ -73,8 +70,7 @@
                 NULL,
                 MakeDesc(kNone_GrTextureFlags,
                          viewport.fWidth, viewport.fHeight,
-                         desc.fConfig, desc.fSampleCnt, 
-                         kDefault_CacheID)) {
+                         desc.fConfig, desc.fSampleCnt)) {
     this->init(desc, viewport, NULL);
 }
 
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index 26018bf..c13bade 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -1014,7 +1014,6 @@
     glTexDesc.fHeight = desc.fHeight;
     glTexDesc.fConfig = desc.fConfig;
     glTexDesc.fSampleCnt = desc.fSampleCnt;
-    glTexDesc.fClientCacheID = desc.fClientCacheID;
 
     glTexDesc.fOwnsID = true;