From b1807129a62889c6bb3fa600f177e3be52c07f71 Mon Sep 17 00:00:00 2001 From: Robert Phillips Date: Tue, 6 Oct 2020 16:44:18 -0400 Subject: [PATCH] Centralize CreateLazyView helper in GrThreadSafeUniquelyKeyedProxyViewCache We'll also be needing this helper for HW-generated blur mask caching Bug: 1108408 Change-Id: I60d91ae8864239f0cf68830d0a5b4266d27545d3 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323109 Reviewed-by: Adlai Holler Commit-Queue: Robert Phillips --- ...rThreadSafeUniquelyKeyedProxyViewCache.cpp | 55 +++++++++++++ .../GrThreadSafeUniquelyKeyedProxyViewCache.h | 12 +++ src/gpu/effects/GrRRectBlurEffect.fp | 72 +++-------------- .../effects/generated/GrRRectBlurEffect.cpp | 79 ++++--------------- tests/GrThreadSafeViewCacheTest.cpp | 63 ++------------- 5 files changed, 99 insertions(+), 182 deletions(-) diff --git a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp index d467d2b3f7..898ad8e3fc 100644 --- a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp +++ b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp @@ -7,6 +7,10 @@ #include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h" +#include "include/gpu/GrDirectContext.h" +#include "src/gpu/GrContextPriv.h" +#include "src/gpu/GrProxyProvider.h" +#include "src/gpu/GrRenderTargetContext.h" #include "src/gpu/GrResourceCache.h" GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache() @@ -222,3 +226,54 @@ void GrThreadSafeUniquelyKeyedProxyViewCache::remove(const GrUniqueKey& key) { this->recycleEntry(tmp); } } + +std::tuple> +GrThreadSafeUniquelyKeyedProxyViewCache::CreateLazyView(GrDirectContext* dContext, + SkISize dimensions, + GrColorType origCT, + GrSurfaceOrigin origin) { + GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); + + constexpr int kSampleCnt = 1; + auto [newCT, format] = GrRenderTargetContext::GetFallbackColorTypeAndFormat( + dContext, origCT, kSampleCnt); + + if (newCT == GrColorType::kUnknown) { + return {GrSurfaceProxyView(nullptr), nullptr}; + } + + sk_sp trampoline(new Trampoline); + + GrProxyProvider::TextureInfo texInfo{ GrMipMapped::kNo, GrTextureType::k2D }; + + sk_sp proxy = proxyProvider->createLazyRenderTargetProxy( + [trampoline]( + GrResourceProvider* resourceProvider, + const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult { + if (!resourceProvider || !trampoline->fProxy || + !trampoline->fProxy->isInstantiated()) { + return GrSurfaceProxy::LazyCallbackResult(nullptr, true); + } + + SkASSERT(!trampoline->fProxy->peekTexture()->getUniqueKey().isValid()); + return GrSurfaceProxy::LazyCallbackResult( + sk_ref_sp(trampoline->fProxy->peekTexture())); + }, + format, + dimensions, + kSampleCnt, + GrInternalSurfaceFlags::kNone, + &texInfo, + GrMipmapStatus::kNotAllocated, + SkBackingFit::kExact, + SkBudgeted::kYes, + GrProtected::kNo, + /* wrapsVkSecondaryCB */ false, + GrSurfaceProxy::UseAllocator::kYes); + + // TODO: It seems like this 'newCT' usage should be 'origCT' but this is + // what GrRenderTargetContext::MakeWithFallback does + GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, newCT); + + return {{std::move(proxy), origin, swizzle}, std::move(trampoline)}; +} diff --git a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h index f6f9d70424..e0bac814ba 100644 --- a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h +++ b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h @@ -95,6 +95,18 @@ public: void remove(const GrUniqueKey&) SK_EXCLUDES(fSpinLock); + // To allow gpu-created resources to have priority, we pre-emptively place a lazy proxy + // in the thread-safe cache (with findOrAdd). The Trampoline object allows that lazy proxy to + // be instantiated with some later generated rendering result. + class Trampoline : public SkRefCnt { + public: + sk_sp fProxy; + }; + + static std::tuple> CreateLazyView(GrDirectContext*, + SkISize dimensions, + GrColorType, + GrSurfaceOrigin); private: struct Entry { Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {} diff --git a/src/gpu/effects/GrRRectBlurEffect.fp b/src/gpu/effects/GrRRectBlurEffect.fp index bbe5d1c9d0..b5316f1d81 100644 --- a/src/gpu/effects/GrRRectBlurEffect.fp +++ b/src/gpu/effects/GrRRectBlurEffect.fp @@ -73,17 +73,13 @@ uniform half blurRadius; builder.finish(); } - class Trampoline : public SkRefCnt { - public: - sk_sp fProxy; - }; - - static bool fillin_view_on_gpu(GrDirectContext* dContext, - const GrSurfaceProxyView& lazyView, - sk_sp trampoline, - const SkRRect& rrectToDraw, - const SkISize& dimensions, - float xformedSigma) { + static bool fillin_view_on_gpu( + GrDirectContext* dContext, + const GrSurfaceProxyView& lazyView, + sk_sp trampoline, + const SkRRect& rrectToDraw, + const SkISize& dimensions, + float xformedSigma) { std::unique_ptr rtc = GrRenderTargetContext::MakeWithFallback( dContext, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1, GrMipmapped::kNo, GrProtected::kNo, kBlurredRRectMaskOrigin); @@ -250,56 +246,6 @@ uniform half blurRadius; return view; } - static std::tuple> create_lazy_view( - GrDirectContext* dContext, - SkISize dimensions) { - - GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); - - constexpr int kSampleCnt = 1; - auto [ct, format] = GrRenderTargetContext::GetFallbackColorTypeAndFormat( - dContext, GrColorType::kAlpha_8, kSampleCnt); - - if (ct == GrColorType::kUnknown) { - return {GrSurfaceProxyView(nullptr), nullptr}; - } - - sk_sp trampoline(new Trampoline); - - GrProxyProvider::TextureInfo texInfo { GrMipMapped::kNo, GrTextureType::k2D }; - - sk_sp proxy = proxyProvider->createLazyRenderTargetProxy( - [trampoline] (GrResourceProvider* resourceProvider, - const GrSurfaceProxy::LazySurfaceDesc&) - -> GrSurfaceProxy::LazyCallbackResult { - if (!resourceProvider || !trampoline->fProxy || - !trampoline->fProxy->isInstantiated()) { - return GrSurfaceProxy::LazyCallbackResult(nullptr, true); - } - - SkASSERT(!trampoline->fProxy->peekTexture()->getUniqueKey().isValid()); - return GrSurfaceProxy::LazyCallbackResult( - sk_ref_sp(trampoline->fProxy->peekTexture())); - }, - format, - dimensions, - kSampleCnt, - GrInternalSurfaceFlags::kNone, - &texInfo, - GrMipmapStatus::kNotAllocated, - SkBackingFit::kExact, - SkBudgeted::kYes, - GrProtected::kNo, - /* wrapsVkSecondaryCB */ false, - GrSurfaceProxy::UseAllocator::kYes); - - // TODO: It seems like this 'ct' usage should be 'GrColorType::kAlpha_8' but this is - // what GrRenderTargetContext::MakeWithFallback does - GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, ct); - - return {{std::move(proxy), kBlurredRRectMaskOrigin, swizzle}, std::move(trampoline)}; - } - static std::unique_ptr find_or_create_rrect_blur_mask_fp( GrRecordingContext* rContext, const SkRRect& rrectToDraw, @@ -323,7 +269,9 @@ uniform half blurRadius; if (GrDirectContext* dContext = rContext->asDirectContext()) { // The gpu thread gets priority over the recording threads. If the gpu thread is first, // it crams a lazy proxy into the cache and then fills it in later. - auto [lazyView, trampoline] = create_lazy_view(dContext, dimensions); + auto[lazyView, trampoline] = GrThreadSafeUniquelyKeyedProxyViewCache::CreateLazyView( + dContext, dimensions, GrColorType::kAlpha_8, + kBlurredRRectMaskOrigin); if (!lazyView) { return nullptr; } diff --git a/src/gpu/effects/generated/GrRRectBlurEffect.cpp b/src/gpu/effects/generated/GrRRectBlurEffect.cpp index d44aa63d13..5cbb7e5a36 100644 --- a/src/gpu/effects/generated/GrRRectBlurEffect.cpp +++ b/src/gpu/effects/generated/GrRRectBlurEffect.cpp @@ -48,17 +48,13 @@ static void make_blurred_rrect_key(GrUniqueKey* key, builder.finish(); } -class Trampoline : public SkRefCnt { -public: - sk_sp fProxy; -}; - -static bool fillin_view_on_gpu(GrDirectContext* dContext, - const GrSurfaceProxyView& lazyView, - sk_sp trampoline, - const SkRRect& rrectToDraw, - const SkISize& dimensions, - float xformedSigma) { +static bool fillin_view_on_gpu( + GrDirectContext* dContext, + const GrSurfaceProxyView& lazyView, + sk_sp trampoline, + const SkRRect& rrectToDraw, + const SkISize& dimensions, + float xformedSigma) { std::unique_ptr rtc = GrRenderTargetContext::MakeWithFallback( dContext, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1, GrMipmapped::kNo, GrProtected::kNo, kBlurredRRectMaskOrigin); @@ -228,54 +224,6 @@ static GrSurfaceProxyView create_mask_on_cpu(GrRecordingContext* rContext, return view; } -static std::tuple> create_lazy_view(GrDirectContext* dContext, - SkISize dimensions) { - GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); - - constexpr int kSampleCnt = 1; - auto[ct, format] = GrRenderTargetContext::GetFallbackColorTypeAndFormat( - dContext, GrColorType::kAlpha_8, kSampleCnt); - - if (ct == GrColorType::kUnknown) { - return {GrSurfaceProxyView(nullptr), nullptr}; - } - - sk_sp trampoline(new Trampoline); - - GrProxyProvider::TextureInfo texInfo{GrMipMapped::kNo, GrTextureType::k2D}; - - sk_sp proxy = proxyProvider->createLazyRenderTargetProxy( - [trampoline]( - GrResourceProvider* resourceProvider, - const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult { - if (!resourceProvider || !trampoline->fProxy || - !trampoline->fProxy->isInstantiated()) { - return GrSurfaceProxy::LazyCallbackResult(nullptr, true); - } - - SkASSERT(!trampoline->fProxy->peekTexture()->getUniqueKey().isValid()); - return GrSurfaceProxy::LazyCallbackResult( - sk_ref_sp(trampoline->fProxy->peekTexture())); - }, - format, - dimensions, - kSampleCnt, - GrInternalSurfaceFlags::kNone, - &texInfo, - GrMipmapStatus::kNotAllocated, - SkBackingFit::kExact, - SkBudgeted::kYes, - GrProtected::kNo, - /* wrapsVkSecondaryCB */ false, - GrSurfaceProxy::UseAllocator::kYes); - - // TODO: It seems like this 'ct' usage should be 'GrColorType::kAlpha_8' but this is - // what GrRenderTargetContext::MakeWithFallback does - GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, ct); - - return {{std::move(proxy), kBlurredRRectMaskOrigin, swizzle}, std::move(trampoline)}; -} - static std::unique_ptr find_or_create_rrect_blur_mask_fp( GrRecordingContext* rContext, const SkRRect& rrectToDraw, @@ -299,7 +247,8 @@ static std::unique_ptr find_or_create_rrect_blur_mask_fp( if (GrDirectContext* dContext = rContext->asDirectContext()) { // The gpu thread gets priority over the recording threads. If the gpu thread is first, // it crams a lazy proxy into the cache and then fills it in later. - auto[lazyView, trampoline] = create_lazy_view(dContext, dimensions); + auto[lazyView, trampoline] = GrThreadSafeUniquelyKeyedProxyViewCache::CreateLazyView( + dContext, dimensions, GrColorType::kAlpha_8, kBlurredRRectMaskOrigin); if (!lazyView) { return nullptr; } @@ -427,18 +376,18 @@ half2 texCoord = translatedFragPos / proxyDims;)SkSL", args.fUniformHandler->getUniformCStr(proxyRectVar), args.fUniformHandler->getUniformCStr(blurRadiusVar), args.fUniformHandler->getUniformCStr(cornerRadiusVar)); - SkString _sample19396 = this->invokeChild(0, args); + SkString _sample17246 = this->invokeChild(0, args); fragBuilder->codeAppendf( R"SkSL( half4 inputColor = %s;)SkSL", - _sample19396.c_str()); - SkString _coords19444("float2(texCoord)"); - SkString _sample19444 = this->invokeChild(1, args, _coords19444.c_str()); + _sample17246.c_str()); + SkString _coords17294("float2(texCoord)"); + SkString _sample17294 = this->invokeChild(1, args, _coords17294.c_str()); fragBuilder->codeAppendf( R"SkSL( %s = inputColor * %s; )SkSL", - args.fOutputColor, _sample19444.c_str()); + args.fOutputColor, _sample17294.c_str()); } private: diff --git a/tests/GrThreadSafeViewCacheTest.cpp b/tests/GrThreadSafeViewCacheTest.cpp index 751f0d99a8..a218b38854 100644 --- a/tests/GrThreadSafeViewCacheTest.cpp +++ b/tests/GrThreadSafeViewCacheTest.cpp @@ -288,25 +288,15 @@ public: } private: - // In the gpu-creation case, we need to pre-emptively place a lazy proxy in the shared - // cache. This object allows that lazy proxy to be instantiated with some rendering result - // generated after the fact. - class Trampoline : public SkRefCnt { - public: - sk_sp fProxy; - }; - static GrSurfaceProxyView AccessCachedView(GrRecordingContext*, GrThreadSafeUniquelyKeyedProxyViewCache*, int wh, bool failLookup, bool failFillingIn, int id, Stats*); static GrSurfaceProxyView CreateViewOnCpu(GrRecordingContext*, int wh, Stats*); - static std::tuple> CreateLazyView(GrDirectContext*, - int wh, Stats*); static bool FillInViewOnGpu(GrDirectContext*, int wh, Stats*, const GrSurfaceProxyView& lazyView, - sk_sp); + sk_sp); Stats fStats; GrDirectContext* fDContext = nullptr; @@ -335,49 +325,10 @@ GrSurfaceProxyView TestHelper::CreateViewOnCpu(GrRecordingContext* rContext, return {std::move(proxy), kImageOrigin, swizzle}; } -std::tuple> TestHelper::CreateLazyView( - GrDirectContext* dContext, int wh, Stats* stats) { - - GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); - - sk_sp trampoline(new Trampoline); - - GrProxyProvider::TextureInfo texInfo { GrMipMapped::kNo, GrTextureType::k2D }; - - GrBackendFormat format = dContext->defaultBackendFormat(kRGBA_8888_SkColorType, - GrRenderable::kYes); - sk_sp proxy = proxyProvider->createLazyRenderTargetProxy( - [trampoline] (GrResourceProvider* resourceProvider, const GrSurfaceProxy::LazySurfaceDesc&) - -> GrSurfaceProxy::LazyCallbackResult { - if (!resourceProvider || !trampoline->fProxy || !trampoline->fProxy->isInstantiated()) { - return GrSurfaceProxy::LazyCallbackResult(nullptr, true); - - } - - SkASSERT(!trampoline->fProxy->peekTexture()->getUniqueKey().isValid()); - return GrSurfaceProxy::LazyCallbackResult(sk_ref_sp(trampoline->fProxy->peekTexture())); - }, - format, - {wh, wh}, - /* renderTargetSampleCnt */ 1, - GrInternalSurfaceFlags::kNone, - &texInfo, - GrMipmapStatus::kNotAllocated, - SkBackingFit::kExact, - SkBudgeted::kYes, - GrProtected::kNo, - /* wrapsVkSecondaryCB */ false, - GrSurfaceProxy::UseAllocator::kYes); - - GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888); - - ++stats->fNumLazyCreations; - return {{std::move(proxy), kImageOrigin, swizzle}, std::move(trampoline)}; -} - -bool TestHelper::FillInViewOnGpu(GrDirectContext* dContext, int wh, Stats* stats, - const GrSurfaceProxyView& lazyView, - sk_sp trampoline) { +bool TestHelper::FillInViewOnGpu( + GrDirectContext* dContext, int wh, Stats* stats, + const GrSurfaceProxyView& lazyView, + sk_sp trampoline) { std::unique_ptr rtc = new_RTC(dContext, wh); @@ -410,7 +361,9 @@ GrSurfaceProxyView TestHelper::AccessCachedView( if (GrDirectContext* dContext = rContext->asDirectContext()) { // The gpu thread gets priority over the recording threads. If the gpu thread is first, // it crams a lazy proxy into the cache and then fills it in later. - auto [lazyView, trampoline] = CreateLazyView(dContext, wh, stats); + auto [lazyView, trampoline] = GrThreadSafeUniquelyKeyedProxyViewCache::CreateLazyView( + dContext, {wh, wh}, GrColorType::kRGBA_8888, kImageOrigin); + ++stats->fNumLazyCreations; auto [view, data] = threadSafeViewCache->findOrAddWithData(key, lazyView); if (view != lazyView) {