Use scratch keys for stencil buffers.
authorbsalomon <bsalomon@google.com>
Mon, 24 Nov 2014 15:05:15 +0000 (07:05 -0800)
committerCommit bot <commit-bot@chromium.org>
Mon, 24 Nov 2014 15:05:15 +0000 (07:05 -0800)
BUG=skia:2889

Review URL: https://codereview.chromium.org/747043004

bench/GrResourceCacheBench.cpp
include/gpu/GrContext.h
src/gpu/GrContext.cpp
src/gpu/GrGpu.cpp
src/gpu/GrStencilBuffer.cpp
src/gpu/GrStencilBuffer.h
src/gpu/gl/GrGpuGL.cpp

index e1ec90d..272e7bf 100644 (file)
@@ -14,9 +14,6 @@
 #include "GrContext.h"
 #include "GrGpu.h"
 #include "GrResourceCache2.h"
-#include "GrStencilBuffer.h"
-#include "GrTexture.h"
-#include "GrTexturePriv.h"
 #include "SkCanvas.h"
 
 enum {
@@ -24,17 +21,24 @@ enum {
     CACHE_SIZE_BYTES = 2 * 1024 * 1024,
 };
 
-class StencilResource : public GrGpuResource {
+class FooResource : public GrGpuResource {
 public:
-    SK_DECLARE_INST_COUNT(StencilResource);
-    StencilResource(GrGpu* gpu, int id)
+    SK_DECLARE_INST_COUNT(FooResource);
+    FooResource(GrGpu* gpu, int id)
         : INHERITED(gpu, false)
         , fID(id) {
         this->registerWithCache();
     }
 
     static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
-        return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
+        GrCacheID::Key key;
+        memset(&key, 0, sizeof(key));
+        key.fData32[0] = width;
+        key.fData32[1] = height;
+        key.fData32[2] = sampleCnt;
+        static int gType = GrResourceKey::GenerateResourceType();
+        static int gDomain = GrCacheID::GenerateDomain();
+        return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
     }
 
     int fID;
@@ -47,10 +51,10 @@ private:
     typedef GrGpuResource INHERITED;
 };
 
-class TextureResource : public GrGpuResource {
+class BarResource : public GrGpuResource {
 public:
-    SK_DECLARE_INST_COUNT(TextureResource);
-    TextureResource(GrGpu* gpu, int id)
+    SK_DECLARE_INST_COUNT(BarResource);
+    BarResource(GrGpu* gpu, int id)
         : INHERITED(gpu, false)
         , fID(id) {
         this->registerWithCache();
@@ -77,13 +81,13 @@ private:
     typedef GrGpuResource INHERITED;
 };
 
-static void get_stencil(int i, int* w, int* h, int* s) {
+static void get_foo_params(int i, int* w, int* h, int* s) {
     *w = i % 1024;
     *h = i * 2 % 1024;
-    *s = i % 1 == 0 ? 0 : 4;
+    *s = i % 2 == 0 ? 0 : 4;
 }
 
-static void get_texture_desc(int i, GrSurfaceDesc* desc) {
+static void get_bar_surf_desc(int i, GrSurfaceDesc* desc) {
     desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
     desc->fWidth  = i % 1024;
     desc->fHeight = i * 2 % 1024;
@@ -94,18 +98,18 @@ static void get_texture_desc(int i, GrSurfaceDesc* desc) {
 static void populate_cache(GrGpu* gpu, int resourceCount) {
     for (int i = 0; i < resourceCount; ++i) {
         int w, h, s;
-        get_stencil(i, &w, &h, &s);
-        GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
-        GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
+        get_foo_params(i, &w, &h, &s);
+        GrResourceKey key = FooResource::ComputeKey(w, h, s);
+        GrGpuResource* resource = SkNEW_ARGS(FooResource, (gpu, i));
         resource->cacheAccess().setContentKey(key);
         resource->unref();
     }
 
     for (int i = 0; i < resourceCount; ++i) {
         GrSurfaceDesc desc;
-        get_texture_desc(i, &desc);
-        GrResourceKey key =  TextureResource::ComputeKey(desc);
-        GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
+        get_bar_surf_desc(i, &desc);
+        GrResourceKey key =  BarResource::ComputeKey(desc);
+        GrGpuResource* resource = SkNEW_ARGS(BarResource, (gpu, i));
         resource->cacheAccess().setContentKey(key);
         resource->unref();
     }
@@ -115,28 +119,28 @@ static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
     // Benchmark find calls that succeed.
     {
         GrSurfaceDesc desc;
-        get_texture_desc(k, &desc);
-        GrResourceKey key = TextureResource::ComputeKey(desc);
+        get_bar_surf_desc(k, &desc);
+        GrResourceKey key = BarResource::ComputeKey(desc);
         SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
         if (!item) {
             SkFAIL("cache add does not work as expected");
             return;
         }
-        if (static_cast<TextureResource*>(item.get())->fID != k) {
+        if (static_cast<BarResource*>(item.get())->fID != k) {
             SkFAIL("cache add does not work as expected");
             return;
         }
     }
     {
         int w, h, s;
-        get_stencil(k, &w, &h, &s);
-        GrResourceKey key = StencilResource::ComputeKey(w, h, s);
+        get_foo_params(k, &w, &h, &s);
+        GrResourceKey key = FooResource::ComputeKey(w, h, s);
         SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
         if (!item) {
             SkFAIL("cache add does not work as expected");
             return;
         }
-        if (static_cast<TextureResource*>(item.get())->fID != k) {
+        if (static_cast<FooResource*>(item.get())->fID != k) {
             SkFAIL("cache add does not work as expected");
             return;
         }
@@ -145,9 +149,9 @@ static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
     // Benchmark also find calls that always fail.
     {
         GrSurfaceDesc desc;
-        get_texture_desc(k, &desc);
+        get_bar_surf_desc(k, &desc);
         desc.fHeight |= 1;
-        GrResourceKey key = TextureResource::ComputeKey(desc);
+        GrResourceKey key = BarResource::ComputeKey(desc);
         SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
         if (item) {
             SkFAIL("cache add does not work as expected");
@@ -156,9 +160,9 @@ static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
     }
     {
         int w, h, s;
-        get_stencil(k, &w, &h, &s);
+        get_foo_params(k, &w, &h, &s);
         h |= 1;
-        GrResourceKey key = StencilResource::ComputeKey(w, h, s);
+        GrResourceKey key = FooResource::ComputeKey(w, h, s);
         SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
         if (item) {
             SkFAIL("cache add does not work as expected");
index b83df45..c6dd2d7 100644 (file)
@@ -34,7 +34,6 @@ class GrPath;
 class GrPathRenderer;
 class GrResourceEntry;
 class GrResourceCache2;
-class GrStencilBuffer;
 class GrTestTarget;
 class GrTextContext;
 class GrTextureParams;
@@ -890,13 +889,6 @@ public:
     void addGpuTraceMarker(const GrGpuTraceMarker* marker);
     void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
 
-    /**
-     * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
-     * called to check the cache for a SB that matches an RT's criteria.
-     */
-    void addStencilBuffer(GrStencilBuffer* sb);
-    GrStencilBuffer* findAndRefStencilBuffer(int width, int height, int sampleCnt);
-
     GrPathRenderer* getPathRenderer(
                     const GrDrawTarget* target,
                     const GrDrawState*,
index 657e57d..607d3c3 100755 (executable)
@@ -24,7 +24,6 @@
 #include "GrPathUtils.h"
 #include "GrResourceCache2.h"
 #include "GrSoftwarePathRenderer.h"
-#include "GrStencilBuffer.h"
 #include "GrStencilAndCoverTextContext.h"
 #include "GrStrokeInfo.h"
 #include "GrSurfacePriv.h"
@@ -252,22 +251,6 @@ bool GrContext::isTextureInCache(const GrSurfaceDesc& desc,
     return fResourceCache2->hasContentKey(resourceKey);
 }
 
-void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
-    // TODO: Make GrStencilBuffers use the scratch mechanism rather than content keys.
-    ASSERT_OWNED_RESOURCE(sb);
-
-    GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
-                                                            sb->height(),
-                                                            sb->numSamples());
-    SkAssertResult(sb->cacheAccess().setContentKey(resourceKey));
-}
-
-GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) {
-    GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width, height, sampleCnt);
-    GrGpuResource* resource = this->findAndRefCachedResource(resourceKey);
-    return static_cast<GrStencilBuffer*>(resource);
-}
-
 static void stretch_image(void* dst,
                           int dstW,
                           int dstH,
index 2bda594..6b742c4 100644 (file)
 #include "GrContext.h"
 #include "GrDrawTargetCaps.h"
 #include "GrIndexBuffer.h"
+#include "GrResourceCache2.h"
 #include "GrStencilBuffer.h"
 #include "GrVertexBuffer.h"
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#define DEBUG_INVAL_BUFFER    0xdeadcafe
-#define DEBUG_INVAL_START_IDX -1
-
 GrGpu::GrGpu(GrContext* context)
     : fResetTimestamp(kExpiredTimestamp+1)
     , fResetBits(kAll_GrBackendState)
@@ -78,8 +76,9 @@ GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc,
 
 bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
     SkASSERT(NULL == rt->getStencilBuffer());
-    SkAutoTUnref<GrStencilBuffer> sb(
-        this->getContext()->findAndRefStencilBuffer(rt->width(), rt->height(), rt->numSamples()));
+    GrResourceKey sbKey = GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples());
+    SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
+        this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
     if (sb) {
         rt->setStencilBuffer(sb);
         bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
index 16b0150..5aa56e0 100644 (file)
 #include "GrGpu.h"
 #include "GrResourceCache2.h"
 
-void GrStencilBuffer::transferToCache() {
-    this->getGpu()->getContext()->addStencilBuffer(this);
-}
-
 namespace {
 // we should never have more than one stencil buffer with same combo of (width,height,samplecount)
 void gen_cache_id(int width, int height, int sampleCnt, GrCacheID* cacheID) {
-    static const GrCacheID::Domain gStencilBufferDomain = GrCacheID::GenerateDomain();
+    static const GrCacheID::Domain gStencilBufferDomain = GrResourceKey::ScratchDomain();
     GrCacheID::Key key;
     uint32_t* keyData = key.fData32;
     keyData[0] = width;
index 86fef50..187556b 100644 (file)
@@ -47,9 +47,6 @@ public:
                !fLastClipStackRect.contains(clipSpaceRect);
     }
 
-    // Places the sb in the cache. The cache takes a ref of the stencil buffer.
-    void transferToCache();
-
     static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
 
 protected:
@@ -60,6 +57,7 @@ protected:
         , fBits(bits)
         , fSampleCnt(sampleCnt)
         , fLastClipStackGenID(SkClipStack::kInvalidGenID) {
+        this->setScratchKey(ComputeKey(width, height, sampleCnt));
         fLastClipStackRect.setEmpty();
     }
 
index 9e1f754..c873c1d 100644 (file)
@@ -1121,8 +1121,7 @@ void inline get_stencil_rb_sizes(const GrGLInterface* gl,
 }
 }
 
-bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
-                                                 int width, int height) {
+bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
 
     // All internally created RTs are also textures. We don't create
     // SBs for a client's standalone RT (that is a RT that isn't also a texture).
@@ -1176,7 +1175,6 @@ bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
             sbID = 0; 
             if (this->attachStencilBufferToRenderTarget(sb, rt)) {
                 fLastSuccessfulStencilFmtIdx = sIdx;
-                sb->transferToCache();
                 rt->setStencilBuffer(sb);
                 return true;
             }