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 <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Robert Phillips 2020-10-06 16:44:18 -04:00 committed by Skia Commit-Bot
parent 6c2f976a10
commit b1807129a6
5 changed files with 99 additions and 182 deletions

View File

@ -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<GrSurfaceProxyView, sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline>>
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> trampoline(new Trampoline);
GrProxyProvider::TextureInfo texInfo{ GrMipMapped::kNo, GrTextureType::k2D };
sk_sp<GrRenderTargetProxy> 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)};
}

View File

@ -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<GrTextureProxy> fProxy;
};
static std::tuple<GrSurfaceProxyView, sk_sp<Trampoline>> CreateLazyView(GrDirectContext*,
SkISize dimensions,
GrColorType,
GrSurfaceOrigin);
private:
struct Entry {
Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}

View File

@ -73,17 +73,13 @@ uniform half blurRadius;
builder.finish();
}
class Trampoline : public SkRefCnt {
public:
sk_sp<GrTextureProxy> fProxy;
};
static bool fillin_view_on_gpu(GrDirectContext* dContext,
const GrSurfaceProxyView& lazyView,
sk_sp<Trampoline> trampoline,
const SkRRect& rrectToDraw,
const SkISize& dimensions,
float xformedSigma) {
static bool fillin_view_on_gpu(
GrDirectContext* dContext,
const GrSurfaceProxyView& lazyView,
sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline> trampoline,
const SkRRect& rrectToDraw,
const SkISize& dimensions,
float xformedSigma) {
std::unique_ptr<GrRenderTargetContext> 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<GrSurfaceProxyView, sk_sp<Trampoline>> 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> trampoline(new Trampoline);
GrProxyProvider::TextureInfo texInfo { GrMipMapped::kNo, GrTextureType::k2D };
sk_sp<GrRenderTargetProxy> 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<GrFragmentProcessor> 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;
}

View File

@ -48,17 +48,13 @@ static void make_blurred_rrect_key(GrUniqueKey* key,
builder.finish();
}
class Trampoline : public SkRefCnt {
public:
sk_sp<GrTextureProxy> fProxy;
};
static bool fillin_view_on_gpu(GrDirectContext* dContext,
const GrSurfaceProxyView& lazyView,
sk_sp<Trampoline> trampoline,
const SkRRect& rrectToDraw,
const SkISize& dimensions,
float xformedSigma) {
static bool fillin_view_on_gpu(
GrDirectContext* dContext,
const GrSurfaceProxyView& lazyView,
sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline> trampoline,
const SkRRect& rrectToDraw,
const SkISize& dimensions,
float xformedSigma) {
std::unique_ptr<GrRenderTargetContext> 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<GrSurfaceProxyView, sk_sp<Trampoline>> 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> trampoline(new Trampoline);
GrProxyProvider::TextureInfo texInfo{GrMipMapped::kNo, GrTextureType::k2D};
sk_sp<GrRenderTargetProxy> 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<GrFragmentProcessor> find_or_create_rrect_blur_mask_fp(
GrRecordingContext* rContext,
const SkRRect& rrectToDraw,
@ -299,7 +247,8 @@ static std::unique_ptr<GrFragmentProcessor> 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:

View File

@ -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<GrTextureProxy> 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<GrSurfaceProxyView, sk_sp<Trampoline>> CreateLazyView(GrDirectContext*,
int wh, Stats*);
static bool FillInViewOnGpu(GrDirectContext*, int wh, Stats*,
const GrSurfaceProxyView& lazyView,
sk_sp<Trampoline>);
sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline>);
Stats fStats;
GrDirectContext* fDContext = nullptr;
@ -335,49 +325,10 @@ GrSurfaceProxyView TestHelper::CreateViewOnCpu(GrRecordingContext* rContext,
return {std::move(proxy), kImageOrigin, swizzle};
}
std::tuple<GrSurfaceProxyView, sk_sp<TestHelper::Trampoline>> TestHelper::CreateLazyView(
GrDirectContext* dContext, int wh, Stats* stats) {
GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
sk_sp<Trampoline> trampoline(new Trampoline);
GrProxyProvider::TextureInfo texInfo { GrMipMapped::kNo, GrTextureType::k2D };
GrBackendFormat format = dContext->defaultBackendFormat(kRGBA_8888_SkColorType,
GrRenderable::kYes);
sk_sp<GrRenderTargetProxy> 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> trampoline) {
bool TestHelper::FillInViewOnGpu(
GrDirectContext* dContext, int wh, Stats* stats,
const GrSurfaceProxyView& lazyView,
sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline> trampoline) {
std::unique_ptr<GrRenderTargetContext> 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) {