From b30dd1db1d914b85a691b4724713ba1b0f16cd6c Mon Sep 17 00:00:00 2001 From: joshualitt Date: Thu, 7 Jan 2016 12:52:05 -0800 Subject: [PATCH] Add guards to GrTextureProvider TBR=bsalomon@google.com BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1567983002 Review URL: https://codereview.chromium.org/1567983002 --- include/gpu/GrDrawContext.h | 2 +- include/gpu/GrTextureProvider.h | 17 +++++++---------- src/gpu/GrContext.cpp | 2 +- src/gpu/GrDrawContext.cpp | 5 ++++- src/gpu/GrResourceProvider.cpp | 3 ++- src/gpu/GrResourceProvider.h | 3 ++- src/gpu/GrTextureProvider.cpp | 35 ++++++++++++++++++++++++++++++++++- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/include/gpu/GrDrawContext.h b/include/gpu/GrDrawContext.h index 7ae6f5e..2f664ba 100644 --- a/include/gpu/GrDrawContext.h +++ b/include/gpu/GrDrawContext.h @@ -310,7 +310,7 @@ private: SkSurfaceProps fSurfaceProps; // In debug builds we guard against improper thread handling - mutable GrSingleOwner* fSingleOwner; + SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) }; #endif diff --git a/include/gpu/GrTextureProvider.h b/include/gpu/GrTextureProvider.h index 5635583..46f381d 100644 --- a/include/gpu/GrTextureProvider.h +++ b/include/gpu/GrTextureProvider.h @@ -11,6 +11,8 @@ #include "GrTexture.h" #include "SkImageFilter.h" +class GrSingleOwner; + class SK_API GrTextureProvider { public: /////////////////////////////////////////////////////////////////////////// @@ -42,15 +44,7 @@ public: } /** Finds a texture by unique key. If the texture is found it is ref'ed and returned. */ - GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key) { - GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); - if (resource) { - GrTexture* texture = static_cast(resource)->asTexture(); - SkASSERT(texture); - return texture; - } - return NULL; - } + GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key); /** * Determines whether a texture is associated with the unique key. If the texture is found it @@ -134,7 +128,7 @@ public: GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); protected: - GrTextureProvider(GrGpu* gpu, GrResourceCache* cache) : fCache(cache), fGpu(gpu) {} + GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner); /** * Assigns a unique key to a resource. If the key is associated with another resource that @@ -186,6 +180,9 @@ protected: private: GrResourceCache* fCache; GrGpu* fGpu; + + // In debug builds we guard against improper thread handling + SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) }; #endif diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 2bf887c..c37f31c 100644 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -85,7 +85,7 @@ void GrContext::initCommon(const GrContextOptions& options) { fCaps = SkRef(fGpu->caps()); fResourceCache = new GrResourceCache(fCaps); fResourceCache->setOverBudgetCallback(OverBudgetCB, this); - fResourceProvider = new GrResourceProvider(fGpu, fResourceCache); + fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner); fLayerCache.reset(new GrLayerCache(this)); diff --git a/src/gpu/GrDrawContext.cpp b/src/gpu/GrDrawContext.cpp index 01bd161..497af7c 100644 --- a/src/gpu/GrDrawContext.cpp +++ b/src/gpu/GrDrawContext.cpp @@ -57,7 +57,10 @@ GrDrawContext::GrDrawContext(GrDrawingManager* drawingMgr, , fDrawTarget(SkSafeRef(rt->getLastDrawTarget())) , fTextContext(nullptr) , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) - , fSingleOwner(singleOwner) { +#ifdef SK_DEBUG + , fSingleOwner(singleOwner) +#endif +{ SkDEBUGCODE(this->validate();) } diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp index a8bccfe..19fa1cf 100644 --- a/src/gpu/GrResourceProvider.cpp +++ b/src/gpu/GrResourceProvider.cpp @@ -19,7 +19,8 @@ GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); -GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache) : INHERITED(gpu, cache) { +GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner) + : INHERITED(gpu, cache, owner) { GR_DEFINE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey); fQuadIndexBufferKey = gQuadIndexBufferKey; } diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h index bffd572..783c7c7 100644 --- a/src/gpu/GrResourceProvider.h +++ b/src/gpu/GrResourceProvider.h @@ -17,6 +17,7 @@ class GrBatchAtlas; class GrIndexBuffer; class GrPath; class GrRenderTarget; +class GrSingleOwner; class GrStencilAttachment; class GrStrokeInfo; class GrVertexBuffer; @@ -35,7 +36,7 @@ class SkTypeface; */ class GrResourceProvider : protected GrTextureProvider { public: - GrResourceProvider(GrGpu* gpu, GrResourceCache* cache); + GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner); template T* findAndRefTByUniqueKey(const GrUniqueKey& key) { return static_cast(this->findAndRefResourceByUniqueKey(key)); diff --git a/src/gpu/GrTextureProvider.cpp b/src/gpu/GrTextureProvider.cpp index cb652f7..1c2f365 100644 --- a/src/gpu/GrTextureProvider.cpp +++ b/src/gpu/GrTextureProvider.cpp @@ -10,6 +10,10 @@ #include "GrTexturePriv.h" #include "GrResourceCache.h" #include "GrGpu.h" +#include "../private/GrSingleOwner.h" + +#define ASSERT_SINGLE_OWNER \ + SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) enum ScratchTextureFlags { kExact_ScratchTextureFlag = 0x1, @@ -17,8 +21,18 @@ enum ScratchTextureFlags { kNoCreate_ScratchTextureFlag = 0x4, }; +GrTextureProvider::GrTextureProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* singleOwner) + : fCache(cache) + , fGpu(gpu) +#ifdef SK_DEBUG + , fSingleOwner(singleOwner) +#endif + { +} + GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData, size_t rowBytes) { + ASSERT_SINGLE_OWNER if (this->isAbandoned()) { return nullptr; } @@ -44,11 +58,13 @@ GrTexture* GrTextureProvider::createTexture(const GrSurfaceDesc& desc, bool budg } GrTexture* GrTextureProvider::createApproxTexture(const GrSurfaceDesc& desc) { + ASSERT_SINGLE_OWNER return this->internalCreateApproxTexture(desc, 0); } GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& desc, uint32_t scratchFlags) { + ASSERT_SINGLE_OWNER if (this->isAbandoned()) { return nullptr; } @@ -62,6 +78,7 @@ GrTexture* GrTextureProvider::internalCreateApproxTexture(const GrSurfaceDesc& d GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) { + ASSERT_SINGLE_OWNER SkASSERT(!this->isAbandoned()); SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); @@ -108,6 +125,7 @@ GrTexture* GrTextureProvider::refScratchTexture(const GrSurfaceDesc& inDesc, GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) { + ASSERT_SINGLE_OWNER if (this->isAbandoned()) { return nullptr; } @@ -115,11 +133,13 @@ GrTexture* GrTextureProvider::wrapBackendTexture(const GrBackendTextureDesc& des } GrRenderTarget* GrTextureProvider::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) { + ASSERT_SINGLE_OWNER return this->isAbandoned() ? nullptr : fGpu->wrapBackendRenderTarget(desc, - kBorrow_GrWrapOwnership); + kBorrow_GrWrapOwnership); } void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuResource* resource) { + ASSERT_SINGLE_OWNER if (this->isAbandoned() || !resource) { return; } @@ -127,9 +147,22 @@ void GrTextureProvider::assignUniqueKeyToResource(const GrUniqueKey& key, GrGpuR } bool GrTextureProvider::existsResourceWithUniqueKey(const GrUniqueKey& key) const { + ASSERT_SINGLE_OWNER return this->isAbandoned() ? false : fCache->hasUniqueKey(key); } GrGpuResource* GrTextureProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) { + ASSERT_SINGLE_OWNER return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key); } + +GrTexture* GrTextureProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) { + ASSERT_SINGLE_OWNER + GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key); + if (resource) { + GrTexture* texture = static_cast(resource)->asTexture(); + SkASSERT(texture); + return texture; + } + return NULL; +} -- 2.7.4