blob: 5a58dac56f45bd986942ce2810320233ab2652e2 [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"
Chris Dalton61d694d2021-03-22 00:00:52 -060017#include "include/private/SkVx.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040019#include "src/core/SkAutoPixmapStorage.h"
20#include "src/core/SkConvertPixels.h"
Chris Dalton319d0202021-01-29 23:49:47 -070021#include "src/core/SkDrawProcs.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkDrawShadowInfo.h"
23#include "src/core/SkGlyphRunPainter.h"
24#include "src/core/SkLatticeIter.h"
25#include "src/core/SkMatrixPriv.h"
Herb Derby64688222020-06-08 14:19:57 -040026#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/core/SkRRectPriv.h"
28#include "src/core/SkSurfacePriv.h"
29#include "src/gpu/GrAppliedClip.h"
Greg Danielc0d69152020-10-08 14:59:00 -040030#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrBlurUtils.h"
33#include "src/gpu/GrCaps.h"
Michael Ludwig58f569b2020-05-21 17:03:08 -040034#include "src/gpu/GrClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040035#include "src/gpu/GrColor.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040036#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040037#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040040#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040041#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrMemoryPool.h"
43#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040044#include "src/gpu/GrProxyProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040045#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrResourceProvider.h"
Robert Phillips31798c22021-03-18 13:32:56 -040047#include "src/gpu/GrStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/GrStyle.h"
49#include "src/gpu/GrTracing.h"
50#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040051#include "src/gpu/effects/GrBicubicEffect.h"
Chris Dalton4718a8b2021-03-18 18:38:52 -060052#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "src/gpu/effects/GrRRectEffect.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040054#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000055#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000056#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "src/gpu/ops/GrAtlasTextOp.h"
58#include "src/gpu/ops/GrClearOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059#include "src/gpu/ops/GrDrawAtlasOp.h"
60#include "src/gpu/ops/GrDrawOp.h"
61#include "src/gpu/ops/GrDrawVerticesOp.h"
62#include "src/gpu/ops/GrDrawableOp.h"
63#include "src/gpu/ops/GrFillRRectOp.h"
64#include "src/gpu/ops/GrFillRectOp.h"
65#include "src/gpu/ops/GrLatticeOp.h"
66#include "src/gpu/ops/GrOp.h"
67#include "src/gpu/ops/GrOvalOpFactory.h"
68#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069#include "src/gpu/ops/GrShadowRRectOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050070#include "src/gpu/ops/GrStrokeRectOp.h"
71#include "src/gpu/ops/GrTextureOp.h"
Chris Dalton43a8b0c2021-06-14 17:10:07 -060072#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050073#include "src/gpu/text/GrSDFTControl.h"
Herb Derby64688222020-06-08 14:19:57 -040074#include "src/gpu/text/GrTextBlobCache.h"
joshualittbc907352016-01-13 06:45:40 -080075
Robert Phillips72152832017-01-25 17:31:35 -050076#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -040077#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040078#define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
Robert Phillips9eb00022020-06-30 15:30:12 -040079#define RETURN_FALSE_IF_ABANDONED if (fContext->abandoned()) { return false; }
robertphillipsea461502015-05-26 11:38:03 -070080
Brian Salomone225b562017-06-14 13:00:03 -040081//////////////////////////////////////////////////////////////////////////////
82
Chris Daltonfbc3a832021-02-15 23:16:17 -070083using DoSimplify = GrStyledShape::DoSimplify;
Chris Daltonb8ed2bc2021-02-02 11:53:19 -070084
robertphillipsea461502015-05-26 11:38:03 -070085class AutoCheckFlush {
86public:
halcanary9d524f22016-03-29 09:03:52 -070087 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070088 SkASSERT(fDrawingManager);
89 }
bsalomonb77a9072016-09-07 10:02:04 -070090 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070091
92private:
robertphillips77a2e522015-10-17 07:43:27 -070093 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070094};
95
Robert Phillips779125d2021-05-24 13:14:39 -040096std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(GrRecordingContext* rContext,
Brian Salomoneebe7352020-12-09 16:37:04 -050097 GrColorType colorType,
Brian Salomoneebe7352020-12-09 16:37:04 -050098 sk_sp<GrSurfaceProxy> proxy,
Robert Phillips779125d2021-05-24 13:14:39 -040099 sk_sp<SkColorSpace> colorSpace,
Brian Salomoneebe7352020-12-09 16:37:04 -0500100 GrSurfaceOrigin origin,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600101 const SkSurfaceProps& surfaceProps,
Brian Salomoneebe7352020-12-09 16:37:04 -0500102 bool flushTimeOpsTask) {
Robert Phillips779125d2021-05-24 13:14:39 -0400103 if (!rContext || !proxy) {
Greg Danielba0ff782020-01-07 15:42:57 -0500104 return nullptr;
105 }
106
107 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400108 GrSwizzle readSwizzle, writeSwizzle;
109 if (colorType != GrColorType::kUnknown) {
Robert Phillips779125d2021-05-24 13:14:39 -0400110 readSwizzle = rContext->priv().caps()->getReadSwizzle(format, colorType);
111 writeSwizzle = rContext->priv().caps()->getWriteSwizzle(format, colorType);
Brian Salomonc5243782020-04-02 12:50:34 -0400112 }
Greg Danielba0ff782020-01-07 15:42:57 -0500113
Brian Salomon07bc9a22020-12-02 13:37:16 -0500114 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400115 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500116
Robert Phillips779125d2021-05-24 13:14:39 -0400117 return std::make_unique<GrSurfaceDrawContext>(rContext,
Brian Salomoneebe7352020-12-09 16:37:04 -0500118 std::move(readView),
119 std::move(writeView),
120 colorType,
121 std::move(colorSpace),
122 surfaceProps,
123 flushTimeOpsTask);
Greg Danielba0ff782020-01-07 15:42:57 -0500124}
125
Brian Salomoneebe7352020-12-09 16:37:04 -0500126std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Danielba0ff782020-01-07 15:42:57 -0500127 GrRecordingContext* context,
Greg Danielba0ff782020-01-07 15:42:57 -0500128 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500129 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500130 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500131 const GrBackendFormat& format,
132 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400133 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500134 GrProtected isProtected,
Brian Salomon07bc9a22020-12-02 13:37:16 -0500135 GrSwizzle readSwizzle,
136 GrSwizzle writeSwizzle,
Greg Daniele20fcad2020-01-08 11:52:34 -0500137 GrSurfaceOrigin origin,
138 SkBudgeted budgeted,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600139 const SkSurfaceProps& surfaceProps) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500140 // It is probably not necessary to check if the context is abandoned here since uses of the
Brian Salomoneebe7352020-12-09 16:37:04 -0500141 // GrSurfaceDrawContext which need the context will mostly likely fail later on without an
Greg Daniele20fcad2020-01-08 11:52:34 -0500142 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
143 // an abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -0400144 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500145 return nullptr;
146 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500147
Greg Daniele20fcad2020-01-08 11:52:34 -0500148 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomon07bc9a22020-12-02 13:37:16 -0500149 format,
150 dimensions,
151 GrRenderable::kYes,
152 sampleCnt,
153 mipMapped,
154 fit,
155 budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500156 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500157 if (!proxy) {
158 return nullptr;
159 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500160
Brian Salomon07bc9a22020-12-02 13:37:16 -0500161 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
162 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
163
Brian Salomoneebe7352020-12-09 16:37:04 -0500164 auto rtc = std::make_unique<GrSurfaceDrawContext>(context,
165 std::move(readView),
166 std::move(writeView),
167 GrColorType::kUnknown,
168 std::move(colorSpace),
169 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500170 rtc->discard();
171 return rtc;
172}
173
Brian Salomoneebe7352020-12-09 16:37:04 -0500174std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Robert Phillips779125d2021-05-24 13:14:39 -0400175 GrRecordingContext* rContext,
Greg Daniele20fcad2020-01-08 11:52:34 -0500176 GrColorType colorType,
177 sk_sp<SkColorSpace> colorSpace,
178 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500179 SkISize dimensions,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600180 const SkSurfaceProps& surfaceProps,
Greg Daniele20fcad2020-01-08 11:52:34 -0500181 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400182 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500183 GrProtected isProtected,
184 GrSurfaceOrigin origin,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600185 SkBudgeted budgeted) {
Robert Phillips779125d2021-05-24 13:14:39 -0400186 auto format = rContext->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
Greg Daniele20fcad2020-01-08 11:52:34 -0500187 if (!format.isValid()) {
188 return nullptr;
189 }
Robert Phillips779125d2021-05-24 13:14:39 -0400190 sk_sp<GrTextureProxy> proxy = rContext->priv().proxyProvider()->createProxy(format,
191 dimensions,
192 GrRenderable::kYes,
193 sampleCnt,
194 mipMapped,
195 fit,
196 budgeted,
197 isProtected);
Brian Salomon07bc9a22020-12-02 13:37:16 -0500198 if (!proxy) {
199 return nullptr;
200 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500201
Robert Phillips779125d2021-05-24 13:14:39 -0400202 return GrSurfaceDrawContext::Make(rContext,
Brian Salomoneebe7352020-12-09 16:37:04 -0500203 colorType,
Brian Salomoneebe7352020-12-09 16:37:04 -0500204 std::move(proxy),
Robert Phillips779125d2021-05-24 13:14:39 -0400205 std::move(colorSpace),
Brian Salomoneebe7352020-12-09 16:37:04 -0500206 origin,
207 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500208}
209
Brian Salomoneebe7352020-12-09 16:37:04 -0500210std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeWithFallback(
Robert Phillips87b57fa2021-06-08 13:10:04 -0400211 GrRecordingContext* rContext,
Greg Daniele20fcad2020-01-08 11:52:34 -0500212 GrColorType colorType,
213 sk_sp<SkColorSpace> colorSpace,
214 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500215 SkISize dimensions,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600216 const SkSurfaceProps& surfaceProps,
Greg Daniele20fcad2020-01-08 11:52:34 -0500217 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400218 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500219 GrProtected isProtected,
220 GrSurfaceOrigin origin,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600221 SkBudgeted budgeted) {
Robert Phillips87b57fa2021-06-08 13:10:04 -0400222 const GrCaps* caps = rContext->priv().caps();
223 auto [ct, format] = caps->getFallbackColorTypeAndFormat(colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400224 if (ct == GrColorType::kUnknown) {
225 return nullptr;
226 }
Robert Phillips87b57fa2021-06-08 13:10:04 -0400227 return GrSurfaceDrawContext::Make(rContext, ct, colorSpace, fit, dimensions, surfaceProps,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600228 sampleCnt, mipMapped, isProtected, origin, budgeted);
Greg Daniele20fcad2020-01-08 11:52:34 -0500229}
230
Brian Salomoneebe7352020-12-09 16:37:04 -0500231std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendTexture(
Greg Danielba0ff782020-01-07 15:42:57 -0500232 GrRecordingContext* context,
233 GrColorType colorType,
234 sk_sp<SkColorSpace> colorSpace,
235 const GrBackendTexture& tex,
236 int sampleCnt,
237 GrSurfaceOrigin origin,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600238 const SkSurfaceProps& surfaceProps,
Robert Phillipsa1121332020-06-29 13:05:29 -0400239 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500240 SkASSERT(sampleCnt > 0);
241 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400242 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
243 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500244 if (!proxy) {
245 return nullptr;
246 }
247
Robert Phillips779125d2021-05-24 13:14:39 -0400248 return GrSurfaceDrawContext::Make(context, colorType, std::move(proxy), std::move(colorSpace),
Brian Salomoneebe7352020-12-09 16:37:04 -0500249 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500250}
251
Greg Danielf41b2bd2019-08-22 16:19:24 -0400252// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
John Stiles0fbc6a32021-06-04 14:40:57 -0400253// GrOpsTask to be picked up and added to by SurfaceDrawContexts lower in the call
Greg Danielf41b2bd2019-08-22 16:19:24 -0400254// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500255// when the surfaceDrawContext attempts to use it (via getOpsTask).
Brian Salomoneebe7352020-12-09 16:37:04 -0500256GrSurfaceDrawContext::GrSurfaceDrawContext(GrRecordingContext* context,
257 GrSurfaceProxyView readView,
258 GrSurfaceProxyView writeView,
259 GrColorType colorType,
260 sk_sp<SkColorSpace> colorSpace,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600261 const SkSurfaceProps& surfaceProps,
Brian Salomoneebe7352020-12-09 16:37:04 -0500262 bool flushTimeOpsTask)
Brian Salomon590f5672020-12-16 11:44:47 -0500263 : GrSurfaceFillContext(context,
264 std::move(readView),
265 std::move(writeView),
266 {colorType, kPremul_SkAlphaType, std::move(colorSpace)},
267 flushTimeOpsTask)
Chris Daltonf5b87f92021-04-19 17:27:09 -0600268 , fSurfaceProps(surfaceProps)
Chris Dalton710e1c92021-04-23 13:07:52 -0600269 , fCanUseDynamicMSAA(
270 (fSurfaceProps.flags() & kDMSAA_SkSurfacePropsPrivateFlag) &&
271 context->priv().caps()->supportsDynamicMSAA(this->asRenderTargetProxy()))
Herb Derbyd29207a2020-06-08 13:50:19 -0400272 , fGlyphPainter(*this) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700273 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700274}
275
Brian Salomoneebe7352020-12-09 16:37:04 -0500276GrSurfaceDrawContext::~GrSurfaceDrawContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800277 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700278}
279
Brian Salomon9a0da782021-04-23 13:41:55 -0400280void GrSurfaceDrawContext::willReplaceOpsTask(GrOpsTask* prevTask, GrOpsTask* nextTask) {
281 if (prevTask && fNeedsStencil) {
282 // Store the stencil values in memory upon completion of fOpsTask.
283 prevTask->setMustPreserveStencil();
284 // Reload the stencil buffer content at the beginning of newOpsTask.
285 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
286 // values?
287 nextTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
288 }
Chris Dalton5dfb3f42021-04-30 17:16:12 -0600289#if GR_GPU_STATS && GR_TEST_UTILS
290 if (fCanUseDynamicMSAA) {
291 fContext->priv().dmsaaStats().fNumRenderPasses++;
292 }
293#endif
Brian Salomon9a0da782021-04-23 13:41:55 -0400294}
295
Brian Salomoneebe7352020-12-09 16:37:04 -0500296inline GrAAType GrSurfaceDrawContext::chooseAAType(GrAA aa) {
Chris Dalton710e1c92021-04-23 13:07:52 -0600297 if (fCanUseDynamicMSAA) {
298 // Trigger dmsaa by default if the render target has it. Coverage ops that know how to
299 // handle both single and multisample targets without popping will do so without calling
300 // chooseAAType.
301 return GrAAType::kMSAA;
302 }
Chris Dalton7d6748e2019-03-13 00:34:52 -0600303 if (GrAA::kNo == aa) {
304 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
305 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600306 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600307 return GrAAType::kMSAA;
308 }
309 return GrAAType::kNone;
310 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600311 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600312}
313
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500314void GrSurfaceDrawContext::drawGlyphRunListNoCache(const GrClip* clip,
315 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400316 const SkGlyphRunList& glyphRunList,
317 const SkPaint& paint) {
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500318 GrSDFTControl control =
319 fContext->priv().getSDFTControl(fSurfaceProps.isUseDeviceIndependentFonts());
320 const SkPoint drawOrigin = glyphRunList.origin();
321 SkMatrix drawMatrix = viewMatrix.localToDevice();
322 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbyd5a67642021-04-16 09:07:24 -0400323 GrSubRunAllocator* const alloc = this->subRunAlloc();
robertphillips2d70dcb2015-10-06 07:38:23 -0700324
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500325 for (auto& glyphRun : glyphRunList) {
Herb Derby0da2c142021-03-22 15:28:23 -0400326 GrSubRunNoCachePainter painter{this, alloc, clip, viewMatrix, glyphRunList, paint};
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500327
328 // Make and add the text ops.
329 fGlyphPainter.processGlyphRun(glyphRun,
330 drawMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400331 paint,
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500332 control,
333 &painter);
Greg Danielbe7fc462019-01-03 16:40:42 -0500334 }
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500335}
Greg Danielbe7fc462019-01-03 16:40:42 -0500336
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500337void GrSurfaceDrawContext::drawGlyphRunListWithCache(const GrClip* clip,
338 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400339 const SkGlyphRunList& glyphRunList,
340 const SkPaint& paint) {
Herb Derby64477842021-03-08 13:47:06 -0500341 SkMatrix drawMatrix(viewMatrix.localToDevice());
342 drawMatrix.preTranslate(glyphRunList.origin().x(), glyphRunList.origin().y());
343
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500344 GrSDFTControl control =
345 this->recordingContext()->priv().getSDFTControl(
Herb Derby9ad09822021-03-01 14:19:36 -0500346 this->surfaceProps().isUseDeviceIndependentFonts());
347
Herb Derby64477842021-03-08 13:47:06 -0500348 auto [canCache, key] = GrTextBlob::Key::Make(glyphRunList,
Herb Derby0da2c142021-03-22 15:28:23 -0400349 paint,
Herb Derby64477842021-03-08 13:47:06 -0500350 fSurfaceProps,
351 this->colorInfo(),
352 drawMatrix,
353 control);
Herb Derby64688222020-06-08 14:19:57 -0400354
355 sk_sp<GrTextBlob> blob;
Herb Derby64477842021-03-08 13:47:06 -0500356 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
Herb Derby64688222020-06-08 14:19:57 -0400357 if (canCache) {
Herb Derby64688222020-06-08 14:19:57 -0400358 blob = textBlobCache->find(key);
359 }
360
Herb Derby0da2c142021-03-22 15:28:23 -0400361 if (blob == nullptr || !blob->canReuse(paint, drawMatrix)) {
Herb Derby64688222020-06-08 14:19:57 -0400362 if (blob != nullptr) {
363 // We have to remake the blob because changes may invalidate our masks.
364 // TODO we could probably get away with reuse most of the time if the pointer is unique,
365 // but we'd have to clear the SubRun information
366 textBlobCache->remove(blob.get());
367 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400368
Herb Derby0da2c142021-03-22 15:28:23 -0400369 blob = GrTextBlob::Make(glyphRunList, paint, drawMatrix, control, &fGlyphPainter);
Herb Derby14992492021-02-25 16:28:03 -0500370
371 if (canCache) {
372 blob->addKey(key);
373 // The blob may already have been created on a different thread. Use the first one
374 // that was there.
375 blob = textBlobCache->addOrReturnExisting(glyphRunList, blob);
376 }
Herb Derby64688222020-06-08 14:19:57 -0400377 }
378
Herb Derby6b27ae82021-02-23 13:03:59 -0500379 for (const GrSubRun& subRun : blob->subRunList()) {
Herb Derby0da2c142021-03-22 15:28:23 -0400380 subRun.draw(clip, viewMatrix, glyphRunList, paint, this);
Herb Derbye5b768b2020-07-07 15:56:07 -0400381 }
robertphillipsea461502015-05-26 11:38:03 -0700382}
383
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500384// choose to use the GrTextBlob cache or not.
385bool gGrDrawTextNoCache = false;
386void GrSurfaceDrawContext::drawGlyphRunList(const GrClip* clip,
387 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400388 const SkGlyphRunList& glyphRunList,
389 const SkPaint& paint) {
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500390 ASSERT_SINGLE_OWNER
391 RETURN_IF_ABANDONED
392 SkDEBUGCODE(this->validate();)
393 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawGlyphRunList", fContext);
394
395 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
396 // secondary command buffers because it would require stopping and starting a render pass which
397 // we don't have access to.
398 if (this->wrapsVkSecondaryCB()) {
399 return;
400 }
401
Herb Derby40f45d62021-04-22 13:43:06 -0400402 if (gGrDrawTextNoCache || glyphRunList.blob() == nullptr) {
403 // If the glyphRunList does not have an associated text blob, then it was created by one of
404 // the direct draw APIs (drawGlyphs, etc.). There is no need to create a GrTextBlob just
405 // build the sub run directly and place it in the op.
Herb Derby0da2c142021-03-22 15:28:23 -0400406 this->drawGlyphRunListNoCache(clip, viewMatrix, glyphRunList, paint);
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500407 } else {
Herb Derby0da2c142021-03-22 15:28:23 -0400408 this->drawGlyphRunListWithCache(clip, viewMatrix, glyphRunList, paint);
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500409 }
410}
411
Brian Salomoneebe7352020-12-09 16:37:04 -0500412void GrSurfaceDrawContext::drawPaint(const GrClip* clip,
413 GrPaint&& paint,
414 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000415 // Start with the render target, since that is the maximum content we could possibly fill.
416 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400417 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000418 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
419 // inverse view matrix.
Chris Dalton35619552021-03-10 19:20:43 -0700420 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig61328202019-06-19 14:48:58 +0000421 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
422 } else {
423 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
424 SkMatrix localMatrix;
425 if (!viewMatrix.invert(&localMatrix)) {
426 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400427 }
Chris Dalton35619552021-03-10 19:20:43 -0700428 SkIRect bounds = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
429 this->fillPixelsWithLocalMatrix(clip, std::move(paint), bounds, localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700430 }
robertphillipsea461502015-05-26 11:38:03 -0700431}
432
Brian Salomoneebe7352020-12-09 16:37:04 -0500433enum class GrSurfaceDrawContext::QuadOptimization {
Michael Ludwig61328202019-06-19 14:48:58 +0000434 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
435 kDiscarded,
436 // The rect to draw was converted to some other op and appended to the oplist, so no additional
437 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
438 // a constColor is provided.
439 kSubmitted,
440 // The clip was folded into the device quad, with updated edge flags and local coords, and
441 // caller is responsible for adding an appropriate op.
442 kClipApplied,
443 // No change to clip, but quad updated to better fit clip/render target, and caller is
444 // responsible for adding an appropriate op.
445 kCropped
446};
Michael Ludwig61a16512019-01-15 11:15:13 -0500447
Brian Salomoneebe7352020-12-09 16:37:04 -0500448GrSurfaceDrawContext::QuadOptimization GrSurfaceDrawContext::attemptQuadOptimization(
Chris Dalton61d694d2021-03-22 00:00:52 -0600449 const GrClip* clip, const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad,
450 GrPaint* paint) {
Michael Ludwig61328202019-06-19 14:48:58 +0000451 // Optimization requirements:
452 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500453 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
454 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
455 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
456 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
457 // 5. kCropped in all other scenarios (although a crop may be a no-op)
Chris Dalton61d694d2021-03-22 00:00:52 -0600458 const SkPMColor4f* constColor = nullptr;
459 SkPMColor4f paintColor;
460 if (!stencilSettings && paint && !paint->hasCoverageFragmentProcessor() &&
461 paint->isConstantBlendedColor(&paintColor)) {
462 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
463 constColor = &paintColor;
464 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500465
466 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
467 // better to just keep the old flags instead of introducing mixed edge flags.
468 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000469
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400470 // Use the logical size of the render target, which allows for "fullscreen" clears even if
471 // the render target has an approximate backing fit
472 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400473
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500474 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400475 if (!quad->fDevice.isFinite() || drawBounds.isEmpty() ||
476 GrClip::IsOutsideClip(rtRect, drawBounds)) {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000477 return QuadOptimization::kDiscarded;
478 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400479 auto conservativeCrop = [&]() {
480 static constexpr int kLargeDrawLimit = 15000;
481 // Crop the quad to the render target. This doesn't change the visual results of drawing but
482 // is meant to help numerical stability for excessively large draws.
483 if (drawBounds.width() > kLargeDrawLimit || drawBounds.height() > kLargeDrawLimit) {
484 GrQuadUtils::CropToRect(rtRect, *aa, quad, /* compute local */ !constColor);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000485 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400486 };
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000487
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400488 bool simpleColor = !stencilSettings && constColor;
Michael Ludwig6397e802020-08-05 15:50:01 -0400489 GrClip::PreClipResult result = clip ? clip->preApply(drawBounds, *aa)
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400490 : GrClip::PreClipResult(GrClip::Effect::kUnclipped);
491 switch(result.fEffect) {
492 case GrClip::Effect::kClippedOut:
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000493 return QuadOptimization::kDiscarded;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400494 case GrClip::Effect::kUnclipped:
495 if (!simpleColor) {
496 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000497 return QuadOptimization::kClipApplied;
498 } else {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400499 // Update result to store the render target bounds in order and then fall
500 // through to attempt the draw->native clear optimization
501 result = GrClip::PreClipResult(SkRRect::MakeRect(rtRect), *aa);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000502 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400503 break;
504 case GrClip::Effect::kClipped:
505 if (!result.fIsRRect || (stencilSettings && result.fAA != *aa) ||
506 (!result.fRRect.isRect() && !simpleColor)) {
507 // The clip and draw state are too complicated to try and reduce
508 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000509 return QuadOptimization::kCropped;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400510 } // Else fall through to attempt to combine the draw and clip geometry together
511 break;
512 default:
513 SkUNREACHABLE;
514 }
515
516 // If we reached here, we know we're an axis-aligned clip that is either a rect or a round rect,
517 // so we can potentially combine it with the draw geometry so that no clipping is needed.
518 SkASSERT(result.fEffect == GrClip::Effect::kClipped && result.fIsRRect);
519 SkRect clippedBounds = result.fRRect.getBounds();
520 clippedBounds.intersect(rtRect);
521 if (result.fRRect.isRect()) {
522 // No rounded corners, so we might be able to become a native clear or we might be able to
523 // modify geometry and edge flags to represent intersected shape of clip and draw.
524 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
525 /*compute local*/ !constColor)) {
526 if (simpleColor && quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
527 // Clear optimization is possible
528 drawBounds = quad->fDevice.bounds();
529 if (drawBounds.contains(rtRect)) {
530 // Fullscreen clear
531 this->clear(*constColor);
532 return QuadOptimization::kSubmitted;
533 } else if (GrClip::IsPixelAligned(drawBounds) &&
534 drawBounds.width() > 256 && drawBounds.height() > 256) {
535 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
536 SkIRect scissorRect;
537 drawBounds.round(&scissorRect);
538 this->clear(scissorRect, *constColor);
539 return QuadOptimization::kSubmitted;
540 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000541 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400542
543 // else the draw and clip were combined so just update the AA to reflect combination
544 if (*aa == GrAA::kNo && result.fAA == GrAA::kYes &&
545 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
546 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
547 // properly reflect the smooth edge of the clip.
548 *aa = GrAA::kYes;
549 }
550 // We intentionally do not downgrade AA here because we don't know if we need to
551 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
552 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
553 // deviceQuad is exactly the intersection of original quad and clip, so it can be
554 // drawn with no clip (submitted by caller)
555 return QuadOptimization::kClipApplied;
556 }
557 } else {
558 // Rounded corners and constant filled color (limit ourselves to solid colors because
559 // there is no way to use custom local coordinates with drawRRect).
560 SkASSERT(simpleColor);
561 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
562 /* compute local */ false) &&
563 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
564 quad->fDevice.bounds().contains(clippedBounds)) {
565 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
566 // we can draw the rrect directly and ignore the edge flags
Chris Dalton61d694d2021-03-22 00:00:52 -0600567 this->drawRRect(nullptr, std::move(*paint), result.fAA, SkMatrix::I(), result.fRRect,
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400568 GrStyle::SimpleFill());
569 return QuadOptimization::kSubmitted;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000570 }
571 }
572
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400573 // The quads have been updated to better fit the clip bounds, but can't get rid of
574 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500575 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000576 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000577}
578
Brian Salomoneebe7352020-12-09 16:37:04 -0500579void GrSurfaceDrawContext::drawFilledQuad(const GrClip* clip,
580 GrPaint&& paint,
581 GrAA aa,
582 DrawQuad* quad,
583 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000584 ASSERT_SINGLE_OWNER
585 RETURN_IF_ABANDONED
586 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500587 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500588
Michael Ludwig61328202019-06-19 14:48:58 +0000589 AutoCheckFlush acf(this->drawingManager());
590
Chris Dalton61d694d2021-03-22 00:00:52 -0600591 QuadOptimization opt = this->attemptQuadOptimization(clip, ss, &aa, quad, &paint);
Michael Ludwig61328202019-06-19 14:48:58 +0000592 if (opt >= QuadOptimization::kClipApplied) {
593 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400594 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Chris Dalton710e1c92021-04-23 13:07:52 -0600595 GrAAType aaType;
596 if (ss) {
597 aaType = (aa == GrAA::kYes) ? GrAAType::kMSAA : GrAAType::kNone;
598 } else if (fCanUseDynamicMSAA && aa == GrAA::kNo) {
599 // The SkGpuDevice ensures GrAA is always kYes when using dmsaa. If the caller calls
600 // into here with GrAA::kNo, trust that they know what they're doing and that the
601 // rendering will be equal with or without msaa.
602 aaType = GrAAType::kNone;
603 } else {
604 aaType = this->chooseAAType(aa);
605 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500606 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
607 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700608 }
Michael Ludwig61328202019-06-19 14:48:58 +0000609 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800610}
611
Chris Dalton4718a8b2021-03-18 18:38:52 -0600612void GrSurfaceDrawContext::drawTexture(const GrClip* clip,
613 GrSurfaceProxyView view,
614 SkAlphaType srcAlphaType,
615 GrSamplerState::Filter filter,
616 GrSamplerState::MipmapMode mm,
617 SkBlendMode blendMode,
618 const SkPMColor4f& color,
619 const SkRect& srcRect,
620 const SkRect& dstRect,
621 GrAA aa,
622 GrQuadAAFlags edgeAA,
623 SkCanvas::SrcRectConstraint constraint,
624 const SkMatrix& viewMatrix,
625 sk_sp<GrColorSpaceXform> colorSpaceXform) {
626 // If we are using dmsaa then go through GrFillRRectOp (via fillRectToRect).
Chris Dalton94df5722021-04-19 17:40:40 -0600627 if ((this->alwaysAntialias() || this->caps()->reducedShaderMode()) && aa == GrAA::kYes) {
Chris Dalton4718a8b2021-03-18 18:38:52 -0600628 GrPaint paint;
629 paint.setColor4f(color);
630 std::unique_ptr<GrFragmentProcessor> fp;
631 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
632 fp = GrTextureEffect::MakeSubset(view, srcAlphaType, SkMatrix::I(),
633 GrSamplerState(filter, mm), srcRect,
634 *this->caps());
635 } else {
636 fp = GrTextureEffect::Make(view, srcAlphaType, SkMatrix::I(), filter, mm);
637 }
638 if (colorSpaceXform) {
639 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
640 }
641 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
642 paint.setColorFragmentProcessor(std::move(fp));
643 if (blendMode != SkBlendMode::kSrcOver) {
644 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
645 }
646 this->fillRectToRect(clip, std::move(paint), GrAA::kYes, viewMatrix, dstRect, srcRect);
647 return;
648 }
649
650 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint ?
651 &srcRect : nullptr;
652 DrawQuad quad{GrQuad::MakeFromRect(dstRect, viewMatrix), GrQuad(srcRect), edgeAA};
653
654 this->drawTexturedQuad(clip, std::move(view), srcAlphaType, std::move(colorSpaceXform), filter,
655 mm, color, blendMode, aa, &quad, subset);
656}
657
Brian Salomoneebe7352020-12-09 16:37:04 -0500658void GrSurfaceDrawContext::drawTexturedQuad(const GrClip* clip,
659 GrSurfaceProxyView proxyView,
660 SkAlphaType srcAlphaType,
661 sk_sp<GrColorSpaceXform> textureXform,
662 GrSamplerState::Filter filter,
663 GrSamplerState::MipmapMode mm,
664 const SkPMColor4f& color,
665 SkBlendMode blendMode,
666 GrAA aa,
667 DrawQuad* quad,
668 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000669 ASSERT_SINGLE_OWNER
670 RETURN_IF_ABANDONED
671 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400672 SkASSERT(proxyView.asTextureProxy());
Brian Salomoneebe7352020-12-09 16:37:04 -0500673 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTexturedQuad", fContext);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000674
675 AutoCheckFlush acf(this->drawingManager());
676
677 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
678 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Chris Dalton61d694d2021-03-22 00:00:52 -0600679 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr/*stencil*/, &aa, quad,
680 nullptr/*paint*/);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000681
682 SkASSERT(opt != QuadOptimization::kSubmitted);
683 if (opt != QuadOptimization::kDiscarded) {
684 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400685 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000686 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400687 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400688 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
689 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400690 // Use the provided subset, although hypothetically we could detect that the cropped local
691 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500692 this->addDrawOp(finalClip,
693 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400694 std::move(textureXform), filter, mm, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400695 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000696 }
697}
698
Brian Salomoneebe7352020-12-09 16:37:04 -0500699void GrSurfaceDrawContext::drawRect(const GrClip* clip,
700 GrPaint&& paint,
701 GrAA aa,
702 const SkMatrix& viewMatrix,
703 const SkRect& rect,
704 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700705 if (!style) {
706 style = &GrStyle::SimpleFill();
707 }
joshualitt1de610a2016-01-06 08:26:09 -0800708 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700709 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700710 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500711 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700712
bsalomon6663acf2016-05-10 09:14:17 -0700713 // Path effects should've been devolved to a path in SkGpuDevice
714 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700715
Robert Phillips72152832017-01-25 17:31:35 -0500716 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700717
bsalomon6663acf2016-05-10 09:14:17 -0700718 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400719 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000720 // Fills the rect, using rect as its own local coordinates
721 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500722 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400723 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
724 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
725 (rect.width() && rect.height())) {
726 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
727 // GrStyledShape to handle stroke caps and dashing properly.
Chris Dalton710e1c92021-04-23 13:07:52 -0600728 GrAAType aaType = (fCanUseDynamicMSAA) ? GrAAType::kCoverage : this->chooseAAType(aa);
Herb Derbyc76d4092020-10-07 16:46:15 -0400729 GrOp::Owner op = GrStrokeRectOp::Make(
730 fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500731 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
732 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500733 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400734 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700735 return;
robertphillips4bc31812016-03-01 12:22:49 -0800736 }
robertphillips4bc31812016-03-01 12:22:49 -0800737 }
Mike Klein16885072018-12-11 09:54:31 -0500738 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000739 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -0700740 GrStyledShape(rect, *style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -0700741}
742
Chris Dalton61d694d2021-03-22 00:00:52 -0600743void GrSurfaceDrawContext::fillRectToRect(const GrClip* clip,
744 GrPaint&& paint,
745 GrAA aa,
746 const SkMatrix& viewMatrix,
747 const SkRect& rectToDraw,
748 const SkRect& localRect) {
749 DrawQuad quad{GrQuad::MakeFromRect(rectToDraw, viewMatrix), GrQuad(localRect),
750 aa == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone};
751
752 // If we are using dmsaa then attempt to draw the rect with GrFillRRectOp.
Chris Dalton94df5722021-04-19 17:40:40 -0600753 if ((fContext->priv().caps()->reducedShaderMode() || this->alwaysAntialias()) &&
754 this->caps()->drawInstancedSupport() &&
Chris Dalton61d694d2021-03-22 00:00:52 -0600755 aa == GrAA::kYes) { // If aa is kNo when using dmsaa, the rect is axis aligned. Don't use
756 // GrFillRRectOp because it might require dual source blending.
757 // http://skbug.com/11756
758 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr/*stencil*/, &aa, &quad,
759 &paint);
760 if (opt < QuadOptimization::kClipApplied) {
761 // The optimization was completely handled inside attempt().
762 return;
763 }
764
765 SkRect croppedRect, croppedLocal{};
766 const GrClip* optimizedClip = clip;
767 if (clip && viewMatrix.isScaleTranslate() && quad.fDevice.asRect(&croppedRect) &&
768 (!paint.usesVaryingCoords() || quad.fLocal.asRect(&croppedLocal))) {
769 // The cropped quad is still a rect, and our view matrix preserves rects. Map it back
770 // to pre-matrix space.
771 SkMatrix inverse;
772 if (!viewMatrix.invert(&inverse)) {
773 return;
774 }
775 SkASSERT(inverse.rectStaysRect());
776 inverse.mapRect(&croppedRect);
777 if (opt == QuadOptimization::kClipApplied) {
778 optimizedClip = nullptr;
779 }
780 } else {
781 // Even if attemptQuadOptimization gave us an optimized quad, GrFillRRectOp needs a rect
782 // in pre-matrix space, so use the original rect. Also preserve the original clip.
783 croppedRect = rectToDraw;
784 croppedLocal = localRect;
785 }
786
787 if (auto op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix,
788 SkRRect::MakeRect(croppedRect), croppedLocal,
789 GrAA::kYes)) {
790 this->addDrawOp(optimizedClip, std::move(op));
791 return;
792 }
793 }
794
795 assert_alive(paint);
796 this->drawFilledQuad(clip, std::move(paint), aa, &quad);
797}
798
Brian Salomoneebe7352020-12-09 16:37:04 -0500799void GrSurfaceDrawContext::drawQuadSet(const GrClip* clip,
800 GrPaint&& paint,
801 GrAA aa,
802 const SkMatrix& viewMatrix,
803 const QuadSetEntry quads[],
804 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600805 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500806
807 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
808 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500809}
810
Brian Salomoneebe7352020-12-09 16:37:04 -0500811int GrSurfaceDrawContext::maxWindowRectangles() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500812 return this->asRenderTargetProxy()->maxWindowRectangles(*this->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500813}
814
Brian Salomoneebe7352020-12-09 16:37:04 -0500815GrOpsTask::CanDiscardPreviousOps GrSurfaceDrawContext::canDiscardPreviousOpsOnFullClear() const {
Chris Dalton6b982802019-06-27 13:53:46 -0600816#if GR_TEST_UTILS
817 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400818 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600819 }
820#endif
821 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
822 // would normally be overwritten. The one exception is if the render target context is marked as
823 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
824 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
825 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
826 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Chris Dalton57ab06c2021-04-22 12:57:28 -0600827 return GrOpsTask::CanDiscardPreviousOps(!fNeedsStencil);
Chris Dalton6b982802019-06-27 13:53:46 -0600828}
829
Chris Dalton57ab06c2021-04-22 12:57:28 -0600830void GrSurfaceDrawContext::setNeedsStencil() {
831 // Don't clear stencil until after we've set fNeedsStencil. This ensures we don't loop forever
832 // in the event that there are driver bugs and we need to clear as a draw.
833 bool hasInitializedStencil = fNeedsStencil;
834 fNeedsStencil = true;
Chris Dalton674f77a2019-09-30 20:49:39 -0600835 if (!hasInitializedStencil) {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600836 this->asRenderTargetProxy()->setNeedsStencil();
Chris Dalton6b982802019-06-27 13:53:46 -0600837 if (this->caps()->performStencilClearsAsDraws()) {
838 // There is a driver bug with clearing stencil. We must use an op to manually clear the
839 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400840 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600841 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600842 this->getOpsTask()->setInitialStencilContent(
843 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600844 }
845 }
846}
847
Brian Salomoneebe7352020-12-09 16:37:04 -0500848void GrSurfaceDrawContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600849 this->setNeedsStencil();
Chris Dalton858cf232019-10-14 16:20:00 -0600850
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400851 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
852 if (scissor && !scissorState.set(*scissor)) {
853 // The requested clear region is off screen, so nothing to do.
854 return;
855 }
856
Michael Ludwig1e632792020-05-21 12:45:31 -0400857 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400858 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400859 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500860 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500861
862 // Configure the paint to have no impact on the color buffer
863 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400864 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400865 this->addDrawOp(nullptr,
866 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
867 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500868 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400869 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500870 }
robertphillips976f5f02016-06-03 10:59:20 -0700871}
872
Chris Dalton7d592cd2021-03-11 22:49:33 -0700873bool GrSurfaceDrawContext::stencilPath(const GrHardClip* clip,
874 GrAA doStencilMSAA,
875 const SkMatrix& viewMatrix,
876 const SkPath& path) {
877 SkIRect clipBounds = clip ? clip->getConservativeBounds()
878 : SkIRect::MakeSize(this->dimensions());
879 GrStyledShape shape(path, GrStyledShape::DoSimplify::kNo);
880
881 GrPathRenderer::CanDrawPathArgs canDrawArgs;
882 canDrawArgs.fCaps = fContext->priv().caps();
883 canDrawArgs.fProxy = this->asRenderTargetProxy();
884 canDrawArgs.fClipConservativeBounds = &clipBounds;
885 canDrawArgs.fViewMatrix = &viewMatrix;
886 canDrawArgs.fShape = &shape;
887 canDrawArgs.fPaint = nullptr;
Chris Dalton8e3bca62021-04-26 15:49:21 -0600888 canDrawArgs.fSurfaceProps = &fSurfaceProps;
Chris Dalton7d592cd2021-03-11 22:49:33 -0700889 canDrawArgs.fAAType = (doStencilMSAA == GrAA::kYes) ? GrAAType::kMSAA : GrAAType::kNone;
890 canDrawArgs.fHasUserStencilSettings = false;
Chris Dalton7d592cd2021-03-11 22:49:33 -0700891 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
892 canDrawArgs, false, GrPathRendererChain::DrawType::kStencil);
893 if (!pr) {
894 SkDebugf("WARNING: No path renderer to stencil path.\n");
895 return false;
896 }
897
898 GrPathRenderer::StencilPathArgs args;
899 args.fContext = fContext;
John Stiles0fbc6a32021-06-04 14:40:57 -0400900 args.fSurfaceDrawContext = this;
Chris Dalton7d592cd2021-03-11 22:49:33 -0700901 args.fClip = clip;
902 args.fClipConservativeBounds = &clipBounds;
903 args.fViewMatrix = &viewMatrix;
904 args.fShape = &shape;
905 args.fDoStencilMSAA = doStencilMSAA;
906 pr->stencilPath(args);
907 return true;
908}
909
Brian Salomoneebe7352020-12-09 16:37:04 -0500910void GrSurfaceDrawContext::drawTextureSet(const GrClip* clip,
911 TextureSetEntry set[],
912 int cnt,
913 int proxyRunCnt,
914 GrSamplerState::Filter filter,
915 GrSamplerState::MipmapMode mm,
916 SkBlendMode mode,
917 GrAA aa,
918 SkCanvas::SrcRectConstraint constraint,
919 const SkMatrix& viewMatrix,
920 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400921 ASSERT_SINGLE_OWNER
922 RETURN_IF_ABANDONED
923 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500924 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500925
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500926 // Create the minimum number of GrTextureOps needed to draw this set. Individual
927 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
928 AutoCheckFlush acf(this->drawingManager());
929 GrAAType aaType = this->chooseAAType(aa);
930 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
931 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
932 : GrTextureOp::Saturate::kNo;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400933 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, mm, saturate,
Michael Ludwig379e4962019-12-06 13:21:26 -0500934 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400935}
936
Brian Salomoneebe7352020-12-09 16:37:04 -0500937void GrSurfaceDrawContext::drawVertices(const GrClip* clip,
938 GrPaint&& paint,
939 const SkMatrixProvider& matrixProvider,
940 sk_sp<SkVertices> vertices,
941 GrPrimitiveType* overridePrimType,
942 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -0500943 ASSERT_SINGLE_OWNER
944 RETURN_IF_ABANDONED
945 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500946 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500947
948 AutoCheckFlush acf(this->drawingManager());
949
950 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600951 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400952 GrOp::Owner op =
Brian Osman449b1152020-04-15 16:43:00 -0400953 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -0400954 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -0400955 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -0400956 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700957}
958
959///////////////////////////////////////////////////////////////////////////////
960
Brian Salomoneebe7352020-12-09 16:37:04 -0500961void GrSurfaceDrawContext::drawAtlas(const GrClip* clip,
962 GrPaint&& paint,
963 const SkMatrix& viewMatrix,
964 int spriteCount,
965 const SkRSXform xform[],
966 const SkRect texRect[],
967 const SkColor colors[]) {
Brian Osman4d92b892019-03-24 00:53:23 +0000968 ASSERT_SINGLE_OWNER
969 RETURN_IF_ABANDONED
970 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500971 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAtlas", fContext);
Brian Osman4d92b892019-03-24 00:53:23 +0000972
973 AutoCheckFlush acf(this->drawingManager());
974
975 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400976 GrOp::Owner op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
977 aaType, spriteCount, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +0000978 this->addDrawOp(clip, std::move(op));
979}
980
981///////////////////////////////////////////////////////////////////////////////
982
Brian Salomoneebe7352020-12-09 16:37:04 -0500983void GrSurfaceDrawContext::drawRRect(const GrClip* origClip,
984 GrPaint&& paint,
985 GrAA aa,
986 const SkMatrix& viewMatrix,
987 const SkRRect& rrect,
988 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800989 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700990 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700991 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500992 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400993
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400994 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
995
Robert Phillips85290802018-07-02 13:14:28 -0400996 const SkStrokeRec& stroke = style.strokeRec();
997 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700998 return;
999 }
1000
Michael Ludwig7c12e282020-05-29 09:54:07 -04001001 const GrClip* clip = origClip;
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001002 // It is not uncommon to clip to a round rect and then draw that same round rect. Since our
1003 // lower level clip code works from op bounds, which are SkRects, it doesn't detect that the
1004 // clip can be ignored. The following test attempts to mitigate the stencil clip cost but only
1005 // works for axis-aligned round rects. This also only works for filled rrects since the stroke
1006 // width outsets beyond the rrect itself.
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001007 // TODO: skbug.com/10462 - There was mixed performance wins and regressions when this
1008 // optimization was turned on outside of Android Framework. I (michaelludwig) believe this is
1009 // do to the overhead in determining if an SkClipStack is just a rrect. Once that is improved,
1010 // re-enable this and see if we avoid the regressions.
1011#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
bsalomon7f0d9f32016-08-15 14:49:10 -07001012 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001013 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001014 rrect.transform(viewMatrix, &devRRect)) {
Michael Ludwig6397e802020-08-05 15:50:01 -04001015 GrClip::PreClipResult result = clip->preApply(devRRect.getBounds(), aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001016 switch(result.fEffect) {
1017 case GrClip::Effect::kClippedOut:
1018 return;
1019 case GrClip::Effect::kUnclipped:
1020 clip = nullptr;
1021 break;
1022 case GrClip::Effect::kClipped:
1023 // Currently there's no general-purpose rrect-to-rrect contains function, and if we
1024 // got here, we know the devRRect's bounds aren't fully contained by the clip.
1025 // Testing for equality between the two is a reasonable stop-gap for now.
1026 if (result.fIsRRect && result.fRRect == devRRect) {
1027 // NOTE: On the android framework, we allow this optimization even when the clip
1028 // is non-AA and the draw is AA.
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001029 if (result.fAA == aa || (result.fAA == GrAA::kNo && aa == GrAA::kYes)) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001030 clip = nullptr;
1031 }
1032 }
1033 break;
1034 default:
1035 SkUNREACHABLE;
1036 }
1037 }
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001038#endif
ksakamotoec7f2ac2016-07-05 03:54:53 -07001039
Robert Phillips72152832017-01-25 17:31:35 -05001040 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001041
Chris Dalton7d6748e2019-03-13 00:34:52 -06001042 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon56892dc2021-04-12 12:59:11 -04001043 bool preferFillRRectOverCircle = aaType == GrAAType::kCoverage &&
1044 this->caps()->drawInstancedSupport() &&
1045 this->caps()->reducedShaderMode();
Chris Dalton133944a2018-11-16 23:30:29 -05001046
Herb Derbyc76d4092020-10-07 16:46:15 -04001047 GrOp::Owner op;
Brian Salomon56892dc2021-04-12 12:59:11 -04001048 if (!preferFillRRectOverCircle &&
1049 GrAAType::kCoverage == aaType &&
1050 rrect.isSimple() &&
Jim Van Verth64b85892019-06-17 12:01:46 -04001051 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1052 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1053 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1054 // to avoid perf regressions on some platforms.
1055 assert_alive(paint);
1056 op = GrOvalOpFactory::MakeCircularRRectOp(
1057 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1058 }
1059 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001060 assert_alive(paint);
Chris Dalton61d694d2021-03-22 00:00:52 -06001061 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, rrect.rect(),
Chris Dalton4f447342021-03-12 12:09:12 -07001062 GrAA(aaType != GrAAType::kNone));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001063 }
Chris Dalton710e1c92021-04-23 13:07:52 -06001064 if (!op && (GrAAType::kCoverage == aaType || fCanUseDynamicMSAA)) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001065 assert_alive(paint);
1066 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001067 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001068 }
1069 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001070 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001071 return;
robertphillipsea461502015-05-26 11:38:03 -07001072 }
robertphillipsb56f9272016-02-25 11:03:52 -08001073
Mike Klein16885072018-12-11 09:54:31 -05001074 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001075 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001076 GrStyledShape(rrect, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001077}
1078
Jim Van Verthc5903412016-11-17 15:27:09 -05001079///////////////////////////////////////////////////////////////////////////////
1080
Brian Salomoneebe7352020-12-09 16:37:04 -05001081bool GrSurfaceDrawContext::drawFastShadow(const GrClip* clip,
1082 const SkMatrix& viewMatrix,
1083 const SkPath& path,
1084 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001085 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001086 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001087 return true;
1088 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001089 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001090 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001091
1092 // check z plane
1093 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1094 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1095 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1096 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1097 return false;
1098 }
1099
1100 SkRRect rrect;
1101 SkRect rect;
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001102 // we can only handle rects, circles, and simple rrects with circular corners
1103 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsNearlySimpleCircular(rrect) &&
1104 rrect.getSimpleRadii().fX > SK_ScalarNearlyZero;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001105 if (!isRRect &&
1106 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1107 rect.width() > SK_ScalarNearlyZero) {
1108 rrect.setOval(rect);
1109 isRRect = true;
1110 }
1111 if (!isRRect && path.isRect(&rect)) {
1112 rrect.setRect(rect);
1113 isRRect = true;
1114 }
1115
1116 if (!isRRect) {
1117 return false;
1118 }
1119
Jim Van Verthc5903412016-11-17 15:27:09 -05001120 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001121 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001122 }
1123
Robert Phillips72152832017-01-25 17:31:35 -05001124 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001125
Jim Van Verth63f03542020-12-16 11:56:11 -05001126 SkPoint3 devLightPos = rec.fLightPos;
1127 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
1128 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -05001129 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -05001130 } else {
1131 // transform light
1132 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
1133 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001134
1135 // 1/scale
1136 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001137 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001138 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1139 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1140
1141 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001142 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1143
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001144 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001145 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1146 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1147 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001148
1149 // Outset the shadow rrect to the border of the penumbra
1150 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1151 SkRRect ambientRRect;
1152 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1153 // If the rrect was an oval then its outset will also be one.
1154 // We set it explicitly to avoid errors.
1155 if (rrect.isOval()) {
1156 ambientRRect = SkRRect::MakeOval(outsetRect);
1157 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001158 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001159 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1160 }
1161
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001162 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001163 if (transparent) {
1164 // set a large inset to force a fill
1165 devSpaceInsetWidth = ambientRRect.width();
1166 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001167
Herb Derbyc76d4092020-10-07 16:46:15 -04001168 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1169 ambientColor,
1170 viewMatrix,
1171 ambientRRect,
1172 devSpaceAmbientBlur,
1173 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001174 if (op) {
1175 this->addDrawOp(clip, std::move(op));
1176 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001177 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001178
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001179 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001180 SkScalar devSpaceSpotBlur;
1181 SkScalar spotScale;
1182 SkVector spotOffset;
Jim Van Verth63f03542020-12-16 11:56:11 -05001183 if (directional) {
1184 SkDrawShadowMetrics::GetDirectionalParams(occluderHeight, devLightPos.fX,
1185 devLightPos.fY, devLightPos.fZ,
1186 rec.fLightRadius, &devSpaceSpotBlur,
1187 &spotScale, &spotOffset);
1188 } else {
1189 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1190 devLightPos.fZ, rec.fLightRadius,
1191 &devSpaceSpotBlur, &spotScale, &spotOffset);
1192 }
Jim Van Verth1af03d42017-07-31 09:34:58 -04001193 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001194 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1195
Jim Van Verth3af1af92017-05-18 15:06:54 -04001196 // Adjust translate for the effect of the scale.
1197 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1198 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1199 // This offset is in dev space, need to transform it into source space.
1200 SkMatrix ctmInverse;
1201 if (viewMatrix.invert(&ctmInverse)) {
1202 ctmInverse.mapPoints(&spotOffset, 1);
1203 } else {
1204 // Since the matrix is a similarity, this should never happen, but just in case...
1205 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1206 SkASSERT(false);
1207 }
1208
1209 // Compute the transformed shadow rrect
1210 SkRRect spotShadowRRect;
1211 SkMatrix shadowTransform;
1212 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1213 rrect.transform(shadowTransform, &spotShadowRRect);
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001214 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001215
1216 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001217 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001218 SkScalar insetWidth = blurOutset;
1219 if (transparent) {
1220 // If transparent, just do a fill
1221 insetWidth += spotShadowRRect.width();
1222 } else {
1223 // For shadows, instead of using a stroke we specify an inset from the penumbra
1224 // border. We want to extend this inset area so that it meets up with the caster
1225 // geometry. The inset geometry will by default already be inset by the blur width.
1226 //
1227 // We compare the min and max corners inset by the radius between the original
1228 // rrect and the shadow rrect. The distance between the two plus the difference
1229 // between the scaled radius and the original radius gives the distance from the
1230 // transformed shadow shape to the original shape in that corner. The max
1231 // of these gives the maximum distance we need to cover.
1232 //
1233 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1234 // that to get the full insetWidth.
1235 SkScalar maxOffset;
1236 if (rrect.isRect()) {
1237 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001238 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001239 rrect.rect().fLeft),
1240 SkTAbs(spotShadowRRect.rect().fTop -
1241 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001242 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001243 rrect.rect().fRight),
1244 SkTAbs(spotShadowRRect.rect().fBottom -
1245 rrect.rect().fBottom)));
1246 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001247 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1249 rrect.rect().fLeft + dr,
1250 spotShadowRRect.rect().fTop -
1251 rrect.rect().fTop + dr);
1252 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1253 rrect.rect().fRight - dr,
1254 spotShadowRRect.rect().fBottom -
1255 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001256 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001257 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001258 }
Brian Osman788b9162020-02-07 10:36:46 -05001259 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001260 }
1261
1262 // Outset the shadow rrect to the border of the penumbra
1263 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1264 if (spotShadowRRect.isOval()) {
1265 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1266 } else {
1267 SkScalar outsetRad = spotRadius + blurOutset;
1268 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1269 }
1270
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001271 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001272
Herb Derbyc76d4092020-10-07 16:46:15 -04001273 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1274 spotColor,
1275 viewMatrix,
1276 spotShadowRRect,
Brian Salomoneebe7352020-12-09 16:37:04 -05001277 2.0f * devSpaceSpotBlur,
Herb Derbyc76d4092020-10-07 16:46:15 -04001278 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001279 if (op) {
1280 this->addDrawOp(clip, std::move(op));
1281 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001282 }
1283
1284 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001285}
1286
1287///////////////////////////////////////////////////////////////////////////////
1288
Brian Salomoneebe7352020-12-09 16:37:04 -05001289void GrSurfaceDrawContext::drawRegion(const GrClip* clip,
1290 GrPaint&& paint,
1291 GrAA aa,
1292 const SkMatrix& viewMatrix,
1293 const SkRegion& region,
1294 const GrStyle& style,
1295 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001296 ASSERT_SINGLE_OWNER
1297 RETURN_IF_ABANDONED
1298 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001299 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001300
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001301 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001302 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001303 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001304 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001305 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1306 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001307 aa = GrAA::kNo;
1308 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001309 }
msarettcc319b92016-08-25 18:07:18 -07001310 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001311 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001312 SkPath path;
1313 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001314 path.setIsVolatile(true);
1315
Brian Salomon82f44312017-01-11 13:42:54 -05001316 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001317 }
1318
Chris Dalton7d6748e2019-03-13 00:34:52 -06001319 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001320 GrOp::Owner op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1321 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001322 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001323}
1324
Brian Salomoneebe7352020-12-09 16:37:04 -05001325void GrSurfaceDrawContext::drawOval(const GrClip* clip,
1326 GrPaint&& paint,
1327 GrAA aa,
1328 const SkMatrix& viewMatrix,
1329 const SkRect& oval,
1330 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001331 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001332 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001333 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001334 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001335
Robert Phillips7484d202018-07-03 09:09:08 -04001336 const SkStrokeRec& stroke = style.strokeRec();
1337
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001338 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001339 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1340 return;
1341 }
1342
1343 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001344 return;
robertphillipsea461502015-05-26 11:38:03 -07001345 }
1346
Robert Phillips72152832017-01-25 17:31:35 -05001347 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001348
Chris Dalton7d6748e2019-03-13 00:34:52 -06001349 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001350
Brian Salomon56892dc2021-04-12 12:59:11 -04001351 bool preferFillRRectOverCircle = aaType == GrAAType::kCoverage &&
1352 this->caps()->drawInstancedSupport() &&
1353 this->caps()->reducedShaderMode();
1354
Herb Derbyc76d4092020-10-07 16:46:15 -04001355 GrOp::Owner op;
Brian Salomon56892dc2021-04-12 12:59:11 -04001356 if (!preferFillRRectOverCircle &&
1357 GrAAType::kCoverage == aaType &&
1358 oval.width() > SK_ScalarNearlyZero &&
1359 oval.width() == oval.height() &&
1360 viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001361 // We don't draw true circles as round rects in coverage mode, because it can
1362 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1363 assert_alive(paint);
1364 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1365 this->caps()->shaderCaps());
1366 }
1367 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001368 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1369 // the arc equation. This same special geometry and fragment branch also turn out to be a
1370 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1371 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1372 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001373 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001374 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
Chris Dalton61d694d2021-03-22 00:00:52 -06001375 oval, GrAA(aaType != GrAAType::kNone));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001376 }
Chris Dalton710e1c92021-04-23 13:07:52 -06001377 if (!op && (GrAAType::kCoverage == aaType || fCanUseDynamicMSAA)) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001378 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001379 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1380 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001381 }
1382 if (op) {
1383 this->addDrawOp(clip, std::move(op));
1384 return;
robertphillipsea461502015-05-26 11:38:03 -07001385 }
robertphillipsb56f9272016-02-25 11:03:52 -08001386
Mike Klein16885072018-12-11 09:54:31 -05001387 assert_alive(paint);
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001388 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1389 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001390 false, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001391}
1392
Brian Salomoneebe7352020-12-09 16:37:04 -05001393void GrSurfaceDrawContext::drawArc(const GrClip* clip,
1394 GrPaint&& paint,
1395 GrAA aa,
1396 const SkMatrix& viewMatrix,
1397 const SkRect& oval,
1398 SkScalar startAngle,
1399 SkScalar sweepAngle,
1400 bool useCenter,
1401 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001402 ASSERT_SINGLE_OWNER
1403 RETURN_IF_ABANDONED
1404 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001405 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001406
1407 AutoCheckFlush acf(this->drawingManager());
1408
Chris Dalton7d6748e2019-03-13 00:34:52 -06001409 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton710e1c92021-04-23 13:07:52 -06001410 if (GrAAType::kCoverage == aaType || fCanUseDynamicMSAA) {
Greg Daniel2655ede2019-04-10 00:49:28 +00001411 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Herb Derbyc76d4092020-10-07 16:46:15 -04001412 GrOp::Owner op = GrOvalOpFactory::MakeArcOp(fContext,
1413 std::move(paint),
1414 viewMatrix,
1415 oval,
1416 startAngle,
1417 sweepAngle,
1418 useCenter,
1419 style,
1420 shaderCaps);
Greg Daniel2655ede2019-04-10 00:49:28 +00001421 if (op) {
1422 this->addDrawOp(clip, std::move(op));
1423 return;
1424 }
1425 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001426 }
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001427 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1428 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001429 style, DoSimplify::kNo));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001430}
1431
Brian Salomoneebe7352020-12-09 16:37:04 -05001432void GrSurfaceDrawContext::drawImageLattice(const GrClip* clip,
1433 GrPaint&& paint,
1434 const SkMatrix& viewMatrix,
1435 GrSurfaceProxyView view,
1436 SkAlphaType alphaType,
1437 sk_sp<GrColorSpaceXform> csxf,
1438 GrSamplerState::Filter filter,
1439 std::unique_ptr<SkLatticeIter> iter,
1440 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001441 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001442 RETURN_IF_ABANDONED
1443 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001444 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001445
Robert Phillips72152832017-01-25 17:31:35 -05001446 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001447
Herb Derbyc76d4092020-10-07 16:46:15 -04001448 GrOp::Owner op =
Greg Danieled96bca2019-12-05 15:05:54 -05001449 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001450 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001451 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001452}
1453
Brian Salomoneebe7352020-12-09 16:37:04 -05001454void GrSurfaceDrawContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001455 const SkRect& bounds) {
Herb Derbyc76d4092020-10-07 16:46:15 -04001456 GrOp::Owner op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001457 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001458 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001459}
1460
Brian Salomoneebe7352020-12-09 16:37:04 -05001461bool GrSurfaceDrawContext::waitOnSemaphores(int numSemaphores,
1462 const GrBackendSemaphore waitSemaphores[],
1463 bool deleteSemaphoresAfterWait) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001464 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001465 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001466 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001467 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001468
1469 AutoCheckFlush acf(this->drawingManager());
1470
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001471 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001472 return false;
1473 }
1474
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001475 auto direct = fContext->asDirectContext();
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001476 if (!direct) {
1477 return false;
1478 }
1479
1480 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001481
Greg Daniel414418d2020-07-08 11:44:25 -04001482 GrWrapOwnership ownership =
1483 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
1484
Greg Daniel301015c2019-11-18 14:06:46 -05001485 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1486 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001487 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001488 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -04001489 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001490 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001491 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1492 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001493 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001494}
joshualitt33a5fce2015-11-18 13:28:51 -08001495
Brian Salomoneebe7352020-12-09 16:37:04 -05001496void GrSurfaceDrawContext::drawPath(const GrClip* clip,
1497 GrPaint&& paint,
1498 GrAA aa,
1499 const SkMatrix& viewMatrix,
1500 const SkPath& path,
1501 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001502 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001503 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001504 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001505 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawPath", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001506
Chris Daltonfbc3a832021-02-15 23:16:17 -07001507 GrStyledShape shape(path, style, DoSimplify::kNo);
Chris Dalton21859012021-01-29 23:44:53 -07001508 this->drawShape(clip, std::move(paint), aa, viewMatrix, std::move(shape));
Robert Phillips20390c32018-08-17 11:01:03 -04001509}
1510
Brian Salomoneebe7352020-12-09 16:37:04 -05001511void GrSurfaceDrawContext::drawShape(const GrClip* clip,
1512 GrPaint&& paint,
1513 GrAA aa,
1514 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001515 GrStyledShape&& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04001516 ASSERT_SINGLE_OWNER
1517 RETURN_IF_ABANDONED
1518 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001519 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawShape", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001520
Brian Salomon2fad74a2017-12-20 13:28:55 -05001521 if (shape.isEmpty()) {
1522 if (shape.inverseFilled()) {
1523 this->drawPaint(clip, std::move(paint), viewMatrix);
1524 }
1525 return;
robertphillipsea461502015-05-26 11:38:03 -07001526 }
1527
Robert Phillips72152832017-01-25 17:31:35 -05001528 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001529
Michael Ludwig6a6de652020-04-30 20:16:36 -04001530 // If we get here in drawShape(), we definitely need to use path rendering
Chris Dalton21859012021-01-29 23:44:53 -07001531 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, std::move(shape),
Chris Dalton9f704a82021-02-04 15:07:56 -07001532 /* attemptDrawSimple */ true);
robertphillipsea461502015-05-26 11:38:03 -07001533}
1534
Brian Salomoneebe7352020-12-09 16:37:04 -05001535static SkIRect get_clip_bounds(const GrSurfaceDrawContext* rtc, const GrClip* clip) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001536 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
1537}
1538
Brian Salomoneebe7352020-12-09 16:37:04 -05001539bool GrSurfaceDrawContext::drawAndStencilPath(const GrHardClip* clip,
1540 const GrUserStencilSettings* ss,
1541 SkRegion::Op op,
1542 bool invert,
1543 GrAA aa,
1544 const SkMatrix& viewMatrix,
1545 const SkPath& path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001546 ASSERT_SINGLE_OWNER
1547 RETURN_FALSE_IF_ABANDONED
1548 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001549 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAndStencilPath", fContext);
robertphillips391395d2016-03-02 09:26:36 -08001550
1551 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001552 GrPaint paint;
1553 paint.setCoverageSetOpXPFactory(op, invert);
1554 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon70fe17e2020-11-30 14:33:58 -05001555 SkRect::Make(this->dimensions()));
robertphillips391395d2016-03-02 09:26:36 -08001556 return true;
1557 }
1558
Brian Salomon70fe17e2020-11-30 14:33:58 -05001559 AutoCheckFlush acf(this->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001560
1561 // An Assumption here is that path renderer would use some form of tweaking
1562 // the src color (either the input alpha or in the frag shader) to implement
1563 // aa. If we have some future driver-mojo path AA that can do the right
1564 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon70fe17e2020-11-30 14:33:58 -05001565 GrAAType aaType = this->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001566 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001567
Brian Salomon70fe17e2020-11-30 14:33:58 -05001568 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001569
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001570 GrPaint paint;
1571 paint.setCoverageSetOpXPFactory(op, invert);
1572
Michael Ludwig2686d692020-04-17 20:21:37 +00001573 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001574 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001575 canDrawArgs.fCaps = this->caps();
1576 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08001577 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001578 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001579 canDrawArgs.fPaint = &paint;
Chris Dalton8e3bca62021-04-26 15:49:21 -06001580 canDrawArgs.fSurfaceProps = &fSurfaceProps;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001581 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001582 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001583 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001584
1585 // Don't allow the SW renderer
Brian Salomon70fe17e2020-11-30 14:33:58 -05001586 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001587 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001588 if (!pr) {
1589 return false;
1590 }
1591
Brian Salomon70fe17e2020-11-30 14:33:58 -05001592 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001593 std::move(paint),
1594 ss,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001595 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001596 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04001597 &clipConservativeBounds,
1598 &viewMatrix,
1599 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001600 aaType,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001601 this->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001602 pr->drawPath(args);
1603 return true;
1604}
1605
Brian Salomoneebe7352020-12-09 16:37:04 -05001606SkBudgeted GrSurfaceDrawContext::isBudgeted() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001607 ASSERT_SINGLE_OWNER
robertphillips714712b2016-08-04 06:20:45 -07001608
Brian Salomon70fe17e2020-11-30 14:33:58 -05001609 if (fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001610 return SkBudgeted::kNo;
1611 }
1612
Brian Salomon70fe17e2020-11-30 14:33:58 -05001613 SkDEBUGCODE(this->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001614
Brian Salomon70fe17e2020-11-30 14:33:58 -05001615 return this->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001616}
1617
Chris Dalton319d0202021-01-29 23:49:47 -07001618void GrSurfaceDrawContext::drawStrokedLine(const GrClip* clip, GrPaint&& paint,
1619 GrAA aa, const SkMatrix& viewMatrix,
1620 const SkPoint points[2], const SkStrokeRec& stroke) {
1621 ASSERT_SINGLE_OWNER
1622
1623 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style);
1624 SkASSERT(stroke.getWidth() > 0);
1625 // Adding support for round capping would require a
1626 // GrSurfaceDrawContext::fillRRectWithLocalMatrix entry point
1627 SkASSERT(SkPaint::kRound_Cap != stroke.getCap());
1628
1629 const SkScalar halfWidth = 0.5f * stroke.getWidth();
1630 if (halfWidth <= 0.f) {
1631 // Prevents underflow when stroke width is epsilon > 0 (so technically not a hairline).
1632 // The CTM would need to have a scale near 1/epsilon in order for this to have meaningful
1633 // coverage (although that would likely overflow elsewhere and cause the draw to drop due
1634 // to non-finite bounds). At any other scale, this line is so thin, it's coverage is
1635 // negligible, so discarding the draw is visually equivalent.
1636 return;
1637 }
1638
1639 SkVector parallel = points[1] - points[0];
1640
1641 if (!SkPoint::Normalize(&parallel)) {
1642 parallel.fX = 1.0f;
1643 parallel.fY = 0.0f;
1644 }
1645 parallel *= halfWidth;
1646
1647 SkVector ortho = { parallel.fY, -parallel.fX };
1648 if (SkPaint::kButt_Cap == stroke.getCap()) {
1649 // No extra extension for butt caps
1650 parallel = {0.f, 0.f};
1651 }
1652 // Order is TL, TR, BR, BL where arbitrarily "down" is p0 to p1 and "right" is positive
1653 SkPoint corners[4] = { points[0] - ortho - parallel,
1654 points[0] + ortho - parallel,
1655 points[1] + ortho + parallel,
1656 points[1] - ortho + parallel };
1657
1658 GrQuadAAFlags edgeAA = (aa == GrAA::kYes) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
1659 this->fillQuadWithEdgeAA(clip, std::move(paint), aa, edgeAA, viewMatrix, corners, nullptr);
1660}
1661
Chris Daltonbfe0d372021-02-04 12:24:54 -07001662bool GrSurfaceDrawContext::drawSimpleShape(const GrClip* clip, GrPaint* paint, GrAA aa,
1663 const SkMatrix& viewMatrix, const GrStyledShape& shape) {
1664 if (!shape.style().hasPathEffect()) {
1665 GrAAType aaType = this->chooseAAType(aa);
1666 SkPoint linePts[2];
1667 SkRRect rrect;
1668 // We can ignore the starting point and direction since there is no path effect.
1669 bool inverted;
1670 if (shape.asLine(linePts, &inverted) && !inverted &&
1671 shape.style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style &&
1672 shape.style().strokeRec().getCap() != SkPaint::kRound_Cap) {
1673 // The stroked line is an oriented rectangle, which looks the same or better (if
1674 // perspective) compared to path rendering. The exception is subpixel/hairline lines
1675 // that are non-AA or MSAA, in which case the default path renderer achieves higher
1676 // quality.
1677 // FIXME(michaelludwig): If the fill rect op could take an external coverage, or checks
1678 // for and outsets thin non-aa rects to 1px, the path renderer could be skipped.
1679 SkScalar coverage;
1680 if (aaType == GrAAType::kCoverage ||
1681 !SkDrawTreatAAStrokeAsHairline(shape.style().strokeRec().getWidth(), viewMatrix,
1682 &coverage)) {
1683 this->drawStrokedLine(clip, std::move(*paint), aa, viewMatrix, linePts,
1684 shape.style().strokeRec());
1685 return true;
1686 }
1687 } else if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1688 if (rrect.isRect()) {
1689 this->drawRect(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1690 &shape.style());
1691 return true;
1692 } else if (rrect.isOval()) {
1693 this->drawOval(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1694 shape.style());
1695 return true;
1696 }
1697 this->drawRRect(clip, std::move(*paint), aa, viewMatrix, rrect, shape.style());
1698 return true;
1699 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1700 viewMatrix.rectStaysRect()) {
1701 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply the
1702 // matrix to all the points individually rather than just to the rect
1703 SkRect rects[2];
1704 if (shape.asNestedRects(rects)) {
1705 // Concave AA paths are expensive - try to avoid them for special cases
1706 GrOp::Owner op = GrStrokeRectOp::MakeNested(
1707 fContext, std::move(*paint), viewMatrix, rects);
1708 if (op) {
1709 this->addDrawOp(clip, std::move(op));
1710 }
1711 // Returning here indicates that there is nothing to draw in this case.
1712 return true;
1713 }
1714 }
1715 }
1716 return false;
1717}
1718
Brian Salomoneebe7352020-12-09 16:37:04 -05001719void GrSurfaceDrawContext::drawShapeUsingPathRenderer(const GrClip* clip,
1720 GrPaint&& paint,
1721 GrAA aa,
1722 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001723 GrStyledShape&& shape,
Chris Daltonbfe0d372021-02-04 12:24:54 -07001724 bool attemptDrawSimple) {
joshualitt1de610a2016-01-06 08:26:09 -08001725 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001726 RETURN_IF_ABANDONED
Brian Salomoneebe7352020-12-09 16:37:04 -05001727 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "internalDrawPath", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001728
Chris Dalton21859012021-01-29 23:44:53 -07001729 if (!viewMatrix.isFinite() || !shape.bounds().isFinite()) {
Jim Van Verthf86073a2018-10-02 13:05:38 -04001730 return;
1731 }
1732
Michael Ludwig7c12e282020-05-29 09:54:07 -04001733 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001734
Chris Dalton6ce447a2019-06-23 18:07:38 -06001735 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06001736
robertphillips68737822015-10-29 12:12:21 -07001737 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001738 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05001739 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07001740 canDrawArgs.fViewMatrix = &viewMatrix;
Chris Dalton21859012021-01-29 23:44:53 -07001741 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001742 canDrawArgs.fPaint = &paint;
Chris Dalton8e3bca62021-04-26 15:49:21 -06001743 canDrawArgs.fSurfaceProps = &fSurfaceProps;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001744 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001745 canDrawArgs.fHasUserStencilSettings = false;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001746 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001747
Chris Daltone6ae4762021-02-05 14:56:21 -07001748 constexpr static bool kDisallowSWPathRenderer = false;
1749 constexpr static bool kAllowSWPathRenderer = true;
1750 using DrawType = GrPathRendererChain::DrawType;
1751
1752 GrPathRenderer* pr = nullptr;
1753
1754 if (!shape.style().strokeRec().isFillStyle() && !shape.isEmpty()) {
1755 // Give the tessellation path renderer a chance to claim this stroke before we simplify it.
1756 GrPathRenderer* tess = this->drawingManager()->getTessellationPathRenderer();
1757 if (tess && tess->canDrawPath(canDrawArgs) == GrPathRenderer::CanDrawPath::kYes) {
1758 pr = tess;
1759 }
1760 }
1761
1762 if (!pr) {
1763 // The shape isn't a stroke that can be drawn directly. Simplify if possible.
Chris Daltonfbc3a832021-02-15 23:16:17 -07001764 shape.simplify();
Chris Daltone6ae4762021-02-05 14:56:21 -07001765
1766 if (shape.isEmpty() && !shape.inverseFilled()) {
1767 return;
1768 }
1769
1770 if (attemptDrawSimple || shape.simplified()) {
1771 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too complex
1772 // for dedicated draw ops. However, if GrStyledShape was able to reduce something we
1773 // ought to try again instead of going right to path rendering.
1774 if (this->drawSimpleShape(clip, &paint, aa, viewMatrix, shape)) {
1775 return;
1776 }
1777 }
1778
1779 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1780 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1781 DrawType::kColor);
1782 }
1783
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001784 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -05001785 if (styleScale == 0.0f) {
1786 return;
1787 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001788
Chris Dalton21859012021-01-29 23:44:53 -07001789 if (!pr && shape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001790 // It didn't work above, so try again with the path effect applied.
Chris Dalton21859012021-01-29 23:44:53 -07001791 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1792 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001793 return;
1794 }
Chris Daltone6ae4762021-02-05 14:56:21 -07001795 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1796 DrawType::kColor);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001797 }
1798 if (!pr) {
Chris Dalton21859012021-01-29 23:44:53 -07001799 if (shape.style().applies()) {
1800 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1801 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001802 return;
1803 }
Brian Salomone7df0bb2018-05-07 14:44:57 -04001804 // This time, allow SW renderer
Chris Daltone6ae4762021-02-05 14:56:21 -07001805 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kAllowSWPathRenderer,
1806 DrawType::kColor);
Brian Salomone7df0bb2018-05-07 14:44:57 -04001807 } else {
1808 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04001809#if GR_PATH_RENDERER_SPEW
1810 SkDebugf("falling back to: %s\n", pr->name());
1811#endif
bsalomon6663acf2016-05-10 09:14:17 -07001812 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001813 }
robertphillipsea461502015-05-26 11:38:03 -07001814
bsalomon8acedde2016-06-24 10:42:16 -07001815 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001816#ifdef SK_DEBUG
1817 SkDebugf("Unable to find path renderer compatible with path.\n");
1818#endif
1819 return;
1820 }
1821
Robert Phillips256c37b2017-03-01 14:32:46 -05001822 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001823 std::move(paint),
1824 &GrUserStencilSettings::kUnused,
1825 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001826 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001827 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001828 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001829 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001830 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001831 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001832 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001833}
1834
Brian Salomon467921e2017-03-06 16:17:12 -05001835static void op_bounds(SkRect* bounds, const GrOp* op) {
1836 *bounds = op->bounds();
1837 if (op->hasZeroArea()) {
1838 if (op->hasAABloat()) {
1839 bounds->outset(0.5f, 0.5f);
1840 } else {
1841 // We don't know which way the particular GPU will snap lines or points at integer
1842 // coords. So we ensure that the bounds is large enough for either snap.
1843 SkRect before = *bounds;
1844 bounds->roundOut(bounds);
1845 if (bounds->fLeft == before.fLeft) {
1846 bounds->fLeft -= 1;
1847 }
1848 if (bounds->fTop == before.fTop) {
1849 bounds->fTop -= 1;
1850 }
1851 if (bounds->fRight == before.fRight) {
1852 bounds->fRight += 1;
1853 }
1854 if (bounds->fBottom == before.fBottom) {
1855 bounds->fBottom += 1;
1856 }
1857 }
1858 }
1859}
1860
Derek Sollenberger396cd1d2021-04-02 15:44:36 +00001861void GrSurfaceDrawContext::addDrawOp(const GrClip* clip,
1862 GrOp::Owner op,
Brian Salomoneebe7352020-12-09 16:37:04 -05001863 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001864 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001865 if (fContext->abandoned()) {
Brian Salomon348a0372018-10-31 10:42:18 -04001866 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001867 }
Herb Derbyc76d4092020-10-07 16:46:15 -04001868 GrDrawOp* drawOp = (GrDrawOp*)op.get();
robertphillips2e1e51f2015-10-15 08:01:48 -07001869 SkDEBUGCODE(this->validate();)
Herb Derbyc76d4092020-10-07 16:46:15 -04001870 SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;)
Brian Salomoneebe7352020-12-09 16:37:04 -05001871 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001872
Brian Salomon467921e2017-03-06 16:17:12 -05001873 // Setup clip
1874 SkRect bounds;
1875 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001876 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Chris Daltonb4403a92021-05-27 14:59:27 -06001877 bool usesMSAA = drawOp->usesMSAA();
Michael Ludwig7c12e282020-05-29 09:54:07 -04001878 bool skipDraw = false;
1879 if (clip) {
1880 // Have a complex clip, so defer to its early clip culling
Brian Salomon590f5672020-12-16 11:44:47 -05001881 GrAAType aaType;
Chris Daltonb4403a92021-05-27 14:59:27 -06001882 if (usesMSAA) {
Brian Salomon590f5672020-12-16 11:44:47 -05001883 aaType = GrAAType::kMSAA;
1884 } else {
1885 aaType = op->hasAABloat() ? GrAAType::kCoverage : GrAAType::kNone;
1886 }
Chris Dalton6edc8d92021-05-27 09:40:43 -06001887 skipDraw = clip->apply(fContext, this, aaType,
Derek Sollenberger396cd1d2021-04-02 15:44:36 +00001888 &appliedClip, &bounds) == GrClip::Effect::kClippedOut;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001889 } else {
1890 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001891 skipDraw = !bounds.intersect(this->asSurfaceProxy()->getBoundsRect());
Michael Ludwig7c12e282020-05-29 09:54:07 -04001892 }
1893
1894 if (skipDraw) {
Brian Salomon348a0372018-10-31 10:42:18 -04001895 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001896 }
1897
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001898 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Chris Dalton57ab06c2021-04-22 12:57:28 -06001899 GrProcessorSet::Analysis analysis = drawOp->finalize(*this->caps(), &appliedClip, clampType);
Chris Dalton6ce447a2019-06-23 18:07:38 -06001900
Chris Daltona1a3afe2021-06-03 14:31:59 -06001901 // Note if the op needs stencil. Stencil clipping already called setNeedsStencil for itself, if
1902 // needed.
1903 if (drawOp->usesStencil()) {
1904 this->setNeedsStencil();
1905 if (fCanUseDynamicMSAA) {
1906 // Always trigger DMSAA when there is stencil. This ensures stencil contents get
1907 // properly preserved between render passes, if needed.
1908 usesMSAA = true;
1909 }
1910 }
1911
Michael Ludwig28e5f112020-06-09 10:57:24 -04001912 // Must be called before setDstProxyView so that it sees the final bounds of the op.
1913 op->setClippedBounds(bounds);
1914
John Stiles52cb1d02021-06-02 11:58:05 -04001915 GrDstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07001916 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04001917 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Brian Salomon348a0372018-10-31 10:42:18 -04001918 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001919 }
1920 }
1921
Greg Danielf41b2bd2019-08-22 16:19:24 -04001922 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04001923 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04001924 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04001925 }
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001926
1927#if GR_GPU_STATS && GR_TEST_UTILS
Chris Daltonb4403a92021-05-27 14:59:27 -06001928 if (fCanUseDynamicMSAA && usesMSAA) {
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001929 if (!opsTask->usesMSAASurface()) {
1930 fContext->priv().dmsaaStats().fNumMultisampleRenderPasses++;
1931 }
1932 fContext->priv().dmsaaStats().fTriggerCounts[op->name()]++;
1933 }
1934#endif
1935
Chris Daltonb4403a92021-05-27 14:59:27 -06001936 opsTask->addDrawOp(this->drawingManager(), std::move(op), usesMSAA, analysis,
Chris Dalton710e1c92021-04-23 13:07:52 -06001937 std::move(appliedClip), dstProxyView,
1938 GrTextureResolveManager(this->drawingManager()), *this->caps());
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001939
1940#ifdef SK_DEBUG
Chris Daltonb4403a92021-05-27 14:59:27 -06001941 if (fCanUseDynamicMSAA && usesMSAA) {
Chris Dalton5dfb3f42021-04-30 17:16:12 -06001942 SkASSERT(opsTask->usesMSAASurface());
1943 }
1944#endif
Brian Salomon54d212e2017-03-21 14:22:38 -04001945}
1946
John Stiles52cb1d02021-06-02 11:58:05 -04001947bool GrSurfaceDrawContext::setupDstProxyView(const GrOp& op, GrDstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001948 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
1949 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
1950 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05001951 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001952 return false;
1953 }
1954
Greg Daniel87fab9f2021-06-07 15:18:23 -04001955 auto dstSampleFlags = this->caps()->getDstSampleFlagsForProxy(this->asRenderTargetProxy());
Greg Danield358cbe2020-09-11 09:33:54 -04001956
Greg Daniel87fab9f2021-06-07 15:18:23 -04001957 if (dstSampleFlags & GrDstSampleFlags::kRequiresTextureBarrier) {
1958 // If we require a barrier to sample the dst it means we are sampling the RT itself either
1959 // as a texture or input attachment.
Greg Danield358cbe2020-09-11 09:33:54 -04001960 dstProxyView->setProxyView(this->readSurfaceView());
1961 dstProxyView->setOffset(0, 0);
Greg Daniel87fab9f2021-06-07 15:18:23 -04001962 dstProxyView->setDstSampleFlags(dstSampleFlags);
Greg Danield358cbe2020-09-11 09:33:54 -04001963 return true;
1964 }
Brian Salomon467921e2017-03-06 16:17:12 -05001965
Greg Daniel3155f7f2020-01-16 16:54:26 -05001966 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05001967 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1968 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04001969 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05001970 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05001971
Michael Ludwig28e5f112020-06-09 10:57:24 -04001972 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001973 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04001974 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
1975 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
1976 // centers during rasterization.
1977 SkIRect conservativeDrawBounds = op.bounds().roundOut();
1978 conservativeDrawBounds.outset(1, 1);
1979 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07001980 }
Brian Salomon467921e2017-03-06 16:17:12 -05001981
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001982 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001983 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001984 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07001985 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001986 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001987 } else {
Eric Karl74480882017-04-03 14:49:05 -07001988 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001989 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001990 }
Brian Salomon982127b2021-01-21 10:43:35 -05001991 auto copy = GrSurfaceProxy::Copy(fContext,
1992 this->asSurfaceProxyRef(),
1993 this->origin(),
1994 GrMipmapped::kNo,
1995 copyRect,
1996 fit,
1997 SkBudgeted::kYes,
1998 restrictions.fRectsMustMatch);
Brian Salomonc5243782020-04-02 12:50:34 -04001999 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002000
Brian Salomonc5243782020-04-02 12:50:34 -04002001 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002002 dstProxyView->setOffset(dstOffset);
Greg Daniel87fab9f2021-06-07 15:18:23 -04002003 dstProxyView->setDstSampleFlags(dstSampleFlags);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002004 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002005}