blob: 4084e2b799033bec3026c785af7dbb9cd4760062 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomoneebe7352020-12-09 16:37:04 -05008#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
Brian Osmanf5ecf512020-04-01 09:29:13 -040011#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040013#include "include/gpu/GrDirectContext.h"
14#include "include/gpu/GrRecordingContext.h"
Brian Salomond005b692020-04-01 15:47:05 -040015#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkShadowFlags.h"
17#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040018#include "src/core/SkAutoPixmapStorage.h"
19#include "src/core/SkConvertPixels.h"
Chris Dalton319d0202021-01-29 23:49:47 -070020#include "src/core/SkDrawProcs.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkDrawShadowInfo.h"
22#include "src/core/SkGlyphRunPainter.h"
23#include "src/core/SkLatticeIter.h"
24#include "src/core/SkMatrixPriv.h"
Herb Derby64688222020-06-08 14:19:57 -040025#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/core/SkRRectPriv.h"
27#include "src/core/SkSurfacePriv.h"
28#include "src/gpu/GrAppliedClip.h"
Greg Danielc0d69152020-10-08 14:59:00 -040029#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040030#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrBlurUtils.h"
32#include "src/gpu/GrCaps.h"
Michael Ludwig58f569b2020-05-21 17:03:08 -040033#include "src/gpu/GrClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040034#include "src/gpu/GrColor.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040035#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040036#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040039#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040040#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrMemoryPool.h"
42#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040043#include "src/gpu/GrProxyProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040044#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrStyle.h"
47#include "src/gpu/GrTracing.h"
48#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040049#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/effects/GrRRectEffect.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040051#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000052#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000053#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/gpu/ops/GrAtlasTextOp.h"
55#include "src/gpu/ops/GrClearOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrDrawAtlasOp.h"
57#include "src/gpu/ops/GrDrawOp.h"
58#include "src/gpu/ops/GrDrawVerticesOp.h"
59#include "src/gpu/ops/GrDrawableOp.h"
60#include "src/gpu/ops/GrFillRRectOp.h"
61#include "src/gpu/ops/GrFillRectOp.h"
62#include "src/gpu/ops/GrLatticeOp.h"
63#include "src/gpu/ops/GrOp.h"
64#include "src/gpu/ops/GrOvalOpFactory.h"
65#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066#include "src/gpu/ops/GrShadowRRectOp.h"
67#include "src/gpu/ops/GrStencilPathOp.h"
68#include "src/gpu/ops/GrStrokeRectOp.h"
69#include "src/gpu/ops/GrTextureOp.h"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050070#include "src/gpu/text/GrSDFTControl.h"
Herb Derby64688222020-06-08 14:19:57 -040071#include "src/gpu/text/GrTextBlobCache.h"
joshualittbc907352016-01-13 06:45:40 -080072
Robert Phillips72152832017-01-25 17:31:35 -050073#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -040074#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040075#define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
Robert Phillips9eb00022020-06-30 15:30:12 -040076#define RETURN_FALSE_IF_ABANDONED if (fContext->abandoned()) { return false; }
robertphillipsea461502015-05-26 11:38:03 -070077
Brian Salomone225b562017-06-14 13:00:03 -040078//////////////////////////////////////////////////////////////////////////////
79
Chris Daltonfbc3a832021-02-15 23:16:17 -070080using DoSimplify = GrStyledShape::DoSimplify;
Chris Daltonb8ed2bc2021-02-02 11:53:19 -070081
robertphillipsea461502015-05-26 11:38:03 -070082class AutoCheckFlush {
83public:
halcanary9d524f22016-03-29 09:03:52 -070084 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070085 SkASSERT(fDrawingManager);
86 }
bsalomonb77a9072016-09-07 10:02:04 -070087 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070088
89private:
robertphillips77a2e522015-10-17 07:43:27 -070090 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070091};
92
Brian Salomoneebe7352020-12-09 16:37:04 -050093std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(GrRecordingContext* context,
94 GrColorType colorType,
95 sk_sp<SkColorSpace> colorSpace,
96 sk_sp<GrSurfaceProxy> proxy,
97 GrSurfaceOrigin origin,
98 const SkSurfaceProps* surfaceProps,
99 bool flushTimeOpsTask) {
Greg Danielba0ff782020-01-07 15:42:57 -0500100 if (!proxy) {
101 return nullptr;
102 }
103
104 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400105 GrSwizzle readSwizzle, writeSwizzle;
106 if (colorType != GrColorType::kUnknown) {
107 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
108 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
109 }
Greg Danielba0ff782020-01-07 15:42:57 -0500110
Brian Salomon07bc9a22020-12-02 13:37:16 -0500111 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400112 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500113
Brian Salomoneebe7352020-12-09 16:37:04 -0500114 return std::make_unique<GrSurfaceDrawContext>(context,
115 std::move(readView),
116 std::move(writeView),
117 colorType,
118 std::move(colorSpace),
119 surfaceProps,
120 flushTimeOpsTask);
Greg Danielba0ff782020-01-07 15:42:57 -0500121}
122
Brian Salomoneebe7352020-12-09 16:37:04 -0500123std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Danielba0ff782020-01-07 15:42:57 -0500124 GrRecordingContext* context,
Greg Danielba0ff782020-01-07 15:42:57 -0500125 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500126 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500127 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500128 const GrBackendFormat& format,
129 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400130 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500131 GrProtected isProtected,
Brian Salomon07bc9a22020-12-02 13:37:16 -0500132 GrSwizzle readSwizzle,
133 GrSwizzle writeSwizzle,
Greg Daniele20fcad2020-01-08 11:52:34 -0500134 GrSurfaceOrigin origin,
135 SkBudgeted budgeted,
136 const SkSurfaceProps* surfaceProps) {
137 // It is probably not necessary to check if the context is abandoned here since uses of the
Brian Salomoneebe7352020-12-09 16:37:04 -0500138 // GrSurfaceDrawContext which need the context will mostly likely fail later on without an
Greg Daniele20fcad2020-01-08 11:52:34 -0500139 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
140 // an abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -0400141 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500142 return nullptr;
143 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500144
Greg Daniele20fcad2020-01-08 11:52:34 -0500145 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomon07bc9a22020-12-02 13:37:16 -0500146 format,
147 dimensions,
148 GrRenderable::kYes,
149 sampleCnt,
150 mipMapped,
151 fit,
152 budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500153 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500154 if (!proxy) {
155 return nullptr;
156 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500157
Brian Salomon07bc9a22020-12-02 13:37:16 -0500158 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
159 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
160
Brian Salomoneebe7352020-12-09 16:37:04 -0500161 auto rtc = std::make_unique<GrSurfaceDrawContext>(context,
162 std::move(readView),
163 std::move(writeView),
164 GrColorType::kUnknown,
165 std::move(colorSpace),
166 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500167 rtc->discard();
168 return rtc;
169}
170
Brian Salomoneebe7352020-12-09 16:37:04 -0500171std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500172 GrRecordingContext* context,
173 GrColorType colorType,
174 sk_sp<SkColorSpace> colorSpace,
175 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500176 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500177 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400178 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 GrProtected isProtected,
180 GrSurfaceOrigin origin,
181 SkBudgeted budgeted,
182 const SkSurfaceProps* surfaceProps) {
183 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
184 if (!format.isValid()) {
185 return nullptr;
186 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500187 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
188 dimensions,
189 GrRenderable::kYes,
190 sampleCnt,
191 mipMapped,
192 fit,
193 budgeted,
194 isProtected);
Brian Salomon07bc9a22020-12-02 13:37:16 -0500195 if (!proxy) {
196 return nullptr;
197 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500198
Brian Salomoneebe7352020-12-09 16:37:04 -0500199 return GrSurfaceDrawContext::Make(context,
200 colorType,
201 std::move(colorSpace),
202 std::move(proxy),
203 origin,
204 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500205}
206
Brian Salomoneebe7352020-12-09 16:37:04 -0500207std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeWithFallback(
Greg Daniele20fcad2020-01-08 11:52:34 -0500208 GrRecordingContext* context,
209 GrColorType colorType,
210 sk_sp<SkColorSpace> colorSpace,
211 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500212 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500213 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400214 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500215 GrProtected isProtected,
216 GrSurfaceOrigin origin,
217 SkBudgeted budgeted,
218 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400219 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400220 if (ct == GrColorType::kUnknown) {
221 return nullptr;
222 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500223 return GrSurfaceDrawContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
224 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500225}
226
Brian Salomoneebe7352020-12-09 16:37:04 -0500227std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendTexture(
Greg Danielba0ff782020-01-07 15:42:57 -0500228 GrRecordingContext* context,
229 GrColorType colorType,
230 sk_sp<SkColorSpace> colorSpace,
231 const GrBackendTexture& tex,
232 int sampleCnt,
233 GrSurfaceOrigin origin,
234 const SkSurfaceProps* surfaceProps,
Robert Phillipsa1121332020-06-29 13:05:29 -0400235 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500236 SkASSERT(sampleCnt > 0);
237 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400238 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
239 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500240 if (!proxy) {
241 return nullptr;
242 }
243
Brian Salomoneebe7352020-12-09 16:37:04 -0500244 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
245 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500246}
247
Brian Salomoneebe7352020-12-09 16:37:04 -0500248std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendRenderTarget(
Greg Danielba0ff782020-01-07 15:42:57 -0500249 GrRecordingContext* context,
250 GrColorType colorType,
251 sk_sp<SkColorSpace> colorSpace,
252 const GrBackendRenderTarget& rt,
253 GrSurfaceOrigin origin,
254 const SkSurfaceProps* surfaceProps,
Brian Salomon8ba54ea2020-10-01 09:48:21 -0400255 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400256 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsa1121332020-06-29 13:05:29 -0400257 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500258 if (!proxy) {
259 return nullptr;
260 }
261
Brian Salomoneebe7352020-12-09 16:37:04 -0500262 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
263 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500264}
265
Brian Salomoneebe7352020-12-09 16:37:04 -0500266std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromVulkanSecondaryCB(
Greg Danielba0ff782020-01-07 15:42:57 -0500267 GrRecordingContext* context,
268 const SkImageInfo& imageInfo,
269 const GrVkDrawableInfo& vkInfo,
270 const SkSurfaceProps* props) {
271 sk_sp<GrSurfaceProxy> proxy(
272 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
273 vkInfo));
274 if (!proxy) {
275 return nullptr;
276 }
277
Brian Salomoneebe7352020-12-09 16:37:04 -0500278 return GrSurfaceDrawContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
279 imageInfo.refColorSpace(), std::move(proxy),
280 kTopLeft_GrSurfaceOrigin, props);
Greg Danielba0ff782020-01-07 15:42:57 -0500281}
282
Greg Danielf41b2bd2019-08-22 16:19:24 -0400283// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
284// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
285// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500286// when the surfaceDrawContext attempts to use it (via getOpsTask).
Brian Salomoneebe7352020-12-09 16:37:04 -0500287GrSurfaceDrawContext::GrSurfaceDrawContext(GrRecordingContext* context,
288 GrSurfaceProxyView readView,
289 GrSurfaceProxyView writeView,
290 GrColorType colorType,
291 sk_sp<SkColorSpace> colorSpace,
292 const SkSurfaceProps* surfaceProps,
293 bool flushTimeOpsTask)
Brian Salomon590f5672020-12-16 11:44:47 -0500294 : GrSurfaceFillContext(context,
295 std::move(readView),
296 std::move(writeView),
297 {colorType, kPremul_SkAlphaType, std::move(colorSpace)},
298 flushTimeOpsTask)
Brian Salomonf3569f02017-10-24 12:52:33 -0400299 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400300 , fGlyphPainter(*this) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700301 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700302}
303
Brian Salomoneebe7352020-12-09 16:37:04 -0500304GrSurfaceDrawContext::~GrSurfaceDrawContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800305 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700306}
307
Brian Salomoneebe7352020-12-09 16:37:04 -0500308inline GrAAType GrSurfaceDrawContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600309 if (GrAA::kNo == aa) {
310 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
311 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600312 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600313 return GrAAType::kMSAA;
314 }
315 return GrAAType::kNone;
316 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600317 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600318}
319
Brian Salomoneebe7352020-12-09 16:37:04 -0500320GrMipmapped GrSurfaceDrawContext::mipmapped() const {
Greg Daniele252f082017-10-23 16:05:23 -0400321 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400322 return proxy->mipmapped();
Greg Daniele252f082017-10-23 16:05:23 -0400323 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400324 return GrMipmapped::kNo;
Greg Daniele252f082017-10-23 16:05:23 -0400325}
326
Herb Derby64688222020-06-08 14:19:57 -0400327static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
328 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
329 if (lcd) {
330 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
331 // can be modified. For now we just regenerate if any run in a textblob has LCD.
332 // TODO figure out where all of these modifications are and see if we can incorporate that
333 // logic at a higher level *OR* use sRGB
334 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
335
336 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
337 // see the note above. We pick a dummy value for LCD text to ensure we always match the
338 // same key
339 return SK_ColorTRANSPARENT;
340 } else {
341 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
342 // gamma corrected masks anyways, nor color
343 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
344 SkColorGetG(canonicalColor),
345 SkColorGetB(canonicalColor));
346 // reduce to our finite number of bits
347 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
348 }
349 return canonicalColor;
350}
351
Brian Salomoneebe7352020-12-09 16:37:04 -0500352void GrSurfaceDrawContext::drawGlyphRunList(const GrClip* clip,
353 const SkMatrixProvider& viewMatrix,
354 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800355 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700356 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700357 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500358 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700359
Greg Danielbe7fc462019-01-03 16:40:42 -0500360 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
361 // secondary command buffers because it would require stopping and starting a render pass which
362 // we don't have access to.
363 if (this->wrapsVkSecondaryCB()) {
364 return;
365 }
366
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500367 GrSDFTControl control =
368 this->recordingContext()->priv().getSDFTControl(
Herb Derby9ad09822021-03-01 14:19:36 -0500369 this->surfaceProps().isUseDeviceIndependentFonts());
370
Herb Derby64688222020-06-08 14:19:57 -0400371 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
372
373 // Get the first paint to use as the key paint.
Herb Derby281583a2020-11-19 11:40:07 -0500374 const SkPaint& drawPaint = glyphRunList.paint();
Herb Derby64688222020-06-08 14:19:57 -0400375
376 SkMaskFilterBase::BlurRec blurRec;
377 // It might be worth caching these things, but its not clear at this time
378 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
Herb Derby281583a2020-11-19 11:40:07 -0500379 const SkMaskFilter* mf = drawPaint.getMaskFilter();
Herb Derby64688222020-06-08 14:19:57 -0400380 bool canCache = glyphRunList.canCache() &&
Herb Derby281583a2020-11-19 11:40:07 -0500381 !(drawPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derby64688222020-06-08 14:19:57 -0400382
383 // If we're doing linear blending, then we can disable the gamma hacks.
384 // Otherwise, leave them on. In either case, we still want the contrast boost:
385 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
386 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
387 ? SkScalerContextFlags::kBoostContrast
388 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
Herb Derbye4ef35c2021-03-01 11:49:32 -0500389 SkMatrix drawMatrix(viewMatrix.localToDevice());
390 SkPoint drawOrigin = glyphRunList.origin();
391 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derby64688222020-06-08 14:19:57 -0400392
393 sk_sp<GrTextBlob> blob;
394 GrTextBlob::Key key;
395 if (canCache) {
396 bool hasLCD = glyphRunList.anyRunsLCD();
397
398 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
399 SkPixelGeometry pixelGeometry =
400 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
401
Herb Derby281583a2020-11-19 11:40:07 -0500402 GrColor canonicalColor = compute_canonical_color(drawPaint, hasLCD);
Herb Derby64688222020-06-08 14:19:57 -0400403
404 key.fPixelGeometry = pixelGeometry;
405 key.fUniqueID = glyphRunList.uniqueID();
Herb Derby281583a2020-11-19 11:40:07 -0500406 key.fStyle = drawPaint.getStyle();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400407 if (key.fStyle != SkPaint::kFill_Style) {
Herb Derby281583a2020-11-19 11:40:07 -0500408 key.fFrameWidth = drawPaint.getStrokeWidth();
409 key.fMiterLimit = drawPaint.getStrokeMiter();
410 key.fJoin = drawPaint.getStrokeJoin();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400411 }
Herb Derby64688222020-06-08 14:19:57 -0400412 key.fHasBlur = SkToBool(mf);
Herb Derby00b2fdb2020-09-03 11:54:01 -0400413 if (key.fHasBlur) {
414 key.fBlurRec = blurRec;
415 }
Herb Derby64688222020-06-08 14:19:57 -0400416 key.fCanonicalColor = canonicalColor;
417 key.fScalerContextFlags = scalerContextFlags;
Herb Derbye4ef35c2021-03-01 11:49:32 -0500418
419 // Calculate the set of drawing types.
420 key.fSetOfDrawingTypes = 0;
421 for (auto& run : glyphRunList) {
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500422 key.fSetOfDrawingTypes |= control.drawingType(run.font(), drawPaint, drawMatrix);
Herb Derbye4ef35c2021-03-01 11:49:32 -0500423 }
424
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500425 if (key.fSetOfDrawingTypes & GrSDFTControl::kDirect) {
Herb Derbye4ef35c2021-03-01 11:49:32 -0500426 // Store the fractional offset of the position. We know that the matrix can't be
427 // perspective at this point.
428 SkPoint mappedOrigin = drawMatrix.mapOrigin();
429 key.fDrawMatrix = drawMatrix;
430 key.fDrawMatrix.setTranslateX(
431 mappedOrigin.x() - SkScalarFloorToScalar(mappedOrigin.x()));
432 key.fDrawMatrix.setTranslateY(
433 mappedOrigin.y() - SkScalarFloorToScalar(mappedOrigin.y()));
434 } else {
435 // For path and SDFT, the matrix doesn't matter.
436 key.fDrawMatrix = SkMatrix::I();
437 }
438
Herb Derby64688222020-06-08 14:19:57 -0400439 blob = textBlobCache->find(key);
440 }
441
Herb Derby281583a2020-11-19 11:40:07 -0500442 if (blob == nullptr || !blob->canReuse(drawPaint, drawMatrix)) {
Herb Derby64688222020-06-08 14:19:57 -0400443 if (blob != nullptr) {
Herb Derbye4ef35c2021-03-01 11:49:32 -0500444 SkASSERT(!drawMatrix.hasPerspective());
Herb Derby64688222020-06-08 14:19:57 -0400445 // We have to remake the blob because changes may invalidate our masks.
446 // TODO we could probably get away with reuse most of the time if the pointer is unique,
447 // but we'd have to clear the SubRun information
448 textBlobCache->remove(blob.get());
449 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400450
Herb Derby63fe8e52021-03-08 13:22:56 -0500451 blob = GrTextBlob::Make(glyphRunList, drawMatrix, control, &fGlyphPainter);
Herb Derby14992492021-02-25 16:28:03 -0500452
453 if (canCache) {
454 blob->addKey(key);
455 // The blob may already have been created on a different thread. Use the first one
456 // that was there.
457 blob = textBlobCache->addOrReturnExisting(glyphRunList, blob);
458 }
Herb Derby64688222020-06-08 14:19:57 -0400459 }
460
Herb Derby6b27ae82021-02-23 13:03:59 -0500461 for (const GrSubRun& subRun : blob->subRunList()) {
Herb Derby55f795e2021-02-05 13:45:05 -0500462 subRun.draw(clip, viewMatrix, glyphRunList, this);
Herb Derbye5b768b2020-07-07 15:56:07 -0400463 }
robertphillipsea461502015-05-26 11:38:03 -0700464}
465
Brian Salomoneebe7352020-12-09 16:37:04 -0500466void GrSurfaceDrawContext::drawPaint(const GrClip* clip,
467 GrPaint&& paint,
468 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000469 // Start with the render target, since that is the maximum content we could possibly fill.
470 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500471 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400472 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000473 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
474 // inverse view matrix.
475 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
476 } else {
477 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
478 SkMatrix localMatrix;
479 if (!viewMatrix.invert(&localMatrix)) {
480 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400481 }
Michael Ludwig61328202019-06-19 14:48:58 +0000482 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
483 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700484 }
robertphillipsea461502015-05-26 11:38:03 -0700485}
486
Brian Salomoneebe7352020-12-09 16:37:04 -0500487enum class GrSurfaceDrawContext::QuadOptimization {
Michael Ludwig61328202019-06-19 14:48:58 +0000488 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
489 kDiscarded,
490 // The rect to draw was converted to some other op and appended to the oplist, so no additional
491 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
492 // a constColor is provided.
493 kSubmitted,
494 // The clip was folded into the device quad, with updated edge flags and local coords, and
495 // caller is responsible for adding an appropriate op.
496 kClipApplied,
497 // No change to clip, but quad updated to better fit clip/render target, and caller is
498 // responsible for adding an appropriate op.
499 kCropped
500};
Michael Ludwig61a16512019-01-15 11:15:13 -0500501
Brian Salomoneebe7352020-12-09 16:37:04 -0500502GrSurfaceDrawContext::QuadOptimization GrSurfaceDrawContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400503 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500504 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000505 // Optimization requirements:
506 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500507 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
508 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
509 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
510 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
511 // 5. kCropped in all other scenarios (although a crop may be a no-op)
512
513 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
514 // better to just keep the old flags instead of introducing mixed edge flags.
515 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000516
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400517 // Use the logical size of the render target, which allows for "fullscreen" clears even if
518 // the render target has an approximate backing fit
519 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400520
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500521 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400522 if (!quad->fDevice.isFinite() || drawBounds.isEmpty() ||
523 GrClip::IsOutsideClip(rtRect, drawBounds)) {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000524 return QuadOptimization::kDiscarded;
525 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400526 auto conservativeCrop = [&]() {
527 static constexpr int kLargeDrawLimit = 15000;
528 // Crop the quad to the render target. This doesn't change the visual results of drawing but
529 // is meant to help numerical stability for excessively large draws.
530 if (drawBounds.width() > kLargeDrawLimit || drawBounds.height() > kLargeDrawLimit) {
531 GrQuadUtils::CropToRect(rtRect, *aa, quad, /* compute local */ !constColor);
532 SkASSERT(quad->fEdgeFlags == oldFlags);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000533 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400534 };
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000535
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400536 bool simpleColor = !stencilSettings && constColor;
Michael Ludwig6397e802020-08-05 15:50:01 -0400537 GrClip::PreClipResult result = clip ? clip->preApply(drawBounds, *aa)
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400538 : GrClip::PreClipResult(GrClip::Effect::kUnclipped);
539 switch(result.fEffect) {
540 case GrClip::Effect::kClippedOut:
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000541 return QuadOptimization::kDiscarded;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400542 case GrClip::Effect::kUnclipped:
543 if (!simpleColor) {
544 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000545 return QuadOptimization::kClipApplied;
546 } else {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400547 // Update result to store the render target bounds in order and then fall
548 // through to attempt the draw->native clear optimization
549 result = GrClip::PreClipResult(SkRRect::MakeRect(rtRect), *aa);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000550 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400551 break;
552 case GrClip::Effect::kClipped:
553 if (!result.fIsRRect || (stencilSettings && result.fAA != *aa) ||
554 (!result.fRRect.isRect() && !simpleColor)) {
555 // The clip and draw state are too complicated to try and reduce
556 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000557 return QuadOptimization::kCropped;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400558 } // Else fall through to attempt to combine the draw and clip geometry together
559 break;
560 default:
561 SkUNREACHABLE;
562 }
563
564 // If we reached here, we know we're an axis-aligned clip that is either a rect or a round rect,
565 // so we can potentially combine it with the draw geometry so that no clipping is needed.
566 SkASSERT(result.fEffect == GrClip::Effect::kClipped && result.fIsRRect);
567 SkRect clippedBounds = result.fRRect.getBounds();
568 clippedBounds.intersect(rtRect);
569 if (result.fRRect.isRect()) {
570 // No rounded corners, so we might be able to become a native clear or we might be able to
571 // modify geometry and edge flags to represent intersected shape of clip and draw.
572 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
573 /*compute local*/ !constColor)) {
574 if (simpleColor && quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
575 // Clear optimization is possible
576 drawBounds = quad->fDevice.bounds();
577 if (drawBounds.contains(rtRect)) {
578 // Fullscreen clear
579 this->clear(*constColor);
580 return QuadOptimization::kSubmitted;
581 } else if (GrClip::IsPixelAligned(drawBounds) &&
582 drawBounds.width() > 256 && drawBounds.height() > 256) {
583 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
584 SkIRect scissorRect;
585 drawBounds.round(&scissorRect);
586 this->clear(scissorRect, *constColor);
587 return QuadOptimization::kSubmitted;
588 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000589 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400590
591 // else the draw and clip were combined so just update the AA to reflect combination
592 if (*aa == GrAA::kNo && result.fAA == GrAA::kYes &&
593 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
594 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
595 // properly reflect the smooth edge of the clip.
596 *aa = GrAA::kYes;
597 }
598 // We intentionally do not downgrade AA here because we don't know if we need to
599 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
600 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
601 // deviceQuad is exactly the intersection of original quad and clip, so it can be
602 // drawn with no clip (submitted by caller)
603 return QuadOptimization::kClipApplied;
604 }
605 } else {
606 // Rounded corners and constant filled color (limit ourselves to solid colors because
607 // there is no way to use custom local coordinates with drawRRect).
608 SkASSERT(simpleColor);
609 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
610 /* compute local */ false) &&
611 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
612 quad->fDevice.bounds().contains(clippedBounds)) {
613 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
614 // we can draw the rrect directly and ignore the edge flags
615 GrPaint paint;
Brian Salomon590f5672020-12-16 11:44:47 -0500616 ClearToGrPaint(constColor->array(), &paint);
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400617 this->drawRRect(nullptr, std::move(paint), result.fAA, SkMatrix::I(), result.fRRect,
618 GrStyle::SimpleFill());
619 return QuadOptimization::kSubmitted;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000620 }
621 }
622
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400623 // The quads have been updated to better fit the clip bounds, but can't get rid of
624 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500625 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000626 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000627}
628
Brian Salomoneebe7352020-12-09 16:37:04 -0500629void GrSurfaceDrawContext::drawFilledQuad(const GrClip* clip,
630 GrPaint&& paint,
631 GrAA aa,
632 DrawQuad* quad,
633 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000634 ASSERT_SINGLE_OWNER
635 RETURN_IF_ABANDONED
636 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500637 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500638
Michael Ludwig61328202019-06-19 14:48:58 +0000639 AutoCheckFlush acf(this->drawingManager());
640
641 SkPMColor4f* constColor = nullptr;
642 SkPMColor4f paintColor;
John Stiles41d91b62020-07-21 14:39:40 -0400643 if (!ss && !paint.hasCoverageFragmentProcessor() && paint.isConstantBlendedColor(&paintColor)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000644 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
645 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500646 }
647
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500648 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000649 if (opt >= QuadOptimization::kClipApplied) {
650 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400651 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000652 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
653 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500654 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
655 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700656 }
Michael Ludwig61328202019-06-19 14:48:58 +0000657 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800658}
659
Brian Salomoneebe7352020-12-09 16:37:04 -0500660void GrSurfaceDrawContext::drawTexturedQuad(const GrClip* clip,
661 GrSurfaceProxyView proxyView,
662 SkAlphaType srcAlphaType,
663 sk_sp<GrColorSpaceXform> textureXform,
664 GrSamplerState::Filter filter,
665 GrSamplerState::MipmapMode mm,
666 const SkPMColor4f& color,
667 SkBlendMode blendMode,
668 GrAA aa,
669 DrawQuad* quad,
670 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000671 ASSERT_SINGLE_OWNER
672 RETURN_IF_ABANDONED
673 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400674 SkASSERT(proxyView.asTextureProxy());
Brian Salomoneebe7352020-12-09 16:37:04 -0500675 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTexturedQuad", fContext);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000676
677 AutoCheckFlush acf(this->drawingManager());
678
679 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
680 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500681 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000682
683 SkASSERT(opt != QuadOptimization::kSubmitted);
684 if (opt != QuadOptimization::kDiscarded) {
685 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400686 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000687 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400688 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400689 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
690 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400691 // Use the provided subset, although hypothetically we could detect that the cropped local
692 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500693 this->addDrawOp(finalClip,
694 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400695 std::move(textureXform), filter, mm, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400696 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000697 }
698}
699
Brian Salomoneebe7352020-12-09 16:37:04 -0500700void GrSurfaceDrawContext::drawRect(const GrClip* clip,
701 GrPaint&& paint,
702 GrAA aa,
703 const SkMatrix& viewMatrix,
704 const SkRect& rect,
705 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700706 if (!style) {
707 style = &GrStyle::SimpleFill();
708 }
joshualitt1de610a2016-01-06 08:26:09 -0800709 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700710 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700711 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500712 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700713
bsalomon6663acf2016-05-10 09:14:17 -0700714 // Path effects should've been devolved to a path in SkGpuDevice
715 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700716
Robert Phillips72152832017-01-25 17:31:35 -0500717 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700718
bsalomon6663acf2016-05-10 09:14:17 -0700719 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400720 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000721 // Fills the rect, using rect as its own local coordinates
722 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500723 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400724 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
725 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
726 (rect.width() && rect.height())) {
727 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
728 // GrStyledShape to handle stroke caps and dashing properly.
Chris Dalton7d6748e2019-03-13 00:34:52 -0600729 GrAAType aaType = this->chooseAAType(aa);
Herb Derbyc76d4092020-10-07 16:46:15 -0400730 GrOp::Owner op = GrStrokeRectOp::Make(
731 fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500732 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
733 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500734 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400735 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700736 return;
robertphillips4bc31812016-03-01 12:22:49 -0800737 }
robertphillips4bc31812016-03-01 12:22:49 -0800738 }
Mike Klein16885072018-12-11 09:54:31 -0500739 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000740 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -0700741 GrStyledShape(rect, *style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -0700742}
743
Brian Salomoneebe7352020-12-09 16:37:04 -0500744void GrSurfaceDrawContext::drawQuadSet(const GrClip* clip,
745 GrPaint&& paint,
746 GrAA aa,
747 const SkMatrix& viewMatrix,
748 const QuadSetEntry quads[],
749 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600750 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500751
752 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
753 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500754}
755
Brian Salomoneebe7352020-12-09 16:37:04 -0500756int GrSurfaceDrawContext::maxWindowRectangles() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500757 return this->asRenderTargetProxy()->maxWindowRectangles(*this->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500758}
759
Brian Salomoneebe7352020-12-09 16:37:04 -0500760GrOpsTask::CanDiscardPreviousOps GrSurfaceDrawContext::canDiscardPreviousOpsOnFullClear() const {
Chris Dalton6b982802019-06-27 13:53:46 -0600761#if GR_TEST_UTILS
762 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400763 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600764 }
765#endif
766 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
767 // would normally be overwritten. The one exception is if the render target context is marked as
768 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
769 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
770 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
771 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400772 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600773}
774
Brian Salomoneebe7352020-12-09 16:37:04 -0500775void GrSurfaceDrawContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600776 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600777 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600778 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600779
Chris Daltoneffee202019-07-01 22:28:03 -0600780 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600781 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500782 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon590f5672020-12-16 11:44:47 -0500783 numRequiredSamples =
784 this->caps()->internalMultisampleCount(this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600785 }
786 SkASSERT(numRequiredSamples > 0);
787
788 if (numRequiredSamples > fNumStencilSamples) {
789 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500790 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600791 }
Chris Dalton6b982802019-06-27 13:53:46 -0600792
Chris Dalton674f77a2019-09-30 20:49:39 -0600793 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600794 if (this->caps()->performStencilClearsAsDraws()) {
795 // There is a driver bug with clearing stencil. We must use an op to manually clear the
796 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400797 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600798 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600799 this->getOpsTask()->setInitialStencilContent(
800 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600801 }
802 }
803}
804
Brian Salomoneebe7352020-12-09 16:37:04 -0500805void GrSurfaceDrawContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600806 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
807
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400808 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
809 if (scissor && !scissorState.set(*scissor)) {
810 // The requested clear region is off screen, so nothing to do.
811 return;
812 }
813
Michael Ludwig1e632792020-05-21 12:45:31 -0400814 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400815 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400816 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500817 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500818
819 // Configure the paint to have no impact on the color buffer
820 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400821 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400822 this->addDrawOp(nullptr,
823 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
824 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500825 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400826 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500827 }
robertphillips976f5f02016-06-03 10:59:20 -0700828}
829
Brian Salomoneebe7352020-12-09 16:37:04 -0500830void GrSurfaceDrawContext::stencilPath(const GrHardClip* clip,
831 GrAA doStencilMSAA,
832 const SkMatrix& viewMatrix,
833 sk_sp<const GrPath> path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500834 ASSERT_SINGLE_OWNER
835 RETURN_IF_ABANDONED
836 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500837 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "stencilPath", fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500838
Brian Salomon467921e2017-03-06 16:17:12 -0500839 // TODO: extract portions of checkDraw that are relevant to path stenciling.
840 SkASSERT(path);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500841 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
Brian Salomon467921e2017-03-06 16:17:12 -0500842
843 // FIXME: Use path bounds instead of this WAR once
844 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500845 SkIRect bounds = SkIRect::MakeSize(this->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -0500846
Michael Ludwig7c12e282020-05-29 09:54:07 -0400847 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
848 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
849 // during this clip application.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500850 GrAppliedHardClip appliedClip(this->dimensions(),
851 this->asSurfaceProxy()->backingStoreDimensions());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400852
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400853 if (clip && GrClip::Effect::kClippedOut == clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500854 return;
855 }
Michael Ludwig7c12e282020-05-29 09:54:07 -0400856 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
857 // but as it is, we're just using the full render target so intersecting the two bounds would
858 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -0500859
Brian Salomon70fe17e2020-11-30 14:33:58 -0500860 GrOp::Owner op = GrStencilPathOp::Make(fContext,
Herb Derbyc76d4092020-10-07 16:46:15 -0400861 viewMatrix,
Brian Salomon70fe17e2020-11-30 14:33:58 -0500862 doStencilMSAA == GrAA::kYes,
Herb Derbyc76d4092020-10-07 16:46:15 -0400863 appliedClip.hasStencilClip(),
864 appliedClip.scissorState(),
865 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400866 if (!op) {
867 return;
868 }
Michael Ludwig6397e802020-08-05 15:50:01 -0400869 op->setClippedBounds(SkRect::Make(bounds));
Chris Dalton6b982802019-06-27 13:53:46 -0600870
Brian Salomon70fe17e2020-11-30 14:33:58 -0500871 this->setNeedsStencil(GrAA::kYes == doStencilMSAA);
872 this->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700873}
874
Brian Salomoneebe7352020-12-09 16:37:04 -0500875void GrSurfaceDrawContext::drawTextureSet(const GrClip* clip,
876 TextureSetEntry set[],
877 int cnt,
878 int proxyRunCnt,
879 GrSamplerState::Filter filter,
880 GrSamplerState::MipmapMode mm,
881 SkBlendMode mode,
882 GrAA aa,
883 SkCanvas::SrcRectConstraint constraint,
884 const SkMatrix& viewMatrix,
885 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400886 ASSERT_SINGLE_OWNER
887 RETURN_IF_ABANDONED
888 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500889 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500890
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500891 // Create the minimum number of GrTextureOps needed to draw this set. Individual
892 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
893 AutoCheckFlush acf(this->drawingManager());
894 GrAAType aaType = this->chooseAAType(aa);
895 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
896 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
897 : GrTextureOp::Saturate::kNo;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400898 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, mm, saturate,
Michael Ludwig379e4962019-12-06 13:21:26 -0500899 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400900}
901
Brian Salomoneebe7352020-12-09 16:37:04 -0500902void GrSurfaceDrawContext::drawVertices(const GrClip* clip,
903 GrPaint&& paint,
904 const SkMatrixProvider& matrixProvider,
905 sk_sp<SkVertices> vertices,
906 GrPrimitiveType* overridePrimType,
907 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -0500908 ASSERT_SINGLE_OWNER
909 RETURN_IF_ABANDONED
910 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500911 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500912
913 AutoCheckFlush acf(this->drawingManager());
914
915 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600916 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400917 GrOp::Owner op =
Brian Osman449b1152020-04-15 16:43:00 -0400918 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -0400919 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -0400920 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -0400921 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700922}
923
924///////////////////////////////////////////////////////////////////////////////
925
Brian Salomoneebe7352020-12-09 16:37:04 -0500926void GrSurfaceDrawContext::drawAtlas(const GrClip* clip,
927 GrPaint&& paint,
928 const SkMatrix& viewMatrix,
929 int spriteCount,
930 const SkRSXform xform[],
931 const SkRect texRect[],
932 const SkColor colors[]) {
Brian Osman4d92b892019-03-24 00:53:23 +0000933 ASSERT_SINGLE_OWNER
934 RETURN_IF_ABANDONED
935 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500936 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAtlas", fContext);
Brian Osman4d92b892019-03-24 00:53:23 +0000937
938 AutoCheckFlush acf(this->drawingManager());
939
940 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400941 GrOp::Owner op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
942 aaType, spriteCount, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +0000943 this->addDrawOp(clip, std::move(op));
944}
945
946///////////////////////////////////////////////////////////////////////////////
947
Brian Salomoneebe7352020-12-09 16:37:04 -0500948void GrSurfaceDrawContext::drawRRect(const GrClip* origClip,
949 GrPaint&& paint,
950 GrAA aa,
951 const SkMatrix& viewMatrix,
952 const SkRRect& rrect,
953 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800954 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700955 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700956 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500957 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400958
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400959 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
960
Robert Phillips85290802018-07-02 13:14:28 -0400961 const SkStrokeRec& stroke = style.strokeRec();
962 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700963 return;
964 }
965
Michael Ludwig7c12e282020-05-29 09:54:07 -0400966 const GrClip* clip = origClip;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400967 // It is not uncommon to clip to a round rect and then draw that same round rect. Since our
968 // lower level clip code works from op bounds, which are SkRects, it doesn't detect that the
969 // clip can be ignored. The following test attempts to mitigate the stencil clip cost but only
970 // works for axis-aligned round rects. This also only works for filled rrects since the stroke
971 // width outsets beyond the rrect itself.
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -0400972 // TODO: skbug.com/10462 - There was mixed performance wins and regressions when this
973 // optimization was turned on outside of Android Framework. I (michaelludwig) believe this is
974 // do to the overhead in determining if an SkClipStack is just a rrect. Once that is improved,
975 // re-enable this and see if we avoid the regressions.
976#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
bsalomon7f0d9f32016-08-15 14:49:10 -0700977 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400978 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400979 rrect.transform(viewMatrix, &devRRect)) {
Michael Ludwig6397e802020-08-05 15:50:01 -0400980 GrClip::PreClipResult result = clip->preApply(devRRect.getBounds(), aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400981 switch(result.fEffect) {
982 case GrClip::Effect::kClippedOut:
983 return;
984 case GrClip::Effect::kUnclipped:
985 clip = nullptr;
986 break;
987 case GrClip::Effect::kClipped:
988 // Currently there's no general-purpose rrect-to-rrect contains function, and if we
989 // got here, we know the devRRect's bounds aren't fully contained by the clip.
990 // Testing for equality between the two is a reasonable stop-gap for now.
991 if (result.fIsRRect && result.fRRect == devRRect) {
992 // NOTE: On the android framework, we allow this optimization even when the clip
993 // is non-AA and the draw is AA.
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400994 if (result.fAA == aa || (result.fAA == GrAA::kNo && aa == GrAA::kYes)) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400995 clip = nullptr;
996 }
997 }
998 break;
999 default:
1000 SkUNREACHABLE;
1001 }
1002 }
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001003#endif
ksakamotoec7f2ac2016-07-05 03:54:53 -07001004
Robert Phillips72152832017-01-25 17:31:35 -05001005 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001006
Chris Dalton7d6748e2019-03-13 00:34:52 -06001007 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001008
Herb Derbyc76d4092020-10-07 16:46:15 -04001009 GrOp::Owner op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001010 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1011 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1012 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1013 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1014 // to avoid perf regressions on some platforms.
1015 assert_alive(paint);
1016 op = GrOvalOpFactory::MakeCircularRRectOp(
1017 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1018 }
1019 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001020 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001021 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001022 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001023 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001024 assert_alive(paint);
1025 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001026 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001027 }
1028 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001029 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001030 return;
robertphillipsea461502015-05-26 11:38:03 -07001031 }
robertphillipsb56f9272016-02-25 11:03:52 -08001032
Mike Klein16885072018-12-11 09:54:31 -05001033 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001034 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001035 GrStyledShape(rrect, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001036}
1037
Jim Van Verthc5903412016-11-17 15:27:09 -05001038///////////////////////////////////////////////////////////////////////////////
1039
Brian Salomoneebe7352020-12-09 16:37:04 -05001040bool GrSurfaceDrawContext::drawFastShadow(const GrClip* clip,
1041 const SkMatrix& viewMatrix,
1042 const SkPath& path,
1043 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001044 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001045 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001046 return true;
1047 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001048 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001049 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001050
1051 // check z plane
1052 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1053 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1054 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1055 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1056 return false;
1057 }
1058
1059 SkRRect rrect;
1060 SkRect rect;
1061 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001062 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001063 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1064 if (!isRRect &&
1065 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1066 rect.width() > SK_ScalarNearlyZero) {
1067 rrect.setOval(rect);
1068 isRRect = true;
1069 }
1070 if (!isRRect && path.isRect(&rect)) {
1071 rrect.setRect(rect);
1072 isRRect = true;
1073 }
1074
1075 if (!isRRect) {
1076 return false;
1077 }
1078
Jim Van Verthc5903412016-11-17 15:27:09 -05001079 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001080 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001081 }
1082
Robert Phillips72152832017-01-25 17:31:35 -05001083 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001084
Jim Van Verth63f03542020-12-16 11:56:11 -05001085 SkPoint3 devLightPos = rec.fLightPos;
1086 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
1087 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -05001088 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -05001089 } else {
1090 // transform light
1091 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
1092 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001093
1094 // 1/scale
1095 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001096 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001097 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1098 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1099
1100 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001101 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1102
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001103 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001104 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1105 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1106 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001107
1108 // Outset the shadow rrect to the border of the penumbra
1109 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1110 SkRRect ambientRRect;
1111 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1112 // If the rrect was an oval then its outset will also be one.
1113 // We set it explicitly to avoid errors.
1114 if (rrect.isOval()) {
1115 ambientRRect = SkRRect::MakeOval(outsetRect);
1116 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001117 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001118 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1119 }
1120
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001121 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001122 if (transparent) {
1123 // set a large inset to force a fill
1124 devSpaceInsetWidth = ambientRRect.width();
1125 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001126
Herb Derbyc76d4092020-10-07 16:46:15 -04001127 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1128 ambientColor,
1129 viewMatrix,
1130 ambientRRect,
1131 devSpaceAmbientBlur,
1132 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001133 if (op) {
1134 this->addDrawOp(clip, std::move(op));
1135 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001136 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001137
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001138 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001139 SkScalar devSpaceSpotBlur;
1140 SkScalar spotScale;
1141 SkVector spotOffset;
Jim Van Verth63f03542020-12-16 11:56:11 -05001142 if (directional) {
1143 SkDrawShadowMetrics::GetDirectionalParams(occluderHeight, devLightPos.fX,
1144 devLightPos.fY, devLightPos.fZ,
1145 rec.fLightRadius, &devSpaceSpotBlur,
1146 &spotScale, &spotOffset);
1147 } else {
1148 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1149 devLightPos.fZ, rec.fLightRadius,
1150 &devSpaceSpotBlur, &spotScale, &spotOffset);
1151 }
Jim Van Verth1af03d42017-07-31 09:34:58 -04001152 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001153 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1154
Jim Van Verth3af1af92017-05-18 15:06:54 -04001155 // Adjust translate for the effect of the scale.
1156 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1157 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1158 // This offset is in dev space, need to transform it into source space.
1159 SkMatrix ctmInverse;
1160 if (viewMatrix.invert(&ctmInverse)) {
1161 ctmInverse.mapPoints(&spotOffset, 1);
1162 } else {
1163 // Since the matrix is a similarity, this should never happen, but just in case...
1164 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1165 SkASSERT(false);
1166 }
1167
1168 // Compute the transformed shadow rrect
1169 SkRRect spotShadowRRect;
1170 SkMatrix shadowTransform;
1171 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1172 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001173 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001174
1175 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001176 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001177 SkScalar insetWidth = blurOutset;
1178 if (transparent) {
1179 // If transparent, just do a fill
1180 insetWidth += spotShadowRRect.width();
1181 } else {
1182 // For shadows, instead of using a stroke we specify an inset from the penumbra
1183 // border. We want to extend this inset area so that it meets up with the caster
1184 // geometry. The inset geometry will by default already be inset by the blur width.
1185 //
1186 // We compare the min and max corners inset by the radius between the original
1187 // rrect and the shadow rrect. The distance between the two plus the difference
1188 // between the scaled radius and the original radius gives the distance from the
1189 // transformed shadow shape to the original shape in that corner. The max
1190 // of these gives the maximum distance we need to cover.
1191 //
1192 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1193 // that to get the full insetWidth.
1194 SkScalar maxOffset;
1195 if (rrect.isRect()) {
1196 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001197 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001198 rrect.rect().fLeft),
1199 SkTAbs(spotShadowRRect.rect().fTop -
1200 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001201 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001202 rrect.rect().fRight),
1203 SkTAbs(spotShadowRRect.rect().fBottom -
1204 rrect.rect().fBottom)));
1205 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001206 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001207 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1208 rrect.rect().fLeft + dr,
1209 spotShadowRRect.rect().fTop -
1210 rrect.rect().fTop + dr);
1211 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1212 rrect.rect().fRight - dr,
1213 spotShadowRRect.rect().fBottom -
1214 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001215 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001216 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001217 }
Brian Osman788b9162020-02-07 10:36:46 -05001218 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001219 }
1220
1221 // Outset the shadow rrect to the border of the penumbra
1222 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1223 if (spotShadowRRect.isOval()) {
1224 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1225 } else {
1226 SkScalar outsetRad = spotRadius + blurOutset;
1227 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1228 }
1229
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001230 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001231
Herb Derbyc76d4092020-10-07 16:46:15 -04001232 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1233 spotColor,
1234 viewMatrix,
1235 spotShadowRRect,
Brian Salomoneebe7352020-12-09 16:37:04 -05001236 2.0f * devSpaceSpotBlur,
Herb Derbyc76d4092020-10-07 16:46:15 -04001237 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001238 if (op) {
1239 this->addDrawOp(clip, std::move(op));
1240 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001241 }
1242
1243 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001244}
1245
1246///////////////////////////////////////////////////////////////////////////////
1247
Brian Salomoneebe7352020-12-09 16:37:04 -05001248bool GrSurfaceDrawContext::drawFilledDRRect(const GrClip* clip,
1249 GrPaint&& paint,
1250 GrAA aa,
1251 const SkMatrix& viewMatrix,
1252 const SkRRect& origOuter,
1253 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001254 SkASSERT(!origInner.isEmpty());
1255 SkASSERT(!origOuter.isEmpty());
1256
Brian Salomon65749212017-12-01 16:01:47 -05001257 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1258
Chris Dalton7d6748e2019-03-13 00:34:52 -06001259 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001260
1261 if (GrAAType::kMSAA == aaType) {
1262 return false;
1263 }
1264
Greg Daniel2655ede2019-04-10 00:49:28 +00001265 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1266 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001267 auto outerR = outer->width() / 2.f;
1268 auto innerR = inner->width() / 2.f;
1269 auto cx = outer->getBounds().fLeft + outerR;
1270 auto cy = outer->getBounds().fTop + outerR;
1271 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1272 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1273 auto avgR = (innerR + outerR) / 2.f;
1274 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1275 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1276 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001277 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001278 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001279 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001280 if (op) {
1281 this->addDrawOp(clip, std::move(op));
1282 return true;
1283 }
Mike Klein16885072018-12-11 09:54:31 -05001284 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001285 }
1286 }
1287
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001288 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001289 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001290 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1291 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001292 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001293 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1294 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001295 }
robertphillips00095892016-02-29 13:50:40 -08001296
robertphillips00095892016-02-29 13:50:40 -08001297 SkMatrix inverseVM;
1298 if (!viewMatrix.isIdentity()) {
1299 if (!origInner.transform(viewMatrix, inner.writable())) {
1300 return false;
1301 }
1302 if (!origOuter.transform(viewMatrix, outer.writable())) {
1303 return false;
1304 }
1305 if (!viewMatrix.invert(&inverseVM)) {
1306 return false;
1307 }
1308 } else {
1309 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001310 }
robertphillips00095892016-02-29 13:50:40 -08001311
Ethan Nicholaseace9352018-10-15 20:09:54 +00001312 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001313 // TODO these need to be a geometry processors
John Stiles851b90e2020-06-17 13:53:37 -04001314 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, innerEdgeType, *inner, caps);
1315 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001316 return false;
1317 }
1318
John Stiles851b90e2020-06-17 13:53:37 -04001319 std::tie(success, fp) = GrRRectEffect::Make(std::move(fp), outerEdgeType, *outer, caps);
1320 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001321 return false;
1322 }
1323
John Stiles41d91b62020-07-21 14:39:40 -04001324 paint.setCoverageFragmentProcessor(std::move(fp));
robertphillips00095892016-02-29 13:50:40 -08001325
1326 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001327 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001328 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1329 }
halcanary9d524f22016-03-29 09:03:52 -07001330
Brian Salomon82f44312017-01-11 13:42:54 -05001331 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1332 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001333 return true;
1334}
1335
Brian Salomoneebe7352020-12-09 16:37:04 -05001336void GrSurfaceDrawContext::drawDRRect(const GrClip* clip,
1337 GrPaint&& paint,
1338 GrAA aa,
1339 const SkMatrix& viewMatrix,
1340 const SkRRect& outer,
1341 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001342 ASSERT_SINGLE_OWNER
1343 RETURN_IF_ABANDONED
1344 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001345 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001346
1347 SkASSERT(!outer.isEmpty());
1348 SkASSERT(!inner.isEmpty());
1349
Robert Phillips72152832017-01-25 17:31:35 -05001350 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001351
Brian Salomon82f44312017-01-11 13:42:54 -05001352 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001353 return;
1354 }
Mike Klein16885072018-12-11 09:54:31 -05001355 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001356
1357 SkPath path;
1358 path.setIsVolatile(true);
1359 path.addRRect(inner);
1360 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001361 path.setFillType(SkPathFillType::kEvenOdd);
Chris Daltonfbc3a832021-02-15 23:16:17 -07001362 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1363 GrStyledShape(path, DoSimplify::kNo));
robertphillips00095892016-02-29 13:50:40 -08001364}
1365
robertphillipsea461502015-05-26 11:38:03 -07001366///////////////////////////////////////////////////////////////////////////////
1367
Brian Salomoneebe7352020-12-09 16:37:04 -05001368void GrSurfaceDrawContext::drawRegion(const GrClip* clip,
1369 GrPaint&& paint,
1370 GrAA aa,
1371 const SkMatrix& viewMatrix,
1372 const SkRegion& region,
1373 const GrStyle& style,
1374 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001375 ASSERT_SINGLE_OWNER
1376 RETURN_IF_ABANDONED
1377 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001378 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001379
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001380 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001381 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001382 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001383 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001384 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1385 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001386 aa = GrAA::kNo;
1387 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001388 }
msarettcc319b92016-08-25 18:07:18 -07001389 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001390 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001391 SkPath path;
1392 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001393 path.setIsVolatile(true);
1394
Brian Salomon82f44312017-01-11 13:42:54 -05001395 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001396 }
1397
Chris Dalton7d6748e2019-03-13 00:34:52 -06001398 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001399 GrOp::Owner op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1400 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001401 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001402}
1403
Brian Salomoneebe7352020-12-09 16:37:04 -05001404void GrSurfaceDrawContext::drawOval(const GrClip* clip,
1405 GrPaint&& paint,
1406 GrAA aa,
1407 const SkMatrix& viewMatrix,
1408 const SkRect& oval,
1409 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001410 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001411 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001412 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001413 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001414
Robert Phillips7484d202018-07-03 09:09:08 -04001415 const SkStrokeRec& stroke = style.strokeRec();
1416
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001417 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001418 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1419 return;
1420 }
1421
1422 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001423 return;
robertphillipsea461502015-05-26 11:38:03 -07001424 }
1425
Robert Phillips72152832017-01-25 17:31:35 -05001426 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001427
Chris Dalton7d6748e2019-03-13 00:34:52 -06001428 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001429
Herb Derbyc76d4092020-10-07 16:46:15 -04001430 GrOp::Owner op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001431 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1432 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001433 // We don't draw true circles as round rects in coverage mode, because it can
1434 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1435 assert_alive(paint);
1436 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1437 this->caps()->shaderCaps());
1438 }
1439 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001440 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1441 // the arc equation. This same special geometry and fragment branch also turn out to be a
1442 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1443 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1444 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001445 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001446 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1447 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001448 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001449 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001450 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001451 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1452 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001453 }
1454 if (op) {
1455 this->addDrawOp(clip, std::move(op));
1456 return;
robertphillipsea461502015-05-26 11:38:03 -07001457 }
robertphillipsb56f9272016-02-25 11:03:52 -08001458
Mike Klein16885072018-12-11 09:54:31 -05001459 assert_alive(paint);
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001460 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1461 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001462 false, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001463}
1464
Brian Salomoneebe7352020-12-09 16:37:04 -05001465void GrSurfaceDrawContext::drawArc(const GrClip* clip,
1466 GrPaint&& paint,
1467 GrAA aa,
1468 const SkMatrix& viewMatrix,
1469 const SkRect& oval,
1470 SkScalar startAngle,
1471 SkScalar sweepAngle,
1472 bool useCenter,
1473 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001474 ASSERT_SINGLE_OWNER
1475 RETURN_IF_ABANDONED
1476 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001477 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001478
1479 AutoCheckFlush acf(this->drawingManager());
1480
Chris Dalton7d6748e2019-03-13 00:34:52 -06001481 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001482 if (GrAAType::kCoverage == aaType) {
1483 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Herb Derbyc76d4092020-10-07 16:46:15 -04001484 GrOp::Owner op = GrOvalOpFactory::MakeArcOp(fContext,
1485 std::move(paint),
1486 viewMatrix,
1487 oval,
1488 startAngle,
1489 sweepAngle,
1490 useCenter,
1491 style,
1492 shaderCaps);
Greg Daniel2655ede2019-04-10 00:49:28 +00001493 if (op) {
1494 this->addDrawOp(clip, std::move(op));
1495 return;
1496 }
1497 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001498 }
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001499 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1500 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001501 style, DoSimplify::kNo));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001502}
1503
Brian Salomoneebe7352020-12-09 16:37:04 -05001504void GrSurfaceDrawContext::drawImageLattice(const GrClip* clip,
1505 GrPaint&& paint,
1506 const SkMatrix& viewMatrix,
1507 GrSurfaceProxyView view,
1508 SkAlphaType alphaType,
1509 sk_sp<GrColorSpaceXform> csxf,
1510 GrSamplerState::Filter filter,
1511 std::unique_ptr<SkLatticeIter> iter,
1512 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001513 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001514 RETURN_IF_ABANDONED
1515 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001516 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001517
Robert Phillips72152832017-01-25 17:31:35 -05001518 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001519
Herb Derbyc76d4092020-10-07 16:46:15 -04001520 GrOp::Owner op =
Greg Danieled96bca2019-12-05 15:05:54 -05001521 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001522 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001523 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001524}
1525
Brian Salomoneebe7352020-12-09 16:37:04 -05001526void GrSurfaceDrawContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001527 const SkRect& bounds) {
Herb Derbyc76d4092020-10-07 16:46:15 -04001528 GrOp::Owner op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001529 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001530 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001531}
1532
Brian Salomoneebe7352020-12-09 16:37:04 -05001533bool GrSurfaceDrawContext::waitOnSemaphores(int numSemaphores,
1534 const GrBackendSemaphore waitSemaphores[],
1535 bool deleteSemaphoresAfterWait) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001536 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001537 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001538 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001539 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001540
1541 AutoCheckFlush acf(this->drawingManager());
1542
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001543 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001544 return false;
1545 }
1546
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001547 auto direct = fContext->asDirectContext();
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001548 if (!direct) {
1549 return false;
1550 }
1551
1552 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001553
Greg Daniel414418d2020-07-08 11:44:25 -04001554 GrWrapOwnership ownership =
1555 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
1556
Greg Daniel301015c2019-11-18 14:06:46 -05001557 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1558 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001559 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001560 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -04001561 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001562 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001563 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1564 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001565 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001566}
joshualitt33a5fce2015-11-18 13:28:51 -08001567
Brian Salomoneebe7352020-12-09 16:37:04 -05001568void GrSurfaceDrawContext::drawPath(const GrClip* clip,
1569 GrPaint&& paint,
1570 GrAA aa,
1571 const SkMatrix& viewMatrix,
1572 const SkPath& path,
1573 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001574 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001575 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001576 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001577 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawPath", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001578
Chris Daltonfbc3a832021-02-15 23:16:17 -07001579 GrStyledShape shape(path, style, DoSimplify::kNo);
Chris Dalton21859012021-01-29 23:44:53 -07001580 this->drawShape(clip, std::move(paint), aa, viewMatrix, std::move(shape));
Robert Phillips20390c32018-08-17 11:01:03 -04001581}
1582
Brian Salomoneebe7352020-12-09 16:37:04 -05001583void GrSurfaceDrawContext::drawShape(const GrClip* clip,
1584 GrPaint&& paint,
1585 GrAA aa,
1586 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001587 GrStyledShape&& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04001588 ASSERT_SINGLE_OWNER
1589 RETURN_IF_ABANDONED
1590 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001591 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawShape", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001592
Brian Salomon2fad74a2017-12-20 13:28:55 -05001593 if (shape.isEmpty()) {
1594 if (shape.inverseFilled()) {
1595 this->drawPaint(clip, std::move(paint), viewMatrix);
1596 }
1597 return;
robertphillipsea461502015-05-26 11:38:03 -07001598 }
1599
Robert Phillips72152832017-01-25 17:31:35 -05001600 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001601
Michael Ludwig6a6de652020-04-30 20:16:36 -04001602 // If we get here in drawShape(), we definitely need to use path rendering
Chris Dalton21859012021-01-29 23:44:53 -07001603 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, std::move(shape),
Chris Dalton9f704a82021-02-04 15:07:56 -07001604 /* attemptDrawSimple */ true);
robertphillipsea461502015-05-26 11:38:03 -07001605}
1606
Brian Salomoneebe7352020-12-09 16:37:04 -05001607static SkIRect get_clip_bounds(const GrSurfaceDrawContext* rtc, const GrClip* clip) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001608 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
1609}
1610
Brian Salomoneebe7352020-12-09 16:37:04 -05001611bool GrSurfaceDrawContext::drawAndStencilPath(const GrHardClip* clip,
1612 const GrUserStencilSettings* ss,
1613 SkRegion::Op op,
1614 bool invert,
1615 GrAA aa,
1616 const SkMatrix& viewMatrix,
1617 const SkPath& path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001618 ASSERT_SINGLE_OWNER
1619 RETURN_FALSE_IF_ABANDONED
1620 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001621 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAndStencilPath", fContext);
robertphillips391395d2016-03-02 09:26:36 -08001622
1623 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001624 GrPaint paint;
1625 paint.setCoverageSetOpXPFactory(op, invert);
1626 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon70fe17e2020-11-30 14:33:58 -05001627 SkRect::Make(this->dimensions()));
robertphillips391395d2016-03-02 09:26:36 -08001628 return true;
1629 }
1630
Brian Salomon70fe17e2020-11-30 14:33:58 -05001631 AutoCheckFlush acf(this->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001632
1633 // An Assumption here is that path renderer would use some form of tweaking
1634 // the src color (either the input alpha or in the frag shader) to implement
1635 // aa. If we have some future driver-mojo path AA that can do the right
1636 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon70fe17e2020-11-30 14:33:58 -05001637 GrAAType aaType = this->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001638 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001639
Brian Salomon70fe17e2020-11-30 14:33:58 -05001640 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001641
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001642 GrPaint paint;
1643 paint.setCoverageSetOpXPFactory(op, invert);
1644
Michael Ludwig2686d692020-04-17 20:21:37 +00001645 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001646 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001647 canDrawArgs.fCaps = this->caps();
1648 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08001649 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001650 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001651 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001652 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001653 canDrawArgs.fAAType = aaType;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001654 SkASSERT(!this->wrapsVkSecondaryCB());
Greg Danielbe7fc462019-01-03 16:40:42 -05001655 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001656 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001657
1658 // Don't allow the SW renderer
Brian Salomon70fe17e2020-11-30 14:33:58 -05001659 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001660 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001661 if (!pr) {
1662 return false;
1663 }
1664
Brian Salomon70fe17e2020-11-30 14:33:58 -05001665 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001666 std::move(paint),
1667 ss,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001668 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001669 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04001670 &clipConservativeBounds,
1671 &viewMatrix,
1672 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001673 aaType,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001674 this->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001675 pr->drawPath(args);
1676 return true;
1677}
1678
Brian Salomoneebe7352020-12-09 16:37:04 -05001679SkBudgeted GrSurfaceDrawContext::isBudgeted() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001680 ASSERT_SINGLE_OWNER
robertphillips714712b2016-08-04 06:20:45 -07001681
Brian Salomon70fe17e2020-11-30 14:33:58 -05001682 if (fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001683 return SkBudgeted::kNo;
1684 }
1685
Brian Salomon70fe17e2020-11-30 14:33:58 -05001686 SkDEBUGCODE(this->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001687
Brian Salomon70fe17e2020-11-30 14:33:58 -05001688 return this->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001689}
1690
Chris Dalton319d0202021-01-29 23:49:47 -07001691void GrSurfaceDrawContext::drawStrokedLine(const GrClip* clip, GrPaint&& paint,
1692 GrAA aa, const SkMatrix& viewMatrix,
1693 const SkPoint points[2], const SkStrokeRec& stroke) {
1694 ASSERT_SINGLE_OWNER
1695
1696 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style);
1697 SkASSERT(stroke.getWidth() > 0);
1698 // Adding support for round capping would require a
1699 // GrSurfaceDrawContext::fillRRectWithLocalMatrix entry point
1700 SkASSERT(SkPaint::kRound_Cap != stroke.getCap());
1701
1702 const SkScalar halfWidth = 0.5f * stroke.getWidth();
1703 if (halfWidth <= 0.f) {
1704 // Prevents underflow when stroke width is epsilon > 0 (so technically not a hairline).
1705 // The CTM would need to have a scale near 1/epsilon in order for this to have meaningful
1706 // coverage (although that would likely overflow elsewhere and cause the draw to drop due
1707 // to non-finite bounds). At any other scale, this line is so thin, it's coverage is
1708 // negligible, so discarding the draw is visually equivalent.
1709 return;
1710 }
1711
1712 SkVector parallel = points[1] - points[0];
1713
1714 if (!SkPoint::Normalize(&parallel)) {
1715 parallel.fX = 1.0f;
1716 parallel.fY = 0.0f;
1717 }
1718 parallel *= halfWidth;
1719
1720 SkVector ortho = { parallel.fY, -parallel.fX };
1721 if (SkPaint::kButt_Cap == stroke.getCap()) {
1722 // No extra extension for butt caps
1723 parallel = {0.f, 0.f};
1724 }
1725 // Order is TL, TR, BR, BL where arbitrarily "down" is p0 to p1 and "right" is positive
1726 SkPoint corners[4] = { points[0] - ortho - parallel,
1727 points[0] + ortho - parallel,
1728 points[1] + ortho + parallel,
1729 points[1] - ortho + parallel };
1730
1731 GrQuadAAFlags edgeAA = (aa == GrAA::kYes) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
1732 this->fillQuadWithEdgeAA(clip, std::move(paint), aa, edgeAA, viewMatrix, corners, nullptr);
1733}
1734
Chris Daltonbfe0d372021-02-04 12:24:54 -07001735bool GrSurfaceDrawContext::drawSimpleShape(const GrClip* clip, GrPaint* paint, GrAA aa,
1736 const SkMatrix& viewMatrix, const GrStyledShape& shape) {
1737 if (!shape.style().hasPathEffect()) {
1738 GrAAType aaType = this->chooseAAType(aa);
1739 SkPoint linePts[2];
1740 SkRRect rrect;
1741 // We can ignore the starting point and direction since there is no path effect.
1742 bool inverted;
1743 if (shape.asLine(linePts, &inverted) && !inverted &&
1744 shape.style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style &&
1745 shape.style().strokeRec().getCap() != SkPaint::kRound_Cap) {
1746 // The stroked line is an oriented rectangle, which looks the same or better (if
1747 // perspective) compared to path rendering. The exception is subpixel/hairline lines
1748 // that are non-AA or MSAA, in which case the default path renderer achieves higher
1749 // quality.
1750 // FIXME(michaelludwig): If the fill rect op could take an external coverage, or checks
1751 // for and outsets thin non-aa rects to 1px, the path renderer could be skipped.
1752 SkScalar coverage;
1753 if (aaType == GrAAType::kCoverage ||
1754 !SkDrawTreatAAStrokeAsHairline(shape.style().strokeRec().getWidth(), viewMatrix,
1755 &coverage)) {
1756 this->drawStrokedLine(clip, std::move(*paint), aa, viewMatrix, linePts,
1757 shape.style().strokeRec());
1758 return true;
1759 }
1760 } else if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1761 if (rrect.isRect()) {
1762 this->drawRect(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1763 &shape.style());
1764 return true;
1765 } else if (rrect.isOval()) {
1766 this->drawOval(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1767 shape.style());
1768 return true;
1769 }
1770 this->drawRRect(clip, std::move(*paint), aa, viewMatrix, rrect, shape.style());
1771 return true;
1772 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1773 viewMatrix.rectStaysRect()) {
1774 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply the
1775 // matrix to all the points individually rather than just to the rect
1776 SkRect rects[2];
1777 if (shape.asNestedRects(rects)) {
1778 // Concave AA paths are expensive - try to avoid them for special cases
1779 GrOp::Owner op = GrStrokeRectOp::MakeNested(
1780 fContext, std::move(*paint), viewMatrix, rects);
1781 if (op) {
1782 this->addDrawOp(clip, std::move(op));
1783 }
1784 // Returning here indicates that there is nothing to draw in this case.
1785 return true;
1786 }
1787 }
1788 }
1789 return false;
1790}
1791
Brian Salomoneebe7352020-12-09 16:37:04 -05001792void GrSurfaceDrawContext::drawShapeUsingPathRenderer(const GrClip* clip,
1793 GrPaint&& paint,
1794 GrAA aa,
1795 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001796 GrStyledShape&& shape,
Chris Daltonbfe0d372021-02-04 12:24:54 -07001797 bool attemptDrawSimple) {
joshualitt1de610a2016-01-06 08:26:09 -08001798 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001799 RETURN_IF_ABANDONED
Brian Salomoneebe7352020-12-09 16:37:04 -05001800 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "internalDrawPath", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001801
Chris Dalton21859012021-01-29 23:44:53 -07001802 if (!viewMatrix.isFinite() || !shape.bounds().isFinite()) {
Jim Van Verthf86073a2018-10-02 13:05:38 -04001803 return;
1804 }
1805
Michael Ludwig7c12e282020-05-29 09:54:07 -04001806 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001807
Chris Dalton6ce447a2019-06-23 18:07:38 -06001808 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06001809
robertphillips68737822015-10-29 12:12:21 -07001810 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001811 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05001812 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07001813 canDrawArgs.fViewMatrix = &viewMatrix;
Chris Dalton21859012021-01-29 23:44:53 -07001814 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001815 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001816 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001817 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001818 canDrawArgs.fHasUserStencilSettings = false;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001819 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001820
Chris Daltone6ae4762021-02-05 14:56:21 -07001821 constexpr static bool kDisallowSWPathRenderer = false;
1822 constexpr static bool kAllowSWPathRenderer = true;
1823 using DrawType = GrPathRendererChain::DrawType;
1824
1825 GrPathRenderer* pr = nullptr;
1826
1827 if (!shape.style().strokeRec().isFillStyle() && !shape.isEmpty()) {
1828 // Give the tessellation path renderer a chance to claim this stroke before we simplify it.
1829 GrPathRenderer* tess = this->drawingManager()->getTessellationPathRenderer();
1830 if (tess && tess->canDrawPath(canDrawArgs) == GrPathRenderer::CanDrawPath::kYes) {
1831 pr = tess;
1832 }
1833 }
1834
1835 if (!pr) {
1836 // The shape isn't a stroke that can be drawn directly. Simplify if possible.
Chris Daltonfbc3a832021-02-15 23:16:17 -07001837 shape.simplify();
Chris Daltone6ae4762021-02-05 14:56:21 -07001838
1839 if (shape.isEmpty() && !shape.inverseFilled()) {
1840 return;
1841 }
1842
1843 if (attemptDrawSimple || shape.simplified()) {
1844 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too complex
1845 // for dedicated draw ops. However, if GrStyledShape was able to reduce something we
1846 // ought to try again instead of going right to path rendering.
1847 if (this->drawSimpleShape(clip, &paint, aa, viewMatrix, shape)) {
1848 return;
1849 }
1850 }
1851
1852 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1853 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1854 DrawType::kColor);
1855 }
1856
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001857 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -05001858 if (styleScale == 0.0f) {
1859 return;
1860 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001861
Chris Dalton21859012021-01-29 23:44:53 -07001862 if (!pr && shape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001863 // It didn't work above, so try again with the path effect applied.
Chris Dalton21859012021-01-29 23:44:53 -07001864 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1865 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001866 return;
1867 }
Chris Daltone6ae4762021-02-05 14:56:21 -07001868 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1869 DrawType::kColor);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001870 }
1871 if (!pr) {
Chris Dalton21859012021-01-29 23:44:53 -07001872 if (shape.style().applies()) {
1873 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1874 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001875 return;
1876 }
Brian Salomone7df0bb2018-05-07 14:44:57 -04001877 // This time, allow SW renderer
Chris Daltone6ae4762021-02-05 14:56:21 -07001878 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kAllowSWPathRenderer,
1879 DrawType::kColor);
Brian Salomone7df0bb2018-05-07 14:44:57 -04001880 } else {
1881 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04001882#if GR_PATH_RENDERER_SPEW
1883 SkDebugf("falling back to: %s\n", pr->name());
1884#endif
bsalomon6663acf2016-05-10 09:14:17 -07001885 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001886 }
robertphillipsea461502015-05-26 11:38:03 -07001887
bsalomon8acedde2016-06-24 10:42:16 -07001888 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001889#ifdef SK_DEBUG
1890 SkDebugf("Unable to find path renderer compatible with path.\n");
1891#endif
1892 return;
1893 }
1894
Robert Phillips256c37b2017-03-01 14:32:46 -05001895 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001896 std::move(paint),
1897 &GrUserStencilSettings::kUnused,
1898 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001899 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001900 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001901 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001902 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001903 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001904 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001905 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001906}
1907
Brian Salomon467921e2017-03-06 16:17:12 -05001908static void op_bounds(SkRect* bounds, const GrOp* op) {
1909 *bounds = op->bounds();
1910 if (op->hasZeroArea()) {
1911 if (op->hasAABloat()) {
1912 bounds->outset(0.5f, 0.5f);
1913 } else {
1914 // We don't know which way the particular GPU will snap lines or points at integer
1915 // coords. So we ensure that the bounds is large enough for either snap.
1916 SkRect before = *bounds;
1917 bounds->roundOut(bounds);
1918 if (bounds->fLeft == before.fLeft) {
1919 bounds->fLeft -= 1;
1920 }
1921 if (bounds->fTop == before.fTop) {
1922 bounds->fTop -= 1;
1923 }
1924 if (bounds->fRight == before.fRight) {
1925 bounds->fRight += 1;
1926 }
1927 if (bounds->fBottom == before.fBottom) {
1928 bounds->fBottom += 1;
1929 }
1930 }
1931 }
1932}
1933
Brian Salomoneebe7352020-12-09 16:37:04 -05001934void GrSurfaceDrawContext::addDrawOp(const GrClip* clip,
1935 GrOp::Owner op,
1936 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001937 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001938 if (fContext->abandoned()) {
Brian Salomon348a0372018-10-31 10:42:18 -04001939 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001940 }
Herb Derbyc76d4092020-10-07 16:46:15 -04001941 GrDrawOp* drawOp = (GrDrawOp*)op.get();
robertphillips2e1e51f2015-10-15 08:01:48 -07001942 SkDEBUGCODE(this->validate();)
Herb Derbyc76d4092020-10-07 16:46:15 -04001943 SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;)
Brian Salomoneebe7352020-12-09 16:37:04 -05001944 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001945
Brian Salomon467921e2017-03-06 16:17:12 -05001946 // Setup clip
1947 SkRect bounds;
1948 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001949 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Herb Derbyc76d4092020-10-07 16:46:15 -04001950 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = drawOp->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06001951 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07001952 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06001953
Chris Dalton9acf6822019-11-12 11:52:40 -07001954 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06001955 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06001956 }
1957
Michael Ludwig7c12e282020-05-29 09:54:07 -04001958 bool skipDraw = false;
1959 if (clip) {
1960 // Have a complex clip, so defer to its early clip culling
Brian Salomon590f5672020-12-16 11:44:47 -05001961 GrAAType aaType;
1962 if (usesHWAA) {
1963 aaType = GrAAType::kMSAA;
1964 } else {
1965 aaType = op->hasAABloat() ? GrAAType::kCoverage : GrAAType::kNone;
1966 }
Michael Ludwig6397e802020-08-05 15:50:01 -04001967 skipDraw = clip->apply(fContext, this, aaType, usesUserStencilBits,
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001968 &appliedClip, &bounds) == GrClip::Effect::kClippedOut;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001969 } else {
1970 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001971 skipDraw = !bounds.intersect(this->asSurfaceProxy()->getBoundsRect());
Michael Ludwig7c12e282020-05-29 09:54:07 -04001972 }
1973
1974 if (skipDraw) {
Brian Salomon348a0372018-10-31 10:42:18 -04001975 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001976 }
1977
Chris Dalton9acf6822019-11-12 11:52:40 -07001978 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
1979 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
1980
1981 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
1982 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
1983 // that uses a stencil test when the stencil buffer is multisampled.)
1984 bool hasMixedSampledCoverage = (
1985 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05001986 SkASSERT(!hasMixedSampledCoverage ||
1987 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04001988
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001989 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Herb Derbyc76d4092020-10-07 16:46:15 -04001990 GrProcessorSet::Analysis analysis = drawOp->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06001991 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
1992
Michael Ludwig28e5f112020-06-09 10:57:24 -04001993 // Must be called before setDstProxyView so that it sees the final bounds of the op.
1994 op->setClippedBounds(bounds);
1995
Greg Daniel524e28b2019-11-01 11:48:53 -04001996 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07001997 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04001998 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Brian Salomon348a0372018-10-31 10:42:18 -04001999 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002000 }
2001 }
2002
Greg Danielf41b2bd2019-08-22 16:19:24 -04002003 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002004 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002005 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002006 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002007 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -04002008 dstProxyView, GrTextureResolveManager(this->drawingManager()),
2009 *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002010}
2011
Brian Salomoneebe7352020-12-09 16:37:04 -05002012bool GrSurfaceDrawContext::setupDstProxyView(const GrOp& op,
2013 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002014 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2015 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2016 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002017 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002018 return false;
2019 }
2020
Greg Danield358cbe2020-09-11 09:33:54 -04002021 if (fDstSampleType == GrDstSampleType::kNone) {
2022 fDstSampleType = this->caps()->getDstSampleTypeForProxy(this->asRenderTargetProxy());
Brian Salomon467921e2017-03-06 16:17:12 -05002023 }
Greg Danield358cbe2020-09-11 09:33:54 -04002024 SkASSERT(fDstSampleType != GrDstSampleType::kNone);
2025
2026 if (GrDstSampleTypeDirectlySamplesDst(fDstSampleType)) {
2027 // The render target is a texture or input attachment, so we can read from it directly in
2028 // the shader. The XP will be responsible to detect this situation and request a texture
2029 // barrier.
2030 dstProxyView->setProxyView(this->readSurfaceView());
2031 dstProxyView->setOffset(0, 0);
2032 dstProxyView->setDstSampleType(fDstSampleType);
2033 return true;
2034 }
2035 SkASSERT(fDstSampleType == GrDstSampleType::kAsTextureCopy);
Brian Salomon467921e2017-03-06 16:17:12 -05002036
Greg Daniel3155f7f2020-01-16 16:54:26 -05002037 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002038 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2039 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002040 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002041 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002042
Michael Ludwig28e5f112020-06-09 10:57:24 -04002043 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002044 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002045 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2046 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2047 // centers during rasterization.
2048 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2049 conservativeDrawBounds.outset(1, 1);
2050 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002051 }
Brian Salomon467921e2017-03-06 16:17:12 -05002052
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002053 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002054 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002055 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002056 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002057 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002058 } else {
Eric Karl74480882017-04-03 14:49:05 -07002059 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002060 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002061 }
Brian Salomon982127b2021-01-21 10:43:35 -05002062 auto copy = GrSurfaceProxy::Copy(fContext,
2063 this->asSurfaceProxyRef(),
2064 this->origin(),
2065 GrMipmapped::kNo,
2066 copyRect,
2067 fit,
2068 SkBudgeted::kYes,
2069 restrictions.fRectsMustMatch);
Brian Salomonc5243782020-04-02 12:50:34 -04002070 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002071
Brian Salomonc5243782020-04-02 12:50:34 -04002072 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002073 dstProxyView->setOffset(dstOffset);
Greg Danield358cbe2020-09-11 09:33:54 -04002074 dstProxyView->setDstSampleType(fDstSampleType);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002075 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002076}