Reland 6914 w/ fixes.



git-svn-id: http://skia.googlecode.com/svn/trunk@6916 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 79bc75d..7cee974 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -56,8 +56,34 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
+static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) {
+    // Our id includes the offset, width, and height so that bitmaps created by extractSubset()
+    // are unique.
+    uint32_t genID = bitmap.getGenerationID();
+    size_t offset = bitmap.pixelRefOffset();
+    int16_t width = static_cast<int16_t>(bitmap.width());
+    int16_t height = static_cast<int16_t>(bitmap.height());
+
+    GrCacheID::Key key;        
+    memcpy(key.fData8, &genID, 4);
+    memcpy(key.fData8 + 4, &width, 2);
+    memcpy(key.fData8 + 6, &height, 2);
+    memcpy(key.fData8 + 8, &offset, sizeof(size_t));
+    GR_STATIC_ASSERT(sizeof(key) >= 8 + sizeof(size_t));
+    static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain();
+    id->reset(gBitmapTextureDomain, key);
+}
+
+static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
+    desc->fFlags = kNone_GrTextureFlags;
+    desc->fWidth = bitmap.width();
+    desc->fHeight = bitmap.height();
+    desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
+    desc->fSampleCnt = 0;
+}
+
 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
-                                              uint64_t key,
+                                              bool cache,
                                               const GrTextureParams* params,
                                               const SkBitmap& origBitmap) {
     SkAutoLockPixels alp(origBitmap);
@@ -71,11 +97,7 @@
     const SkBitmap* bitmap = &origBitmap;
 
     GrTextureDesc desc;
-    desc.fWidth = bitmap->width();
-    desc.fHeight = bitmap->height();
-    desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
-
-    GrCacheData cacheData(key);
+    generate_bitmap_texture_desc(*bitmap, &desc);
 
     if (SkBitmap::kIndex8_Config == bitmap->config()) {
         // build_compressed_data doesn't do npot->pot expansion
@@ -91,31 +113,33 @@
             // our compressed data will be trimmed, so pass width() for its
             // "rowBytes", since they are the same now.
 
-            if (GrCacheData::kScratch_CacheID != key) {
-                return ctx->createTexture(params, desc, cacheData,
+            if (cache) {
+                GrCacheID cacheID;
+                generate_bitmap_cache_id(origBitmap, &cacheID);
+                return ctx->createTexture(params, desc, cacheID,
                                           storage.get(),
                                           bitmap->width());
             } else {
                 GrTexture* result = ctx->lockScratchTexture(desc,
-                                          GrContext::kExact_ScratchTexMatch);
+                                                            GrContext::kExact_ScratchTexMatch);
                 result->writePixels(0, 0, bitmap->width(),
                                     bitmap->height(), desc.fConfig,
                                     storage.get());
                 return result;
             }
-
         } else {
             origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
             // now bitmap points to our temp, which has been promoted to 32bits
             bitmap = &tmpBitmap;
+            desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
         }
     }
 
-    desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
-    if (GrCacheData::kScratch_CacheID != key) {
+    if (cache) {
         // This texture is likely to be used again so leave it in the cache
-        // but locked.
-        return ctx->createTexture(params, desc, cacheData,
+        GrCacheID cacheID;
+        generate_bitmap_cache_id(origBitmap, &cacheID);
+        return ctx->createTexture(params, desc, cacheID,
                                   bitmap->getPixels(),
                                   bitmap->rowBytes());
     } else {
@@ -124,8 +148,7 @@
         // 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.
-        GrTexture* result = ctx->lockScratchTexture(desc,
-                                         GrContext::kExact_ScratchTexMatch);
+        GrTexture* result = ctx->lockScratchTexture(desc, GrContext::kExact_ScratchTexMatch);
         result->writePixels(0, 0,
                             bitmap->width(), bitmap->height(),
                             desc.fConfig,
@@ -135,32 +158,37 @@
     }
 }
 
-///////////////////////////////////////////////////////////////////////////////
+bool GrIsBitmapInCache(const GrContext* ctx,
+                       const SkBitmap& bitmap,
+                       const GrTextureParams* params) {
+    GrCacheID cacheID;
+    generate_bitmap_cache_id(bitmap, &cacheID);
+
+    GrTextureDesc desc;
+    generate_bitmap_texture_desc(bitmap, &desc);
+    return ctx->isTextureInCache(desc, cacheID, params);
+}
 
 GrTexture* GrLockCachedBitmapTexture(GrContext* ctx,
                                      const SkBitmap& bitmap,
                                      const GrTextureParams* params) {
     GrTexture* result = NULL;
 
-    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;
+    bool cache = !bitmap.isVolatile();
+
+    if (cache) {
+        // If the bitmap isn't changing try to find a cached copy first.
+
+        GrCacheID cacheID;
+        generate_bitmap_cache_id(bitmap, &cacheID);
 
         GrTextureDesc desc;
-        desc.fWidth = bitmap.width();
-        desc.fHeight = bitmap.height();
-        desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
+        generate_bitmap_texture_desc(bitmap, &desc);
 
-        GrCacheData cacheData(key);
-
-        result = ctx->findTexture(desc, cacheData, params);
-        if (NULL == result) {
-            // didn't find a cached copy so create one
-            result = sk_gr_create_bitmap_texture(ctx, key, params, bitmap);
-        }
-    } else {
-        result = sk_gr_create_bitmap_texture(ctx, GrCacheData::kScratch_CacheID, params, bitmap);
+        result = ctx->findTexture(desc, cacheID, params);
+    }
+    if (NULL == result) {
+        result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap);
     }
     if (NULL == result) {
         GrPrintf("---- failed to create texture for cache [%d %d]\n",