From 91175f19664a62851da4ca4e0984a7c7c45b258f Mon Sep 17 00:00:00 2001 From: bsalomon Date: Mon, 24 Nov 2014 07:05:15 -0800 Subject: [PATCH] Use scratch keys for stencil buffers. BUG=skia:2889 Review URL: https://codereview.chromium.org/747043004 --- bench/GrResourceCacheBench.cpp | 62 ++++++++++++++++++++++-------------------- include/gpu/GrContext.h | 8 ------ src/gpu/GrContext.cpp | 17 ------------ src/gpu/GrGpu.cpp | 9 +++--- src/gpu/GrStencilBuffer.cpp | 6 +--- src/gpu/GrStencilBuffer.h | 4 +-- src/gpu/gl/GrGpuGL.cpp | 4 +-- 7 files changed, 40 insertions(+), 70 deletions(-) diff --git a/bench/GrResourceCacheBench.cpp b/bench/GrResourceCacheBench.cpp index e1ec90d..272e7bf 100644 --- a/bench/GrResourceCacheBench.cpp +++ b/bench/GrResourceCacheBench.cpp @@ -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 item(cache->findAndRefContentResource(key)); if (!item) { SkFAIL("cache add does not work as expected"); return; } - if (static_cast(item.get())->fID != k) { + if (static_cast(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 item(cache->findAndRefContentResource(key)); if (!item) { SkFAIL("cache add does not work as expected"); return; } - if (static_cast(item.get())->fID != k) { + if (static_cast(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 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 item(cache->findAndRefContentResource(key)); if (item) { SkFAIL("cache add does not work as expected"); diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h index b83df45..c6dd2d7 100644 --- a/include/gpu/GrContext.h +++ b/include/gpu/GrContext.h @@ -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*, diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 657e57d..607d3c3 100755 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -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(resource); -} - static void stretch_image(void* dst, int dstW, int dstH, diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp index 2bda594..6b742c4 100644 --- a/src/gpu/GrGpu.cpp +++ b/src/gpu/GrGpu.cpp @@ -13,14 +13,12 @@ #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 sb( - this->getContext()->findAndRefStencilBuffer(rt->width(), rt->height(), rt->numSamples())); + GrResourceKey sbKey = GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples()); + SkAutoTUnref sb(static_cast( + this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey))); if (sb) { rt->setStencilBuffer(sb); bool attached = this->attachStencilBufferToRenderTarget(sb, rt); diff --git a/src/gpu/GrStencilBuffer.cpp b/src/gpu/GrStencilBuffer.cpp index 16b0150..5aa56e0 100644 --- a/src/gpu/GrStencilBuffer.cpp +++ b/src/gpu/GrStencilBuffer.cpp @@ -12,14 +12,10 @@ #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; diff --git a/src/gpu/GrStencilBuffer.h b/src/gpu/GrStencilBuffer.h index 86fef50..187556b 100644 --- a/src/gpu/GrStencilBuffer.h +++ b/src/gpu/GrStencilBuffer.h @@ -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(); } diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp index 9e1f754..c873c1d 100644 --- a/src/gpu/gl/GrGpuGL.cpp +++ b/src/gpu/gl/GrGpuGL.cpp @@ -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; } -- 2.7.4