Reland 6914 w/ fixes.



git-svn-id: http://skia.googlecode.com/svn/trunk@6916 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrCacheID.h b/include/gpu/GrCacheID.h
deleted file mode 100644
index e593d7e..0000000
--- a/include/gpu/GrCacheID.h
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright 2012 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef GrCacheID_DEFINED
-#define GrCacheID_DEFINED
-
-#include "GrTypes.h"
-
-///////////////////////////////////////////////////////////////////////////////
-#define GR_DECLARE_RESOURCE_CACHE_TYPE()                         \
-    static int8_t GetResourceType();
-
-#define GR_DEFINE_RESOURCE_CACHE_TYPE(ClassName)                 \
-    int8_t ClassName::GetResourceType() {                        \
-        static int8_t kResourceTypeID = 0;                       \
-        if (0 == kResourceTypeID) {                              \
-            kResourceTypeID = GrCacheID::GetNextResourceType();  \
-        }                                                        \
-        return kResourceTypeID;                                  \
-    }
-
-
-///////////////////////////////////////////////////////////////////////////////
-#define GR_DECLARE_RESOURCE_CACHE_DOMAIN(AccessorName)           \
-    static int8_t AccessorName();
-
-#define GR_DEFINE_RESOURCE_CACHE_DOMAIN(ClassName, AccessorName) \
-    int8_t ClassName::AccessorName() {                           \
-        static int8_t kDomainID = 0;                             \
-        if (0 == kDomainID) {                                    \
-            kDomainID = GrCacheID::GetNextDomain();              \
-        }                                                        \
-        return kDomainID;                                        \
-    }
-
-/**
- * The cache ID adds structure to the IDs used for caching GPU resources. It
- * is broken into three portions:
- *      the public portion - which is filled in by Skia clients
- *      the private portion - which is used by the cache (domain & type)
- *      the resource-specific portion - which is filled in by each GrResource-
- *              derived class.
- *
- * For the public portion each client of the cache makes up its own
- * unique-per-resource identifier (e.g., bitmap genID). A public ID of
- * 'kScratch_CacheID' indicates that the resource is a "scratch" resource.
- * When used to acquire a resource it indicates the cache user is
- * looking for a resource that matches a resource-subclass-specific set of
- * “dimensions” such as width, height, buffer size, or pixel config, but not
- * for particular resource contents (e.g., texel or vertex values). The public
- * IDs are unique within a private ID value but not necessarily across
- * private IDs.
- *
- * The domain portion identifies the cache client while the type field
- * indicates the resource type. When the public portion indicates that the
- * resource is a scratch resource, the domain field should be kUnrestricted
- * so that scratch resources can be recycled across domains.
- */
-class GrCacheID {
-public:
-    uint64_t     fPublicID;
-
-    uint32_t     fResourceSpecific32;
-
-    uint8_t      fDomain;
-private:
-    uint8_t      fResourceType;
-
-public:
-    uint16_t     fResourceSpecific16;
-
-    GrCacheID(uint8_t resourceType)
-        : fPublicID(kDefaultPublicCacheID)
-        , fDomain(GrCacheData::kScratch_ResourceDomain)
-        , fResourceType(resourceType) {
-    }
-
-    void toRaw(uint32_t v[4]);
-
-    uint8_t getResourceType() const { return fResourceType; }
-
-    /*
-     * Default value for public portion of GrCacheID
-     */
-    static const uint64_t kDefaultPublicCacheID = 0;
-
-    static const uint8_t kInvalid_ResourceType = 0;
-
-    static uint8_t    GetNextDomain();
-    static uint8_t    GetNextResourceType();
-
-
-};
-
-#endif // GrCacheID_DEFINED
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 06d2682..b1e9dd6 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -118,22 +118,21 @@
     // Textures
 
     /**
-     *  Create a new entry, based on the specified key and texture, and return
-     *  a "locked" texture. Must call be balanced with an unlockTexture() call.
+     * Create a new entry, based on the specified key and texture and return it.
      *
      * @param params    The texture 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.
      * @param desc      Description of the texture properties.
-     * @param cacheData Cache-specific properties (e.g., texture gen ID)
+     * @param cacheID 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.
      */
     GrTexture* createTexture(const GrTextureParams* params,
                              const GrTextureDesc& desc,
-                             const GrCacheData& cacheData,
+                             const GrCacheID& cacheID,
                              void* srcData, size_t rowBytes);
 
     /**
@@ -141,14 +140,14 @@
      *  return it. The return value will be NULL if not found.
      *
      *  @param desc     Description of the texture properties.
-     *  @param cacheData Cache-specific properties (e.g., texture gen ID)
+     *  @param cacheID Cache-specific properties (e.g., texture gen ID)
      *  @param params   The texture 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.
      */
     GrTexture* findTexture(const GrTextureDesc& desc,
-                           const GrCacheData& cacheData,
+                           const GrCacheID& cacheID,
                            const GrTextureParams* params);
     /**
      * Determines whether a texture is in the cache. If the texture is found it
@@ -156,7 +155,7 @@
      * the texture for deletion.
      */
     bool isTextureInCache(const GrTextureDesc& desc,
-                          const GrCacheData& cacheData,
+                          const GrCacheID& cacheID,
                           const GrTextureParams* params) const;
 
     /**
@@ -191,11 +190,10 @@
      * such an API will create gaps in the tiling pattern. This includes clamp
      * mode. (This may be addressed in a future update.)
      */
-    GrTexture* lockScratchTexture(const GrTextureDesc& desc,
-                                  ScratchTexMatch match);
+    GrTexture* lockScratchTexture(const GrTextureDesc&, ScratchTexMatch match);
 
     /**
-     *  When done with an entry, call unlockTexture(entry) on it, which returns
+     *  When done with an entry, call unlockScratchTexture(entry) on it, which returns
      *  it to the cache, where it may be purged.
      */
     void unlockScratchTexture(GrTexture* texture);
@@ -913,7 +911,7 @@
     void internalDrawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke);
 
     GrTexture* createResizedTexture(const GrTextureDesc& desc,
-                                    const GrCacheData& cacheData,
+                                    const GrCacheID& cacheID,
                                     void* srcData,
                                     size_t rowBytes,
                                     bool needsFiltering);
@@ -951,8 +949,7 @@
 
     GrAutoScratchTexture(GrContext* context,
                          const GrTextureDesc& desc,
-                         GrContext::ScratchTexMatch match =
-                            GrContext::kApprox_ScratchTexMatch)
+                         GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch)
       : fContext(NULL)
       , fTexture(NULL) {
       this->set(context, desc, match);
@@ -996,8 +993,7 @@
 
     GrTexture* set(GrContext* context,
                    const GrTextureDesc& desc,
-                   GrContext::ScratchTexMatch match =
-                        GrContext::kApprox_ScratchTexMatch) {
+                   GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) {
         this->reset();
 
         fContext = context;
diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h
index d41be11..36515b4 100644
--- a/include/gpu/GrTexture.h
+++ b/include/gpu/GrTexture.h
@@ -10,7 +10,6 @@
 #define GrTexture_DEFINED
 
 #include "GrSurface.h"
-#include "GrCacheID.h"
 
 class GrRenderTarget;
 class GrResourceKey;
@@ -20,8 +19,6 @@
 
 public:
     SK_DECLARE_INST_COUNT(GrTexture)
-    GR_DECLARE_RESOURCE_CACHE_TYPE()
-
     // from GrResource
     /**
      * Informational texture flags
@@ -130,15 +127,12 @@
 #else
     void validate() const {}
 #endif
-
     static GrResourceKey ComputeKey(const GrGpu* gpu,
-                                    const GrTextureParams* sampler,
+                                    const GrTextureParams* params,
                                     const GrTextureDesc& desc,
-                                    const GrCacheData& cacheData,
-                                    bool scratch);
-
+                                    const GrCacheID& cacheID);
+    static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
     static bool NeedsResizing(const GrResourceKey& key);
-    static bool IsScratchTexture(const GrResourceKey& key);
     static bool NeedsFiltering(const GrResourceKey& key);
 
 protected:
diff --git a/include/gpu/GrTypes.h b/include/gpu/GrTypes.h
index 21ae6de..bf599a0 100644
--- a/include/gpu/GrTypes.h
+++ b/include/gpu/GrTypes.h
@@ -459,44 +459,55 @@
 };
 
 /**
- * GrCacheData holds user-provided cache-specific data. It is used in
- * combination with the GrTextureDesc to construct a cache key for texture
- * resources.
+ * GrCacheID is used create and find cached GrResources (e.g. GrTextures). The ID has two parts:
+ * the domain and the key. Domains simply allow multiple clients to use 0-based indices as their
+ * cache key without colliding. The key uniquely identifies a GrResource within the domain.
+ * Users of the cache must obtain a domain via GenerateDomain().
  */
-struct GrCacheData {
-    /*
-     * Scratch textures should all have this value as their fClientCacheID
-     */
-    static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
+struct GrCacheID {
+public:
+    typedef uint8_t  Domain;
+
+    struct Key {
+        union {
+            uint8_t  fData8[16];
+            uint32_t fData32[4];
+            uint64_t fData64[2];
+        };
+    };
 
     /**
-      * 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;
+     * A default cache ID is invalid; a set method must be called before the object is used.
+     */
+    GrCacheID() { fDomain = kInvalid_Domain; }
 
-
-    // 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) {
+    /**
+     * Initialize the cache ID to a domain and key.
+     */
+    GrCacheID(Domain domain, const Key& key) {
+        GrAssert(kInvalid_Domain != domain); 
+        this->reset(domain, key);
     }
 
-    /**
-     * A user-provided texture ID. It should be unique to the texture data and
-     * does not need to take into account the width or height. Two textures
-     * with the same ID but different dimensions will not collide. This field
-     * is only relevant for textures that will be cached.
-     */
-    uint64_t               fClientCacheID;
+    void reset(Domain domain, const Key& key) {
+        fDomain = domain;
+        memcpy(&fKey, &key, sizeof(Key));
+    }
 
-    /**
-     * Allows cache clients to cluster their textures inside domains (e.g.,
-     * alpha clip masks). Only relevant for cached textures.
-     */
-    uint8_t                fResourceDomain;
+    /** Has this been initialized to a valid domain */
+    bool isValid() const { return kInvalid_Domain != fDomain; }
+
+    const Key& getKey() const { GrAssert(this->isValid()); return fKey; }
+    Domain getDomain() const { GrAssert(this->isValid()); return fDomain; }
+    
+    /** Creates a new unique ID domain. */
+    static Domain GenerateDomain();
+
+private:
+    Key             fKey;
+    Domain          fDomain;
+
+    static const Domain kInvalid_Domain = 0;
 };
 
 /**
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index 1e78b4a..45b0693 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -110,9 +110,6 @@
     class SkAutoCachedTexture; // used internally
 
 protected:
-    bool isBitmapInTextureCache(const SkBitmap& bitmap,
-                                const GrTextureParams& params) const;
-
     // overrides from SkDevice
     virtual bool onReadPixels(const SkBitmap& bitmap,
                               int x, int y,
diff --git a/include/gpu/SkGr.h b/include/gpu/SkGr.h
index 88ca08a..e7c2964 100644
--- a/include/gpu/SkGr.h
+++ b/include/gpu/SkGr.h
@@ -75,9 +75,9 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-GrTexture* GrLockCachedBitmapTexture(GrContext*,
-                                     const SkBitmap&,
-                                     const GrTextureParams*);
+bool GrIsBitmapInCache(const GrContext*, const SkBitmap&, const GrTextureParams*);
+
+GrTexture* GrLockCachedBitmapTexture(GrContext*, const SkBitmap&, const GrTextureParams*);
 
 void GrUnlockCachedBitmapTexture(GrTexture*);