Revert "Revert "Assert that all GrCaps colortype/format -> swizzle queries are legal.""
This reverts commit ef4d3794a2ddffa877065aa66869c02bb582a479.
Fix for finding cached textures that were made with fallback color types.
Change-Id: Ic5b67801f9275f6a1cbe8373b8cce382f3cedfe7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/280607
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrBlurUtils.cpp b/src/gpu/GrBlurUtils.cpp
index 4d084f9..b6cbdf8 100644
--- a/src/gpu/GrBlurUtils.cpp
+++ b/src/gpu/GrBlurUtils.cpp
@@ -31,6 +31,12 @@
return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
}
+static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
+
+static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
+ return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8);
+}
+
// Draw a mask using the supplied paint. Since the coverage/geometry
// is already burnt into the mask this boils down to a rect draw.
// Return true if the mask was successfully drawn.
@@ -81,18 +87,11 @@
: SkStrokeRec::kFill_InitStyle;
if (key.isValid()) {
- // TODO: this cache look up is duplicated in draw_shape_with_mask_filter for gpu
- static const GrSurfaceOrigin kCacheOrigin = kTopLeft_GrSurfaceOrigin;
- auto filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key);
- if (filteredMask) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
- filteredMask->backendFormat(), GrColorType::kAlpha_8);
- filteredMaskView = GrSurfaceProxyView(std::move(filteredMask), kCacheOrigin, swizzle);
- }
+ filteredMaskView = find_filtered_mask(proxyProvider, key);
}
SkIRect drawRect;
- if (filteredMaskView.proxy()) {
+ if (filteredMaskView) {
SkRect devBounds = shape.bounds();
viewMatrix.mapRect(&devBounds);
@@ -157,7 +156,7 @@
return false;
}
- SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMaskView.origin());
+ SkASSERT(kMaskOrigin == filteredMaskView.origin());
drawRect = dstM.fBounds;
@@ -188,7 +187,7 @@
auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
auto rtContext = GrRenderTargetContext::MakeWithFallback(
context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
- GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
+ GrMipMapped::kNo, GrProtected::kNo, kMaskOrigin);
if (!rtContext) {
return nullptr;
}
@@ -202,8 +201,8 @@
const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
GrFixedClip clip(clipRect);
- // Draw the mask into maskTexture with the path's integerized top-left at
- // the origin using maskPaint.
+ // Draw the mask into maskTexture with the path's integerized top-left at the origin using
+ // maskPaint.
SkMatrix viewMatrix = origViewMatrix;
viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
@@ -398,18 +397,10 @@
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
if (maskKey.isValid()) {
- // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster
- static const GrSurfaceOrigin kCacheOrigin = kTopLeft_GrSurfaceOrigin;
- auto filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(maskKey);
- if (filteredMask) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
- filteredMask->backendFormat(), GrColorType::kAlpha_8);
- filteredMaskView = GrSurfaceProxyView(std::move(filteredMask), kCacheOrigin,
- swizzle);
- }
+ filteredMaskView = find_filtered_mask(proxyProvider, maskKey);
}
- if (!filteredMaskView.proxy()) {
+ if (!filteredMaskView) {
std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
context,
maskRect,
@@ -431,7 +422,7 @@
}
}
- if (filteredMaskView.proxy()) {
+ if (filteredMaskView) {
if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
std::move(filteredMaskView))) {
// This path is completely drawn
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index 8c3a0f2..ec6c648 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -352,6 +352,12 @@
SkDEBUGFAIL("Gen ID was not found in stack.");
}
+static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
+
+static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
+ return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8);
+}
+
GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
const GrReducedClip& reducedClip) const {
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
@@ -359,16 +365,14 @@
create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
reducedClip.numAnalyticFPs(), &key);
- if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
- GrColorType::kAlpha_8);
- return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
+ if (auto cachedView = find_mask(context->priv().proxyProvider(), key)) {
+ return cachedView;
}
auto rtc = GrRenderTargetContext::MakeWithFallback(
context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
{reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
- kTopLeft_GrSurfaceOrigin);
+ kMaskOrigin);
if (!rtc) {
return {};
}
@@ -382,7 +386,7 @@
return {};
}
- SkASSERT(result.origin() == kTopLeft_GrSurfaceOrigin);
+ SkASSERT(result.origin() == kMaskOrigin);
proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
@@ -471,12 +475,9 @@
reducedClip.numAnalyticFPs(), &key);
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
- const GrCaps* caps = context->priv().caps();
- if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
- GrColorType::kAlpha_8);
- return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
+ if (auto cachedView = find_mask(proxyProvider, key)) {
+ return cachedView;
}
// The mask texture may be larger than necessary. We round out the clip bounds and pin the top
@@ -490,6 +491,7 @@
GrSurfaceProxyView view;
if (taskGroup && renderTargetContext) {
+ const GrCaps* caps = context->priv().caps();
// Create our texture proxy
GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
GrRenderable::kNo);
@@ -525,7 +527,7 @@
taskGroup->add(std::move(drawAndUploadMask));
proxy->texPriv().setDeferredUploader(std::move(uploader));
- view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
+ view = {std::move(proxy), kMaskOrigin, swizzle};
} else {
GrSWMaskHelper helper;
if (!helper.init(maskSpaceIBounds)) {
@@ -539,7 +541,7 @@
}
SkASSERT(view);
- SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
+ SkASSERT(view.origin() == kMaskOrigin);
proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
return view;
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index 4fb2a64..9255d9d 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -23,6 +23,7 @@
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrImageContextPriv.h"
#include "src/gpu/GrRenderTarget.h"
+#include "src/gpu/GrRenderTargetContext.h"
#include "src/gpu/GrResourceProvider.h"
#include "src/gpu/GrSurfaceProxy.h"
#include "src/gpu/GrSurfaceProxyPriv.h"
@@ -229,6 +230,24 @@
return result;
}
+GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
+ GrSurfaceOrigin origin,
+ GrColorType ct) {
+ auto proxy = this->findOrCreateProxyByUniqueKey(key);
+ if (!proxy) {
+ return {};
+ }
+ // Assume that we used a fallback color type if and only if the proxy is renderable.
+ if (proxy->asRenderTargetProxy()) {
+ GrBackendFormat expectedFormat;
+ std::tie(ct, expectedFormat) =
+ GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct);
+ SkASSERT(expectedFormat == proxy->backendFormat());
+ }
+ GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
+ return {std::move(proxy), origin, swizzle};
+}
+
sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
GrMipMapped mipMapped,
SkBackingFit fit,
diff --git a/src/gpu/GrProxyProvider.h b/src/gpu/GrProxyProvider.h
index db2dd09..52f6d44 100644
--- a/src/gpu/GrProxyProvider.h
+++ b/src/gpu/GrProxyProvider.h
@@ -59,6 +59,16 @@
sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&,
UseAllocator = UseAllocator::kYes);
+ /**
+ * A helper that uses findOrCreateProxyByUniqueKey() to find a proxy and if found creates a view
+ *a view for the found proxy using the passed in origin and color type. It is assumed that if
+ * the proxy is renderable then it was created by GrRenderTargetContext::MakeWithFallback and
+ * the fallback color type will be used to create the view.
+ */
+ GrSurfaceProxyView findCachedProxyWithColorTypeFallback(const GrUniqueKey&,
+ GrSurfaceOrigin,
+ GrColorType);
+
/*
* Creates a new texture proxy for the bitmap, optionally with mip levels generated by the cpu.
* The bitmap is uploaded to the texture proxy assuming a kTopLeft_GrSurfaceOrigin.
diff --git a/src/gpu/GrRenderTargetContext.cpp b/src/gpu/GrRenderTargetContext.cpp
index ada5a04..d075092 100644
--- a/src/gpu/GrRenderTargetContext.cpp
+++ b/src/gpu/GrRenderTargetContext.cpp
@@ -9,6 +9,7 @@
#include "include/core/SkDrawable.h"
#include "include/gpu/GrBackendSemaphore.h"
+#include "include/private/GrImageContext.h"
#include "include/private/GrRecordingContext.h"
#include "include/private/SkShadowFlags.h"
#include "include/utils/SkShadowUtils.h"
@@ -32,6 +33,7 @@
#include "src/gpu/GrDrawingManager.h"
#include "src/gpu/GrFixedClip.h"
#include "src/gpu/GrGpuResourcePriv.h"
+#include "src/gpu/GrImageContextPriv.h"
#include "src/gpu/GrImageInfo.h"
#include "src/gpu/GrMemoryPool.h"
#include "src/gpu/GrPathRenderer.h"
@@ -240,6 +242,19 @@
}
}
+std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
+ GrImageContext* context, GrColorType colorType) {
+ do {
+ auto format =
+ context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
+ if (format.isValid()) {
+ return {colorType, format};
+ }
+ colorType = color_type_fallback(colorType);
+ } while (colorType != GrColorType::kUnknown);
+ return {GrColorType::kUnknown, {}};
+}
+
std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
GrRecordingContext* context,
GrColorType colorType,
@@ -252,14 +267,12 @@
GrSurfaceOrigin origin,
SkBudgeted budgeted,
const SkSurfaceProps* surfaceProps) {
- std::unique_ptr<GrRenderTargetContext> rtc;
- do {
- rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
- sampleCnt, mipMapped, isProtected, origin, budgeted,
- surfaceProps);
- colorType = color_type_fallback(colorType);
- } while (!rtc && colorType != GrColorType::kUnknown);
- return rtc;
+ auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType);
+ if (ct == GrColorType::kUnknown) {
+ return nullptr;
+ }
+ return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
+ mipMapped, isProtected, origin, budgeted, surfaceProps);
}
std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
diff --git a/src/gpu/GrRenderTargetContext.h b/src/gpu/GrRenderTargetContext.h
index 6befa9e..a5f9008 100644
--- a/src/gpu/GrRenderTargetContext.h
+++ b/src/gpu/GrRenderTargetContext.h
@@ -89,6 +89,9 @@
SkBudgeted = SkBudgeted::kYes,
const SkSurfaceProps* = nullptr);
+ static std::tuple<GrColorType, GrBackendFormat> GetFallbackColorTypeAndFormat(GrImageContext*,
+ GrColorType);
+
// Same as previous factory but will try to use fallback GrColorTypes if the one passed in
// fails. The fallback GrColorType will have at least the number of channels and precision per
// channel as the passed in GrColorType. It may also swizzle the changes (e.g., BGRA -> RGBA).
diff --git a/src/gpu/GrSurfaceContext.cpp b/src/gpu/GrSurfaceContext.cpp
index 094c8b97..7e18008 100644
--- a/src/gpu/GrSurfaceContext.cpp
+++ b/src/gpu/GrSurfaceContext.cpp
@@ -74,7 +74,10 @@
sk_sp<SkColorSpace> colorSpace,
SkBackingFit fit,
SkBudgeted budgeted) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
+ GrSwizzle swizzle("rgba");
+ if (!context->priv().caps()->isFormatCompressed(format)) {
+ swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
+ }
sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
format, dimensions, renderable, renderTargetSampleCnt, mipMapped, fit, budgeted,
diff --git a/src/gpu/d3d/GrD3DCaps.cpp b/src/gpu/d3d/GrD3DCaps.cpp
index 07dca85..1f4fbe7 100644
--- a/src/gpu/d3d/GrD3DCaps.cpp
+++ b/src/gpu/d3d/GrD3DCaps.cpp
@@ -944,7 +944,8 @@
return ctInfo.fReadSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
GrSwizzle GrD3DCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
@@ -957,7 +958,8 @@
return ctInfo.fOutputSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
uint64_t GrD3DCaps::computeFormatKey(const GrBackendFormat& format) const {
diff --git a/src/gpu/effects/GrRRectBlurEffect.fp b/src/gpu/effects/GrRRectBlurEffect.fp
index 75412a8..643a848 100644
--- a/src/gpu/effects/GrRRectBlurEffect.fp
+++ b/src/gpu/effects/GrRRectBlurEffect.fp
@@ -47,16 +47,17 @@
}
builder.finish();
+ static constexpr auto kMaskOrigin = kBottomLeft_GrSurfaceOrigin;
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
- if (sk_sp<GrTextureProxy> mask = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(mask->backendFormat(),
- GrColorType::kAlpha_8);
- return {std::move(mask), kBottomLeft_GrSurfaceOrigin, swizzle};
+ if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback(
+ key, kMaskOrigin, GrColorType::kAlpha_8)) {
+ return view;
}
auto rtc = GrRenderTargetContext::MakeWithFallback(
- context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions);
+ context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1,
+ GrMipMapped::kNo, GrProtected::kNo, kMaskOrigin);
if (!rtc) {
return {};
}
diff --git a/src/gpu/effects/generated/GrRRectBlurEffect.h b/src/gpu/effects/generated/GrRRectBlurEffect.h
index b4620b7..a1a2ea4 100644
--- a/src/gpu/effects/generated/GrRRectBlurEffect.h
+++ b/src/gpu/effects/generated/GrRRectBlurEffect.h
@@ -49,16 +49,17 @@
}
builder.finish();
+ static constexpr auto kMaskOrigin = kBottomLeft_GrSurfaceOrigin;
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
- if (sk_sp<GrTextureProxy> mask = proxyProvider->findOrCreateProxyByUniqueKey(key)) {
- GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(mask->backendFormat(),
- GrColorType::kAlpha_8);
- return {std::move(mask), kBottomLeft_GrSurfaceOrigin, swizzle};
+ if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback(
+ key, kMaskOrigin, GrColorType::kAlpha_8)) {
+ return view;
}
- auto rtc = GrRenderTargetContext::MakeWithFallback(context, GrColorType::kAlpha_8, nullptr,
- SkBackingFit::kExact, dimensions);
+ auto rtc = GrRenderTargetContext::MakeWithFallback(
+ context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, dimensions, 1,
+ GrMipMapped::kNo, GrProtected::kNo, kMaskOrigin);
if (!rtc) {
return {};
}
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index cee6edc..436684b 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -4331,8 +4331,10 @@
return ctInfo.fReadSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
+
GrSwizzle GrGLCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
const auto& info = this->getFormatInfo(format.asGLFormat());
for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
@@ -4341,7 +4343,8 @@
return ctInfo.fOutputSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
uint64_t GrGLCaps::computeFormatKey(const GrBackendFormat& format) const {
diff --git a/src/gpu/mock/GrMockCaps.h b/src/gpu/mock/GrMockCaps.h
index 6335757..2400c49 100644
--- a/src/gpu/mock/GrMockCaps.h
+++ b/src/gpu/mock/GrMockCaps.h
@@ -164,11 +164,13 @@
return {};
}
- GrSwizzle getReadSwizzle(const GrBackendFormat&, GrColorType) const override {
- return GrSwizzle();
+ GrSwizzle getReadSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
+ SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
+ return GrSwizzle("rgba");
}
- GrSwizzle getOutputSwizzle(const GrBackendFormat&, GrColorType) const override {
- return GrSwizzle();
+ GrSwizzle getOutputSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
+ SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
+ return GrSwizzle("rgba");
}
uint64_t computeFormatKey(const GrBackendFormat&) const override;
diff --git a/src/gpu/mtl/GrMtlCaps.mm b/src/gpu/mtl/GrMtlCaps.mm
index e146d7f..de7293c 100644
--- a/src/gpu/mtl/GrMtlCaps.mm
+++ b/src/gpu/mtl/GrMtlCaps.mm
@@ -943,8 +943,10 @@
return ctInfo.fReadSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
+
GrSwizzle GrMtlCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
SkASSERT(mtlFormat != MTLPixelFormatInvalid);
@@ -955,7 +957,8 @@
return ctInfo.fOutputSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
uint64_t GrMtlCaps::computeFormatKey(const GrBackendFormat& format) const {
diff --git a/src/gpu/vk/GrVkCaps.cpp b/src/gpu/vk/GrVkCaps.cpp
index 55ddbad..9579803 100644
--- a/src/gpu/vk/GrVkCaps.cpp
+++ b/src/gpu/vk/GrVkCaps.cpp
@@ -1593,7 +1593,8 @@
return ctInfo.fReadSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
@@ -1606,7 +1607,8 @@
return ctInfo.fOutputSwizzle;
}
}
- return GrSwizzle::RGBA();
+ SkDEBUGFAIL("Illegal color type/format combination.");
+ return {};
}
uint64_t GrVkCaps::computeFormatKey(const GrBackendFormat& format) const {