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/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;