blob: 5f8c49bc66a71804d377f9cc85e97c45e9dc590b [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"
Herb Derbybf2dd2a2021-03-04 10:13:22 -050072#include "src/gpu/text/GrSDFTControl.h"
Herb Derby64688222020-06-08 14:19:57 -040073#include "src/gpu/text/GrTextBlobCache.h"
joshualittbc907352016-01-13 06:45:40 -080074
Robert Phillips72152832017-01-25 17:31:35 -050075#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -040076#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040077#define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
Robert Phillips9eb00022020-06-30 15:30:12 -040078#define RETURN_FALSE_IF_ABANDONED if (fContext->abandoned()) { return false; }
robertphillipsea461502015-05-26 11:38:03 -070079
Brian Salomone225b562017-06-14 13:00:03 -040080//////////////////////////////////////////////////////////////////////////////
81
Chris Daltonfbc3a832021-02-15 23:16:17 -070082using DoSimplify = GrStyledShape::DoSimplify;
Chris Daltonb8ed2bc2021-02-02 11:53:19 -070083
robertphillipsea461502015-05-26 11:38:03 -070084class AutoCheckFlush {
85public:
halcanary9d524f22016-03-29 09:03:52 -070086 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070087 SkASSERT(fDrawingManager);
88 }
bsalomonb77a9072016-09-07 10:02:04 -070089 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070090
91private:
robertphillips77a2e522015-10-17 07:43:27 -070092 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070093};
94
Brian Salomoneebe7352020-12-09 16:37:04 -050095std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(GrRecordingContext* context,
96 GrColorType colorType,
97 sk_sp<SkColorSpace> colorSpace,
98 sk_sp<GrSurfaceProxy> proxy,
99 GrSurfaceOrigin origin,
100 const SkSurfaceProps* surfaceProps,
101 bool flushTimeOpsTask) {
Greg Danielba0ff782020-01-07 15:42:57 -0500102 if (!proxy) {
103 return nullptr;
104 }
105
106 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400107 GrSwizzle readSwizzle, writeSwizzle;
108 if (colorType != GrColorType::kUnknown) {
109 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
110 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
111 }
Greg Danielba0ff782020-01-07 15:42:57 -0500112
Brian Salomon07bc9a22020-12-02 13:37:16 -0500113 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400114 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500115
Brian Salomoneebe7352020-12-09 16:37:04 -0500116 return std::make_unique<GrSurfaceDrawContext>(context,
117 std::move(readView),
118 std::move(writeView),
119 colorType,
120 std::move(colorSpace),
121 surfaceProps,
122 flushTimeOpsTask);
Greg Danielba0ff782020-01-07 15:42:57 -0500123}
124
Brian Salomoneebe7352020-12-09 16:37:04 -0500125std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Danielba0ff782020-01-07 15:42:57 -0500126 GrRecordingContext* context,
Greg Danielba0ff782020-01-07 15:42:57 -0500127 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500128 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500129 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500130 const GrBackendFormat& format,
131 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400132 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500133 GrProtected isProtected,
Brian Salomon07bc9a22020-12-02 13:37:16 -0500134 GrSwizzle readSwizzle,
135 GrSwizzle writeSwizzle,
Greg Daniele20fcad2020-01-08 11:52:34 -0500136 GrSurfaceOrigin origin,
137 SkBudgeted budgeted,
138 const SkSurfaceProps* surfaceProps) {
139 // It is probably not necessary to check if the context is abandoned here since uses of the
Brian Salomoneebe7352020-12-09 16:37:04 -0500140 // GrSurfaceDrawContext which need the context will mostly likely fail later on without an
Greg Daniele20fcad2020-01-08 11:52:34 -0500141 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
142 // an abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -0400143 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500144 return nullptr;
145 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500146
Greg Daniele20fcad2020-01-08 11:52:34 -0500147 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomon07bc9a22020-12-02 13:37:16 -0500148 format,
149 dimensions,
150 GrRenderable::kYes,
151 sampleCnt,
152 mipMapped,
153 fit,
154 budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500155 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500156 if (!proxy) {
157 return nullptr;
158 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500159
Brian Salomon07bc9a22020-12-02 13:37:16 -0500160 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
161 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
162
Brian Salomoneebe7352020-12-09 16:37:04 -0500163 auto rtc = std::make_unique<GrSurfaceDrawContext>(context,
164 std::move(readView),
165 std::move(writeView),
166 GrColorType::kUnknown,
167 std::move(colorSpace),
168 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500169 rtc->discard();
170 return rtc;
171}
172
Brian Salomoneebe7352020-12-09 16:37:04 -0500173std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500174 GrRecordingContext* context,
175 GrColorType colorType,
176 sk_sp<SkColorSpace> colorSpace,
177 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500178 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400180 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500181 GrProtected isProtected,
182 GrSurfaceOrigin origin,
183 SkBudgeted budgeted,
184 const SkSurfaceProps* surfaceProps) {
185 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
186 if (!format.isValid()) {
187 return nullptr;
188 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500189 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
190 dimensions,
191 GrRenderable::kYes,
192 sampleCnt,
193 mipMapped,
194 fit,
195 budgeted,
196 isProtected);
Brian Salomon07bc9a22020-12-02 13:37:16 -0500197 if (!proxy) {
198 return nullptr;
199 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500200
Brian Salomoneebe7352020-12-09 16:37:04 -0500201 return GrSurfaceDrawContext::Make(context,
202 colorType,
203 std::move(colorSpace),
204 std::move(proxy),
205 origin,
206 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500207}
208
Brian Salomoneebe7352020-12-09 16:37:04 -0500209std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeWithFallback(
Greg Daniele20fcad2020-01-08 11:52:34 -0500210 GrRecordingContext* context,
211 GrColorType colorType,
212 sk_sp<SkColorSpace> colorSpace,
213 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500214 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500215 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400216 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500217 GrProtected isProtected,
218 GrSurfaceOrigin origin,
219 SkBudgeted budgeted,
220 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400221 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400222 if (ct == GrColorType::kUnknown) {
223 return nullptr;
224 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500225 return GrSurfaceDrawContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
226 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500227}
228
Brian Salomoneebe7352020-12-09 16:37:04 -0500229std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendTexture(
Greg Danielba0ff782020-01-07 15:42:57 -0500230 GrRecordingContext* context,
231 GrColorType colorType,
232 sk_sp<SkColorSpace> colorSpace,
233 const GrBackendTexture& tex,
234 int sampleCnt,
235 GrSurfaceOrigin origin,
236 const SkSurfaceProps* surfaceProps,
Robert Phillipsa1121332020-06-29 13:05:29 -0400237 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500238 SkASSERT(sampleCnt > 0);
239 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400240 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
241 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500242 if (!proxy) {
243 return nullptr;
244 }
245
Brian Salomoneebe7352020-12-09 16:37:04 -0500246 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
247 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500248}
249
Brian Salomoneebe7352020-12-09 16:37:04 -0500250std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendRenderTarget(
Greg Danielba0ff782020-01-07 15:42:57 -0500251 GrRecordingContext* context,
252 GrColorType colorType,
253 sk_sp<SkColorSpace> colorSpace,
254 const GrBackendRenderTarget& rt,
255 GrSurfaceOrigin origin,
256 const SkSurfaceProps* surfaceProps,
Brian Salomon8ba54ea2020-10-01 09:48:21 -0400257 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400258 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsa1121332020-06-29 13:05:29 -0400259 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500260 if (!proxy) {
261 return nullptr;
262 }
263
Brian Salomoneebe7352020-12-09 16:37:04 -0500264 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
265 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500266}
267
Brian Salomoneebe7352020-12-09 16:37:04 -0500268std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromVulkanSecondaryCB(
Greg Danielba0ff782020-01-07 15:42:57 -0500269 GrRecordingContext* context,
270 const SkImageInfo& imageInfo,
271 const GrVkDrawableInfo& vkInfo,
272 const SkSurfaceProps* props) {
273 sk_sp<GrSurfaceProxy> proxy(
274 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
275 vkInfo));
276 if (!proxy) {
277 return nullptr;
278 }
279
Brian Salomoneebe7352020-12-09 16:37:04 -0500280 return GrSurfaceDrawContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
281 imageInfo.refColorSpace(), std::move(proxy),
282 kTopLeft_GrSurfaceOrigin, props);
Greg Danielba0ff782020-01-07 15:42:57 -0500283}
284
Greg Danielf41b2bd2019-08-22 16:19:24 -0400285// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
286// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
287// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500288// when the surfaceDrawContext attempts to use it (via getOpsTask).
Brian Salomoneebe7352020-12-09 16:37:04 -0500289GrSurfaceDrawContext::GrSurfaceDrawContext(GrRecordingContext* context,
290 GrSurfaceProxyView readView,
291 GrSurfaceProxyView writeView,
292 GrColorType colorType,
293 sk_sp<SkColorSpace> colorSpace,
294 const SkSurfaceProps* surfaceProps,
295 bool flushTimeOpsTask)
Brian Salomon590f5672020-12-16 11:44:47 -0500296 : GrSurfaceFillContext(context,
297 std::move(readView),
298 std::move(writeView),
299 {colorType, kPremul_SkAlphaType, std::move(colorSpace)},
300 flushTimeOpsTask)
Brian Salomonf3569f02017-10-24 12:52:33 -0400301 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400302 , fGlyphPainter(*this) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700303 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700304}
305
Brian Salomoneebe7352020-12-09 16:37:04 -0500306GrSurfaceDrawContext::~GrSurfaceDrawContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800307 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700308}
309
Brian Salomoneebe7352020-12-09 16:37:04 -0500310inline GrAAType GrSurfaceDrawContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600311 if (GrAA::kNo == aa) {
312 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
313 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600314 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600315 return GrAAType::kMSAA;
316 }
317 return GrAAType::kNone;
318 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600319 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600320}
321
Brian Salomoneebe7352020-12-09 16:37:04 -0500322GrMipmapped GrSurfaceDrawContext::mipmapped() const {
Greg Daniele252f082017-10-23 16:05:23 -0400323 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400324 return proxy->mipmapped();
Greg Daniele252f082017-10-23 16:05:23 -0400325 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400326 return GrMipmapped::kNo;
Greg Daniele252f082017-10-23 16:05:23 -0400327}
328
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500329void GrSurfaceDrawContext::drawGlyphRunListNoCache(const GrClip* clip,
330 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400331 const SkGlyphRunList& glyphRunList,
332 const SkPaint& paint) {
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500333 GrSDFTControl control =
334 fContext->priv().getSDFTControl(fSurfaceProps.isUseDeviceIndependentFonts());
335 const SkPoint drawOrigin = glyphRunList.origin();
336 SkMatrix drawMatrix = viewMatrix.localToDevice();
337 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
338 GrSubRunAllocator* const alloc = this->recordingContext()->priv().recordTimeSubRunAllocator();
robertphillips2d70dcb2015-10-06 07:38:23 -0700339
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500340 for (auto& glyphRun : glyphRunList) {
Herb Derby0da2c142021-03-22 15:28:23 -0400341 GrSubRunNoCachePainter painter{this, alloc, clip, viewMatrix, glyphRunList, paint};
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500342
343 // Make and add the text ops.
344 fGlyphPainter.processGlyphRun(glyphRun,
345 drawMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400346 paint,
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500347 control,
348 &painter);
Greg Danielbe7fc462019-01-03 16:40:42 -0500349 }
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500350}
Greg Danielbe7fc462019-01-03 16:40:42 -0500351
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500352void GrSurfaceDrawContext::drawGlyphRunListWithCache(const GrClip* clip,
353 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400354 const SkGlyphRunList& glyphRunList,
355 const SkPaint& paint) {
Herb Derby64477842021-03-08 13:47:06 -0500356 SkMatrix drawMatrix(viewMatrix.localToDevice());
357 drawMatrix.preTranslate(glyphRunList.origin().x(), glyphRunList.origin().y());
358
Herb Derbybf2dd2a2021-03-04 10:13:22 -0500359 GrSDFTControl control =
360 this->recordingContext()->priv().getSDFTControl(
Herb Derby9ad09822021-03-01 14:19:36 -0500361 this->surfaceProps().isUseDeviceIndependentFonts());
362
Herb Derby64477842021-03-08 13:47:06 -0500363 auto [canCache, key] = GrTextBlob::Key::Make(glyphRunList,
Herb Derby0da2c142021-03-22 15:28:23 -0400364 paint,
Herb Derby64477842021-03-08 13:47:06 -0500365 fSurfaceProps,
366 this->colorInfo(),
367 drawMatrix,
368 control);
Herb Derby64688222020-06-08 14:19:57 -0400369
370 sk_sp<GrTextBlob> blob;
Herb Derby64477842021-03-08 13:47:06 -0500371 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
Herb Derby64688222020-06-08 14:19:57 -0400372 if (canCache) {
Herb Derby64688222020-06-08 14:19:57 -0400373 blob = textBlobCache->find(key);
374 }
375
Herb Derby0da2c142021-03-22 15:28:23 -0400376 if (blob == nullptr || !blob->canReuse(paint, drawMatrix)) {
Herb Derby64688222020-06-08 14:19:57 -0400377 if (blob != nullptr) {
Herb Derbye4ef35c2021-03-01 11:49:32 -0500378 SkASSERT(!drawMatrix.hasPerspective());
Herb Derby64688222020-06-08 14:19:57 -0400379 // We have to remake the blob because changes may invalidate our masks.
380 // TODO we could probably get away with reuse most of the time if the pointer is unique,
381 // but we'd have to clear the SubRun information
382 textBlobCache->remove(blob.get());
383 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400384
Herb Derby0da2c142021-03-22 15:28:23 -0400385 blob = GrTextBlob::Make(glyphRunList, paint, drawMatrix, control, &fGlyphPainter);
Herb Derby14992492021-02-25 16:28:03 -0500386
387 if (canCache) {
388 blob->addKey(key);
389 // The blob may already have been created on a different thread. Use the first one
390 // that was there.
391 blob = textBlobCache->addOrReturnExisting(glyphRunList, blob);
392 }
Herb Derby64688222020-06-08 14:19:57 -0400393 }
394
Herb Derby6b27ae82021-02-23 13:03:59 -0500395 for (const GrSubRun& subRun : blob->subRunList()) {
Herb Derby0da2c142021-03-22 15:28:23 -0400396 subRun.draw(clip, viewMatrix, glyphRunList, paint, this);
Herb Derbye5b768b2020-07-07 15:56:07 -0400397 }
robertphillipsea461502015-05-26 11:38:03 -0700398}
399
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500400// choose to use the GrTextBlob cache or not.
401bool gGrDrawTextNoCache = false;
402void GrSurfaceDrawContext::drawGlyphRunList(const GrClip* clip,
403 const SkMatrixProvider& viewMatrix,
Herb Derby0da2c142021-03-22 15:28:23 -0400404 const SkGlyphRunList& glyphRunList,
405 const SkPaint& paint) {
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500406 ASSERT_SINGLE_OWNER
407 RETURN_IF_ABANDONED
408 SkDEBUGCODE(this->validate();)
409 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawGlyphRunList", fContext);
410
411 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
412 // secondary command buffers because it would require stopping and starting a render pass which
413 // we don't have access to.
414 if (this->wrapsVkSecondaryCB()) {
415 return;
416 }
417
418 if (gGrDrawTextNoCache) {
419 // drawGlyphRunListNoCache lives in GrTextBlob.cpp to share sub run implementation code.
Herb Derby0da2c142021-03-22 15:28:23 -0400420 this->drawGlyphRunListNoCache(clip, viewMatrix, glyphRunList, paint);
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500421 } else {
Herb Derby0da2c142021-03-22 15:28:23 -0400422 this->drawGlyphRunListWithCache(clip, viewMatrix, glyphRunList, paint);
Herb Derby2e0b5bb2020-11-19 11:03:50 -0500423 }
424}
425
Brian Salomoneebe7352020-12-09 16:37:04 -0500426void GrSurfaceDrawContext::drawPaint(const GrClip* clip,
427 GrPaint&& paint,
428 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000429 // Start with the render target, since that is the maximum content we could possibly fill.
430 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400431 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000432 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
433 // inverse view matrix.
Chris Dalton35619552021-03-10 19:20:43 -0700434 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig61328202019-06-19 14:48:58 +0000435 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
436 } else {
437 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
438 SkMatrix localMatrix;
439 if (!viewMatrix.invert(&localMatrix)) {
440 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400441 }
Chris Dalton35619552021-03-10 19:20:43 -0700442 SkIRect bounds = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
443 this->fillPixelsWithLocalMatrix(clip, std::move(paint), bounds, localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700444 }
robertphillipsea461502015-05-26 11:38:03 -0700445}
446
Brian Salomoneebe7352020-12-09 16:37:04 -0500447enum class GrSurfaceDrawContext::QuadOptimization {
Michael Ludwig61328202019-06-19 14:48:58 +0000448 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
449 kDiscarded,
450 // The rect to draw was converted to some other op and appended to the oplist, so no additional
451 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
452 // a constColor is provided.
453 kSubmitted,
454 // The clip was folded into the device quad, with updated edge flags and local coords, and
455 // caller is responsible for adding an appropriate op.
456 kClipApplied,
457 // No change to clip, but quad updated to better fit clip/render target, and caller is
458 // responsible for adding an appropriate op.
459 kCropped
460};
Michael Ludwig61a16512019-01-15 11:15:13 -0500461
Brian Salomoneebe7352020-12-09 16:37:04 -0500462GrSurfaceDrawContext::QuadOptimization GrSurfaceDrawContext::attemptQuadOptimization(
Chris Dalton61d694d2021-03-22 00:00:52 -0600463 const GrClip* clip, const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad,
464 GrPaint* paint) {
Michael Ludwig61328202019-06-19 14:48:58 +0000465 // Optimization requirements:
466 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500467 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
468 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
469 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
470 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
471 // 5. kCropped in all other scenarios (although a crop may be a no-op)
Chris Dalton61d694d2021-03-22 00:00:52 -0600472 const SkPMColor4f* constColor = nullptr;
473 SkPMColor4f paintColor;
474 if (!stencilSettings && paint && !paint->hasCoverageFragmentProcessor() &&
475 paint->isConstantBlendedColor(&paintColor)) {
476 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
477 constColor = &paintColor;
478 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500479
480 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
481 // better to just keep the old flags instead of introducing mixed edge flags.
482 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000483
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400484 // Use the logical size of the render target, which allows for "fullscreen" clears even if
485 // the render target has an approximate backing fit
486 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400487
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500488 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400489 if (!quad->fDevice.isFinite() || drawBounds.isEmpty() ||
490 GrClip::IsOutsideClip(rtRect, drawBounds)) {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000491 return QuadOptimization::kDiscarded;
492 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400493 auto conservativeCrop = [&]() {
494 static constexpr int kLargeDrawLimit = 15000;
495 // Crop the quad to the render target. This doesn't change the visual results of drawing but
496 // is meant to help numerical stability for excessively large draws.
497 if (drawBounds.width() > kLargeDrawLimit || drawBounds.height() > kLargeDrawLimit) {
498 GrQuadUtils::CropToRect(rtRect, *aa, quad, /* compute local */ !constColor);
499 SkASSERT(quad->fEdgeFlags == oldFlags);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000500 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400501 };
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000502
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400503 bool simpleColor = !stencilSettings && constColor;
Michael Ludwig6397e802020-08-05 15:50:01 -0400504 GrClip::PreClipResult result = clip ? clip->preApply(drawBounds, *aa)
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400505 : GrClip::PreClipResult(GrClip::Effect::kUnclipped);
506 switch(result.fEffect) {
507 case GrClip::Effect::kClippedOut:
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000508 return QuadOptimization::kDiscarded;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400509 case GrClip::Effect::kUnclipped:
510 if (!simpleColor) {
511 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000512 return QuadOptimization::kClipApplied;
513 } else {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400514 // Update result to store the render target bounds in order and then fall
515 // through to attempt the draw->native clear optimization
516 result = GrClip::PreClipResult(SkRRect::MakeRect(rtRect), *aa);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000517 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400518 break;
519 case GrClip::Effect::kClipped:
520 if (!result.fIsRRect || (stencilSettings && result.fAA != *aa) ||
521 (!result.fRRect.isRect() && !simpleColor)) {
522 // The clip and draw state are too complicated to try and reduce
523 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000524 return QuadOptimization::kCropped;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400525 } // Else fall through to attempt to combine the draw and clip geometry together
526 break;
527 default:
528 SkUNREACHABLE;
529 }
530
531 // If we reached here, we know we're an axis-aligned clip that is either a rect or a round rect,
532 // so we can potentially combine it with the draw geometry so that no clipping is needed.
533 SkASSERT(result.fEffect == GrClip::Effect::kClipped && result.fIsRRect);
534 SkRect clippedBounds = result.fRRect.getBounds();
535 clippedBounds.intersect(rtRect);
536 if (result.fRRect.isRect()) {
537 // No rounded corners, so we might be able to become a native clear or we might be able to
538 // modify geometry and edge flags to represent intersected shape of clip and draw.
539 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
540 /*compute local*/ !constColor)) {
541 if (simpleColor && quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
542 // Clear optimization is possible
543 drawBounds = quad->fDevice.bounds();
544 if (drawBounds.contains(rtRect)) {
545 // Fullscreen clear
546 this->clear(*constColor);
547 return QuadOptimization::kSubmitted;
548 } else if (GrClip::IsPixelAligned(drawBounds) &&
549 drawBounds.width() > 256 && drawBounds.height() > 256) {
550 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
551 SkIRect scissorRect;
552 drawBounds.round(&scissorRect);
553 this->clear(scissorRect, *constColor);
554 return QuadOptimization::kSubmitted;
555 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000556 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400557
558 // else the draw and clip were combined so just update the AA to reflect combination
559 if (*aa == GrAA::kNo && result.fAA == GrAA::kYes &&
560 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
561 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
562 // properly reflect the smooth edge of the clip.
563 *aa = GrAA::kYes;
564 }
565 // We intentionally do not downgrade AA here because we don't know if we need to
566 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
567 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
568 // deviceQuad is exactly the intersection of original quad and clip, so it can be
569 // drawn with no clip (submitted by caller)
570 return QuadOptimization::kClipApplied;
571 }
572 } else {
573 // Rounded corners and constant filled color (limit ourselves to solid colors because
574 // there is no way to use custom local coordinates with drawRRect).
575 SkASSERT(simpleColor);
576 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
577 /* compute local */ false) &&
578 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
579 quad->fDevice.bounds().contains(clippedBounds)) {
580 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
581 // we can draw the rrect directly and ignore the edge flags
Chris Dalton61d694d2021-03-22 00:00:52 -0600582 this->drawRRect(nullptr, std::move(*paint), result.fAA, SkMatrix::I(), result.fRRect,
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400583 GrStyle::SimpleFill());
584 return QuadOptimization::kSubmitted;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000585 }
586 }
587
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400588 // The quads have been updated to better fit the clip bounds, but can't get rid of
589 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500590 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000591 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000592}
593
Brian Salomoneebe7352020-12-09 16:37:04 -0500594void GrSurfaceDrawContext::drawFilledQuad(const GrClip* clip,
595 GrPaint&& paint,
596 GrAA aa,
597 DrawQuad* quad,
598 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000599 ASSERT_SINGLE_OWNER
600 RETURN_IF_ABANDONED
601 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500602 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500603
Michael Ludwig61328202019-06-19 14:48:58 +0000604 AutoCheckFlush acf(this->drawingManager());
605
Chris Dalton61d694d2021-03-22 00:00:52 -0600606 QuadOptimization opt = this->attemptQuadOptimization(clip, ss, &aa, quad, &paint);
Michael Ludwig61328202019-06-19 14:48:58 +0000607 if (opt >= QuadOptimization::kClipApplied) {
608 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400609 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000610 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
611 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500612 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
613 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700614 }
Michael Ludwig61328202019-06-19 14:48:58 +0000615 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800616}
617
Chris Dalton4718a8b2021-03-18 18:38:52 -0600618void GrSurfaceDrawContext::drawTexture(const GrClip* clip,
619 GrSurfaceProxyView view,
620 SkAlphaType srcAlphaType,
621 GrSamplerState::Filter filter,
622 GrSamplerState::MipmapMode mm,
623 SkBlendMode blendMode,
624 const SkPMColor4f& color,
625 const SkRect& srcRect,
626 const SkRect& dstRect,
627 GrAA aa,
628 GrQuadAAFlags edgeAA,
629 SkCanvas::SrcRectConstraint constraint,
630 const SkMatrix& viewMatrix,
631 sk_sp<GrColorSpaceXform> colorSpaceXform) {
632 // If we are using dmsaa then go through GrFillRRectOp (via fillRectToRect).
633 if (fContext->priv().alwaysAntialias() && aa == GrAA::kYes) {
634 GrPaint paint;
635 paint.setColor4f(color);
636 std::unique_ptr<GrFragmentProcessor> fp;
637 if (constraint == SkCanvas::kStrict_SrcRectConstraint) {
638 fp = GrTextureEffect::MakeSubset(view, srcAlphaType, SkMatrix::I(),
639 GrSamplerState(filter, mm), srcRect,
640 *this->caps());
641 } else {
642 fp = GrTextureEffect::Make(view, srcAlphaType, SkMatrix::I(), filter, mm);
643 }
644 if (colorSpaceXform) {
645 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
646 }
647 fp = GrBlendFragmentProcessor::Make(std::move(fp), nullptr, SkBlendMode::kModulate);
648 paint.setColorFragmentProcessor(std::move(fp));
649 if (blendMode != SkBlendMode::kSrcOver) {
650 paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode));
651 }
652 this->fillRectToRect(clip, std::move(paint), GrAA::kYes, viewMatrix, dstRect, srcRect);
653 return;
654 }
655
656 const SkRect* subset = constraint == SkCanvas::kStrict_SrcRectConstraint ?
657 &srcRect : nullptr;
658 DrawQuad quad{GrQuad::MakeFromRect(dstRect, viewMatrix), GrQuad(srcRect), edgeAA};
659
660 this->drawTexturedQuad(clip, std::move(view), srcAlphaType, std::move(colorSpaceXform), filter,
661 mm, color, blendMode, aa, &quad, subset);
662}
663
Brian Salomoneebe7352020-12-09 16:37:04 -0500664void GrSurfaceDrawContext::drawTexturedQuad(const GrClip* clip,
665 GrSurfaceProxyView proxyView,
666 SkAlphaType srcAlphaType,
667 sk_sp<GrColorSpaceXform> textureXform,
668 GrSamplerState::Filter filter,
669 GrSamplerState::MipmapMode mm,
670 const SkPMColor4f& color,
671 SkBlendMode blendMode,
672 GrAA aa,
673 DrawQuad* quad,
674 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000675 ASSERT_SINGLE_OWNER
676 RETURN_IF_ABANDONED
677 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400678 SkASSERT(proxyView.asTextureProxy());
Brian Salomoneebe7352020-12-09 16:37:04 -0500679 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTexturedQuad", fContext);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000680
681 AutoCheckFlush acf(this->drawingManager());
682
683 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
684 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Chris Dalton61d694d2021-03-22 00:00:52 -0600685 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr/*stencil*/, &aa, quad,
686 nullptr/*paint*/);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000687
688 SkASSERT(opt != QuadOptimization::kSubmitted);
689 if (opt != QuadOptimization::kDiscarded) {
690 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400691 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000692 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400693 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400694 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
695 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400696 // Use the provided subset, although hypothetically we could detect that the cropped local
697 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500698 this->addDrawOp(finalClip,
699 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400700 std::move(textureXform), filter, mm, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400701 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000702 }
703}
704
Brian Salomoneebe7352020-12-09 16:37:04 -0500705void GrSurfaceDrawContext::drawRect(const GrClip* clip,
706 GrPaint&& paint,
707 GrAA aa,
708 const SkMatrix& viewMatrix,
709 const SkRect& rect,
710 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700711 if (!style) {
712 style = &GrStyle::SimpleFill();
713 }
joshualitt1de610a2016-01-06 08:26:09 -0800714 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700715 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700716 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500717 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700718
bsalomon6663acf2016-05-10 09:14:17 -0700719 // Path effects should've been devolved to a path in SkGpuDevice
720 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700721
Robert Phillips72152832017-01-25 17:31:35 -0500722 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700723
bsalomon6663acf2016-05-10 09:14:17 -0700724 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400725 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000726 // Fills the rect, using rect as its own local coordinates
727 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500728 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400729 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
730 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
731 (rect.width() && rect.height())) {
732 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
733 // GrStyledShape to handle stroke caps and dashing properly.
Chris Dalton7d6748e2019-03-13 00:34:52 -0600734 GrAAType aaType = this->chooseAAType(aa);
Herb Derbyc76d4092020-10-07 16:46:15 -0400735 GrOp::Owner op = GrStrokeRectOp::Make(
736 fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500737 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
738 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500739 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400740 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700741 return;
robertphillips4bc31812016-03-01 12:22:49 -0800742 }
robertphillips4bc31812016-03-01 12:22:49 -0800743 }
Mike Klein16885072018-12-11 09:54:31 -0500744 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000745 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -0700746 GrStyledShape(rect, *style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -0700747}
748
Chris Dalton61d694d2021-03-22 00:00:52 -0600749void GrSurfaceDrawContext::fillRectToRect(const GrClip* clip,
750 GrPaint&& paint,
751 GrAA aa,
752 const SkMatrix& viewMatrix,
753 const SkRect& rectToDraw,
754 const SkRect& localRect) {
755 DrawQuad quad{GrQuad::MakeFromRect(rectToDraw, viewMatrix), GrQuad(localRect),
756 aa == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone};
757
758 // If we are using dmsaa then attempt to draw the rect with GrFillRRectOp.
759 if (fContext->priv().alwaysAntialias() && this->caps()->drawInstancedSupport() &&
760 aa == GrAA::kYes) { // If aa is kNo when using dmsaa, the rect is axis aligned. Don't use
761 // GrFillRRectOp because it might require dual source blending.
762 // http://skbug.com/11756
763 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr/*stencil*/, &aa, &quad,
764 &paint);
765 if (opt < QuadOptimization::kClipApplied) {
766 // The optimization was completely handled inside attempt().
767 return;
768 }
769
770 SkRect croppedRect, croppedLocal{};
771 const GrClip* optimizedClip = clip;
772 if (clip && viewMatrix.isScaleTranslate() && quad.fDevice.asRect(&croppedRect) &&
773 (!paint.usesVaryingCoords() || quad.fLocal.asRect(&croppedLocal))) {
774 // The cropped quad is still a rect, and our view matrix preserves rects. Map it back
775 // to pre-matrix space.
776 SkMatrix inverse;
777 if (!viewMatrix.invert(&inverse)) {
778 return;
779 }
780 SkASSERT(inverse.rectStaysRect());
781 inverse.mapRect(&croppedRect);
782 if (opt == QuadOptimization::kClipApplied) {
783 optimizedClip = nullptr;
784 }
785 } else {
786 // Even if attemptQuadOptimization gave us an optimized quad, GrFillRRectOp needs a rect
787 // in pre-matrix space, so use the original rect. Also preserve the original clip.
788 croppedRect = rectToDraw;
789 croppedLocal = localRect;
790 }
791
792 if (auto op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix,
793 SkRRect::MakeRect(croppedRect), croppedLocal,
794 GrAA::kYes)) {
795 this->addDrawOp(optimizedClip, std::move(op));
796 return;
797 }
798 }
799
800 assert_alive(paint);
801 this->drawFilledQuad(clip, std::move(paint), aa, &quad);
802}
803
Brian Salomoneebe7352020-12-09 16:37:04 -0500804void GrSurfaceDrawContext::drawQuadSet(const GrClip* clip,
805 GrPaint&& paint,
806 GrAA aa,
807 const SkMatrix& viewMatrix,
808 const QuadSetEntry quads[],
809 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600810 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500811
812 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
813 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500814}
815
Brian Salomoneebe7352020-12-09 16:37:04 -0500816int GrSurfaceDrawContext::maxWindowRectangles() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500817 return this->asRenderTargetProxy()->maxWindowRectangles(*this->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500818}
819
Brian Salomoneebe7352020-12-09 16:37:04 -0500820GrOpsTask::CanDiscardPreviousOps GrSurfaceDrawContext::canDiscardPreviousOpsOnFullClear() const {
Chris Dalton6b982802019-06-27 13:53:46 -0600821#if GR_TEST_UTILS
822 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400823 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600824 }
825#endif
826 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
827 // would normally be overwritten. The one exception is if the render target context is marked as
828 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
829 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
830 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
831 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400832 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600833}
834
Brian Salomoneebe7352020-12-09 16:37:04 -0500835void GrSurfaceDrawContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600836 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600837 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600838 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600839
Chris Daltoneffee202019-07-01 22:28:03 -0600840 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600841 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500842 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon590f5672020-12-16 11:44:47 -0500843 numRequiredSamples =
844 this->caps()->internalMultisampleCount(this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600845 }
846 SkASSERT(numRequiredSamples > 0);
847
848 if (numRequiredSamples > fNumStencilSamples) {
849 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500850 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600851 }
Chris Dalton6b982802019-06-27 13:53:46 -0600852
Chris Dalton674f77a2019-09-30 20:49:39 -0600853 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600854 if (this->caps()->performStencilClearsAsDraws()) {
855 // There is a driver bug with clearing stencil. We must use an op to manually clear the
856 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400857 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600858 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600859 this->getOpsTask()->setInitialStencilContent(
860 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600861 }
862 }
863}
864
Brian Salomoneebe7352020-12-09 16:37:04 -0500865void GrSurfaceDrawContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600866 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
867
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400868 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
869 if (scissor && !scissorState.set(*scissor)) {
870 // The requested clear region is off screen, so nothing to do.
871 return;
872 }
873
Michael Ludwig1e632792020-05-21 12:45:31 -0400874 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400875 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400876 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500877 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500878
879 // Configure the paint to have no impact on the color buffer
880 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400881 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400882 this->addDrawOp(nullptr,
883 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
884 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500885 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400886 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500887 }
robertphillips976f5f02016-06-03 10:59:20 -0700888}
889
Chris Dalton7d592cd2021-03-11 22:49:33 -0700890bool GrSurfaceDrawContext::stencilPath(const GrHardClip* clip,
891 GrAA doStencilMSAA,
892 const SkMatrix& viewMatrix,
893 const SkPath& path) {
894 SkIRect clipBounds = clip ? clip->getConservativeBounds()
895 : SkIRect::MakeSize(this->dimensions());
896 GrStyledShape shape(path, GrStyledShape::DoSimplify::kNo);
897
898 GrPathRenderer::CanDrawPathArgs canDrawArgs;
899 canDrawArgs.fCaps = fContext->priv().caps();
900 canDrawArgs.fProxy = this->asRenderTargetProxy();
901 canDrawArgs.fClipConservativeBounds = &clipBounds;
902 canDrawArgs.fViewMatrix = &viewMatrix;
903 canDrawArgs.fShape = &shape;
904 canDrawArgs.fPaint = nullptr;
905 canDrawArgs.fAAType = (doStencilMSAA == GrAA::kYes) ? GrAAType::kMSAA : GrAAType::kNone;
906 canDrawArgs.fHasUserStencilSettings = false;
907 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
908 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
909 canDrawArgs, false, GrPathRendererChain::DrawType::kStencil);
910 if (!pr) {
911 SkDebugf("WARNING: No path renderer to stencil path.\n");
912 return false;
913 }
914
915 GrPathRenderer::StencilPathArgs args;
916 args.fContext = fContext;
917 args.fRenderTargetContext = this;
918 args.fClip = clip;
919 args.fClipConservativeBounds = &clipBounds;
920 args.fViewMatrix = &viewMatrix;
921 args.fShape = &shape;
922 args.fDoStencilMSAA = doStencilMSAA;
923 pr->stencilPath(args);
924 return true;
925}
926
Brian Salomoneebe7352020-12-09 16:37:04 -0500927void GrSurfaceDrawContext::drawTextureSet(const GrClip* clip,
928 TextureSetEntry set[],
929 int cnt,
930 int proxyRunCnt,
931 GrSamplerState::Filter filter,
932 GrSamplerState::MipmapMode mm,
933 SkBlendMode mode,
934 GrAA aa,
935 SkCanvas::SrcRectConstraint constraint,
936 const SkMatrix& viewMatrix,
937 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400938 ASSERT_SINGLE_OWNER
939 RETURN_IF_ABANDONED
940 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500941 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500942
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500943 // Create the minimum number of GrTextureOps needed to draw this set. Individual
944 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
945 AutoCheckFlush acf(this->drawingManager());
946 GrAAType aaType = this->chooseAAType(aa);
947 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
948 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
949 : GrTextureOp::Saturate::kNo;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400950 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, mm, saturate,
Michael Ludwig379e4962019-12-06 13:21:26 -0500951 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400952}
953
Brian Salomoneebe7352020-12-09 16:37:04 -0500954void GrSurfaceDrawContext::drawVertices(const GrClip* clip,
955 GrPaint&& paint,
956 const SkMatrixProvider& matrixProvider,
957 sk_sp<SkVertices> vertices,
958 GrPrimitiveType* overridePrimType,
959 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -0500960 ASSERT_SINGLE_OWNER
961 RETURN_IF_ABANDONED
962 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500963 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500964
965 AutoCheckFlush acf(this->drawingManager());
966
967 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600968 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400969 GrOp::Owner op =
Brian Osman449b1152020-04-15 16:43:00 -0400970 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -0400971 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -0400972 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -0400973 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700974}
975
976///////////////////////////////////////////////////////////////////////////////
977
Brian Salomoneebe7352020-12-09 16:37:04 -0500978void GrSurfaceDrawContext::drawAtlas(const GrClip* clip,
979 GrPaint&& paint,
980 const SkMatrix& viewMatrix,
981 int spriteCount,
982 const SkRSXform xform[],
983 const SkRect texRect[],
984 const SkColor colors[]) {
Brian Osman4d92b892019-03-24 00:53:23 +0000985 ASSERT_SINGLE_OWNER
986 RETURN_IF_ABANDONED
987 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500988 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAtlas", fContext);
Brian Osman4d92b892019-03-24 00:53:23 +0000989
990 AutoCheckFlush acf(this->drawingManager());
991
992 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -0400993 GrOp::Owner op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
994 aaType, spriteCount, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +0000995 this->addDrawOp(clip, std::move(op));
996}
997
998///////////////////////////////////////////////////////////////////////////////
999
Brian Salomoneebe7352020-12-09 16:37:04 -05001000void GrSurfaceDrawContext::drawRRect(const GrClip* origClip,
1001 GrPaint&& paint,
1002 GrAA aa,
1003 const SkMatrix& viewMatrix,
1004 const SkRRect& rrect,
1005 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001006 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001007 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001008 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001009 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001010
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001011 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1012
Robert Phillips85290802018-07-02 13:14:28 -04001013 const SkStrokeRec& stroke = style.strokeRec();
1014 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001015 return;
1016 }
1017
Michael Ludwig7c12e282020-05-29 09:54:07 -04001018 const GrClip* clip = origClip;
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001019 // It is not uncommon to clip to a round rect and then draw that same round rect. Since our
1020 // lower level clip code works from op bounds, which are SkRects, it doesn't detect that the
1021 // clip can be ignored. The following test attempts to mitigate the stencil clip cost but only
1022 // works for axis-aligned round rects. This also only works for filled rrects since the stroke
1023 // width outsets beyond the rrect itself.
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001024 // TODO: skbug.com/10462 - There was mixed performance wins and regressions when this
1025 // optimization was turned on outside of Android Framework. I (michaelludwig) believe this is
1026 // do to the overhead in determining if an SkClipStack is just a rrect. Once that is improved,
1027 // re-enable this and see if we avoid the regressions.
1028#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
bsalomon7f0d9f32016-08-15 14:49:10 -07001029 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001030 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001031 rrect.transform(viewMatrix, &devRRect)) {
Michael Ludwig6397e802020-08-05 15:50:01 -04001032 GrClip::PreClipResult result = clip->preApply(devRRect.getBounds(), aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001033 switch(result.fEffect) {
1034 case GrClip::Effect::kClippedOut:
1035 return;
1036 case GrClip::Effect::kUnclipped:
1037 clip = nullptr;
1038 break;
1039 case GrClip::Effect::kClipped:
1040 // Currently there's no general-purpose rrect-to-rrect contains function, and if we
1041 // got here, we know the devRRect's bounds aren't fully contained by the clip.
1042 // Testing for equality between the two is a reasonable stop-gap for now.
1043 if (result.fIsRRect && result.fRRect == devRRect) {
1044 // NOTE: On the android framework, we allow this optimization even when the clip
1045 // is non-AA and the draw is AA.
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001046 if (result.fAA == aa || (result.fAA == GrAA::kNo && aa == GrAA::kYes)) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001047 clip = nullptr;
1048 }
1049 }
1050 break;
1051 default:
1052 SkUNREACHABLE;
1053 }
1054 }
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001055#endif
ksakamotoec7f2ac2016-07-05 03:54:53 -07001056
Robert Phillips72152832017-01-25 17:31:35 -05001057 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001058
Chris Dalton7d6748e2019-03-13 00:34:52 -06001059 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001060
Herb Derbyc76d4092020-10-07 16:46:15 -04001061 GrOp::Owner op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001062 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1063 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1064 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1065 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1066 // to avoid perf regressions on some platforms.
1067 assert_alive(paint);
1068 op = GrOvalOpFactory::MakeCircularRRectOp(
1069 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1070 }
1071 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001072 assert_alive(paint);
Chris Dalton61d694d2021-03-22 00:00:52 -06001073 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, rrect.rect(),
Chris Dalton4f447342021-03-12 12:09:12 -07001074 GrAA(aaType != GrAAType::kNone));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001075 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001076 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001077 assert_alive(paint);
1078 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001079 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001080 }
1081 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001082 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001083 return;
robertphillipsea461502015-05-26 11:38:03 -07001084 }
robertphillipsb56f9272016-02-25 11:03:52 -08001085
Mike Klein16885072018-12-11 09:54:31 -05001086 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001087 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001088 GrStyledShape(rrect, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001089}
1090
Jim Van Verthc5903412016-11-17 15:27:09 -05001091///////////////////////////////////////////////////////////////////////////////
1092
Brian Salomoneebe7352020-12-09 16:37:04 -05001093bool GrSurfaceDrawContext::drawFastShadow(const GrClip* clip,
1094 const SkMatrix& viewMatrix,
1095 const SkPath& path,
1096 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001097 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001098 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001099 return true;
1100 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001101 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001102 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001103
1104 // check z plane
1105 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1106 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1107 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1108 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1109 return false;
1110 }
1111
1112 SkRRect rrect;
1113 SkRect rect;
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001114 // we can only handle rects, circles, and simple rrects with circular corners
1115 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsNearlySimpleCircular(rrect) &&
1116 rrect.getSimpleRadii().fX > SK_ScalarNearlyZero;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001117 if (!isRRect &&
1118 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1119 rect.width() > SK_ScalarNearlyZero) {
1120 rrect.setOval(rect);
1121 isRRect = true;
1122 }
1123 if (!isRRect && path.isRect(&rect)) {
1124 rrect.setRect(rect);
1125 isRRect = true;
1126 }
1127
1128 if (!isRRect) {
1129 return false;
1130 }
1131
Jim Van Verthc5903412016-11-17 15:27:09 -05001132 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001134 }
1135
Robert Phillips72152832017-01-25 17:31:35 -05001136 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001137
Jim Van Verth63f03542020-12-16 11:56:11 -05001138 SkPoint3 devLightPos = rec.fLightPos;
1139 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
1140 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -05001141 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -05001142 } else {
1143 // transform light
1144 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
1145 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001146
1147 // 1/scale
1148 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001149 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001150 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1151 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1152
1153 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001154 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1155
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001156 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001157 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1158 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1159 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001160
1161 // Outset the shadow rrect to the border of the penumbra
1162 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1163 SkRRect ambientRRect;
1164 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1165 // If the rrect was an oval then its outset will also be one.
1166 // We set it explicitly to avoid errors.
1167 if (rrect.isOval()) {
1168 ambientRRect = SkRRect::MakeOval(outsetRect);
1169 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001170 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001171 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1172 }
1173
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001174 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001175 if (transparent) {
1176 // set a large inset to force a fill
1177 devSpaceInsetWidth = ambientRRect.width();
1178 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001179
Herb Derbyc76d4092020-10-07 16:46:15 -04001180 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1181 ambientColor,
1182 viewMatrix,
1183 ambientRRect,
1184 devSpaceAmbientBlur,
1185 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001186 if (op) {
1187 this->addDrawOp(clip, std::move(op));
1188 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001189 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001190
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001191 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001192 SkScalar devSpaceSpotBlur;
1193 SkScalar spotScale;
1194 SkVector spotOffset;
Jim Van Verth63f03542020-12-16 11:56:11 -05001195 if (directional) {
1196 SkDrawShadowMetrics::GetDirectionalParams(occluderHeight, devLightPos.fX,
1197 devLightPos.fY, devLightPos.fZ,
1198 rec.fLightRadius, &devSpaceSpotBlur,
1199 &spotScale, &spotOffset);
1200 } else {
1201 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1202 devLightPos.fZ, rec.fLightRadius,
1203 &devSpaceSpotBlur, &spotScale, &spotOffset);
1204 }
Jim Van Verth1af03d42017-07-31 09:34:58 -04001205 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001206 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1207
Jim Van Verth3af1af92017-05-18 15:06:54 -04001208 // Adjust translate for the effect of the scale.
1209 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1210 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1211 // This offset is in dev space, need to transform it into source space.
1212 SkMatrix ctmInverse;
1213 if (viewMatrix.invert(&ctmInverse)) {
1214 ctmInverse.mapPoints(&spotOffset, 1);
1215 } else {
1216 // Since the matrix is a similarity, this should never happen, but just in case...
1217 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1218 SkASSERT(false);
1219 }
1220
1221 // Compute the transformed shadow rrect
1222 SkRRect spotShadowRRect;
1223 SkMatrix shadowTransform;
1224 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1225 rrect.transform(shadowTransform, &spotShadowRRect);
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001226 SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001227
1228 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001229 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001230 SkScalar insetWidth = blurOutset;
1231 if (transparent) {
1232 // If transparent, just do a fill
1233 insetWidth += spotShadowRRect.width();
1234 } else {
1235 // For shadows, instead of using a stroke we specify an inset from the penumbra
1236 // border. We want to extend this inset area so that it meets up with the caster
1237 // geometry. The inset geometry will by default already be inset by the blur width.
1238 //
1239 // We compare the min and max corners inset by the radius between the original
1240 // rrect and the shadow rrect. The distance between the two plus the difference
1241 // between the scaled radius and the original radius gives the distance from the
1242 // transformed shadow shape to the original shape in that corner. The max
1243 // of these gives the maximum distance we need to cover.
1244 //
1245 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1246 // that to get the full insetWidth.
1247 SkScalar maxOffset;
1248 if (rrect.isRect()) {
1249 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001250 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001251 rrect.rect().fLeft),
1252 SkTAbs(spotShadowRRect.rect().fTop -
1253 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001254 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255 rrect.rect().fRight),
1256 SkTAbs(spotShadowRRect.rect().fBottom -
1257 rrect.rect().fBottom)));
1258 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001259 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001260 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1261 rrect.rect().fLeft + dr,
1262 spotShadowRRect.rect().fTop -
1263 rrect.rect().fTop + dr);
1264 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1265 rrect.rect().fRight - dr,
1266 spotShadowRRect.rect().fBottom -
1267 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001268 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Jim Van Verthe82fdaa2021-03-10 10:36:55 -05001269 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001270 }
Brian Osman788b9162020-02-07 10:36:46 -05001271 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001272 }
1273
1274 // Outset the shadow rrect to the border of the penumbra
1275 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1276 if (spotShadowRRect.isOval()) {
1277 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1278 } else {
1279 SkScalar outsetRad = spotRadius + blurOutset;
1280 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1281 }
1282
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001283 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001284
Herb Derbyc76d4092020-10-07 16:46:15 -04001285 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1286 spotColor,
1287 viewMatrix,
1288 spotShadowRRect,
Brian Salomoneebe7352020-12-09 16:37:04 -05001289 2.0f * devSpaceSpotBlur,
Herb Derbyc76d4092020-10-07 16:46:15 -04001290 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001291 if (op) {
1292 this->addDrawOp(clip, std::move(op));
1293 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001294 }
1295
1296 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001297}
1298
1299///////////////////////////////////////////////////////////////////////////////
1300
Brian Salomoneebe7352020-12-09 16:37:04 -05001301void GrSurfaceDrawContext::drawRegion(const GrClip* clip,
1302 GrPaint&& paint,
1303 GrAA aa,
1304 const SkMatrix& viewMatrix,
1305 const SkRegion& region,
1306 const GrStyle& style,
1307 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001308 ASSERT_SINGLE_OWNER
1309 RETURN_IF_ABANDONED
1310 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001311 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001312
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001313 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001314 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001315 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001316 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001317 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1318 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001319 aa = GrAA::kNo;
1320 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001321 }
msarettcc319b92016-08-25 18:07:18 -07001322 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001323 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001324 SkPath path;
1325 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001326 path.setIsVolatile(true);
1327
Brian Salomon82f44312017-01-11 13:42:54 -05001328 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001329 }
1330
Chris Dalton7d6748e2019-03-13 00:34:52 -06001331 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001332 GrOp::Owner op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1333 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001334 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001335}
1336
Brian Salomoneebe7352020-12-09 16:37:04 -05001337void GrSurfaceDrawContext::drawOval(const GrClip* clip,
1338 GrPaint&& paint,
1339 GrAA aa,
1340 const SkMatrix& viewMatrix,
1341 const SkRect& oval,
1342 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001343 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001344 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001345 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001346 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001347
Robert Phillips7484d202018-07-03 09:09:08 -04001348 const SkStrokeRec& stroke = style.strokeRec();
1349
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001350 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001351 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1352 return;
1353 }
1354
1355 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001356 return;
robertphillipsea461502015-05-26 11:38:03 -07001357 }
1358
Robert Phillips72152832017-01-25 17:31:35 -05001359 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001360
Chris Dalton7d6748e2019-03-13 00:34:52 -06001361 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001362
Herb Derbyc76d4092020-10-07 16:46:15 -04001363 GrOp::Owner op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001364 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1365 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001366 // We don't draw true circles as round rects in coverage mode, because it can
1367 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1368 assert_alive(paint);
1369 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1370 this->caps()->shaderCaps());
1371 }
1372 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001373 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1374 // the arc equation. This same special geometry and fragment branch also turn out to be a
1375 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1376 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1377 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001378 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001379 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
Chris Dalton61d694d2021-03-22 00:00:52 -06001380 oval, GrAA(aaType != GrAAType::kNone));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001381 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001382 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001383 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001384 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1385 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001386 }
1387 if (op) {
1388 this->addDrawOp(clip, std::move(op));
1389 return;
robertphillipsea461502015-05-26 11:38:03 -07001390 }
robertphillipsb56f9272016-02-25 11:03:52 -08001391
Mike Klein16885072018-12-11 09:54:31 -05001392 assert_alive(paint);
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001393 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1394 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001395 false, style, DoSimplify::kNo));
robertphillipsea461502015-05-26 11:38:03 -07001396}
1397
Brian Salomoneebe7352020-12-09 16:37:04 -05001398void GrSurfaceDrawContext::drawArc(const GrClip* clip,
1399 GrPaint&& paint,
1400 GrAA aa,
1401 const SkMatrix& viewMatrix,
1402 const SkRect& oval,
1403 SkScalar startAngle,
1404 SkScalar sweepAngle,
1405 bool useCenter,
1406 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001407 ASSERT_SINGLE_OWNER
1408 RETURN_IF_ABANDONED
1409 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001410 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001411
1412 AutoCheckFlush acf(this->drawingManager());
1413
Chris Dalton7d6748e2019-03-13 00:34:52 -06001414 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001415 if (GrAAType::kCoverage == aaType) {
1416 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Herb Derbyc76d4092020-10-07 16:46:15 -04001417 GrOp::Owner op = GrOvalOpFactory::MakeArcOp(fContext,
1418 std::move(paint),
1419 viewMatrix,
1420 oval,
1421 startAngle,
1422 sweepAngle,
1423 useCenter,
1424 style,
1425 shaderCaps);
Greg Daniel2655ede2019-04-10 00:49:28 +00001426 if (op) {
1427 this->addDrawOp(clip, std::move(op));
1428 return;
1429 }
1430 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001431 }
Chris Daltonb8ed2bc2021-02-02 11:53:19 -07001432 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
1433 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter,
Chris Daltonfbc3a832021-02-15 23:16:17 -07001434 style, DoSimplify::kNo));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001435}
1436
Brian Salomoneebe7352020-12-09 16:37:04 -05001437void GrSurfaceDrawContext::drawImageLattice(const GrClip* clip,
1438 GrPaint&& paint,
1439 const SkMatrix& viewMatrix,
1440 GrSurfaceProxyView view,
1441 SkAlphaType alphaType,
1442 sk_sp<GrColorSpaceXform> csxf,
1443 GrSamplerState::Filter filter,
1444 std::unique_ptr<SkLatticeIter> iter,
1445 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001446 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001447 RETURN_IF_ABANDONED
1448 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001449 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001450
Robert Phillips72152832017-01-25 17:31:35 -05001451 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001452
Herb Derbyc76d4092020-10-07 16:46:15 -04001453 GrOp::Owner op =
Greg Danieled96bca2019-12-05 15:05:54 -05001454 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001455 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001456 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001457}
1458
Brian Salomoneebe7352020-12-09 16:37:04 -05001459void GrSurfaceDrawContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001460 const SkRect& bounds) {
Herb Derbyc76d4092020-10-07 16:46:15 -04001461 GrOp::Owner op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001462 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001463 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001464}
1465
Brian Salomoneebe7352020-12-09 16:37:04 -05001466bool GrSurfaceDrawContext::waitOnSemaphores(int numSemaphores,
1467 const GrBackendSemaphore waitSemaphores[],
1468 bool deleteSemaphoresAfterWait) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001469 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001470 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001471 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001472 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001473
1474 AutoCheckFlush acf(this->drawingManager());
1475
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001476 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001477 return false;
1478 }
1479
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001480 auto direct = fContext->asDirectContext();
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001481 if (!direct) {
1482 return false;
1483 }
1484
1485 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001486
Greg Daniel414418d2020-07-08 11:44:25 -04001487 GrWrapOwnership ownership =
1488 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
1489
Greg Daniel301015c2019-11-18 14:06:46 -05001490 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1491 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001492 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001493 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -04001494 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001495 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001496 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1497 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001498 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001499}
joshualitt33a5fce2015-11-18 13:28:51 -08001500
Brian Salomoneebe7352020-12-09 16:37:04 -05001501void GrSurfaceDrawContext::drawPath(const GrClip* clip,
1502 GrPaint&& paint,
1503 GrAA aa,
1504 const SkMatrix& viewMatrix,
1505 const SkPath& path,
1506 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001507 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001508 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001509 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001510 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawPath", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001511
Chris Daltonfbc3a832021-02-15 23:16:17 -07001512 GrStyledShape shape(path, style, DoSimplify::kNo);
Chris Dalton21859012021-01-29 23:44:53 -07001513 this->drawShape(clip, std::move(paint), aa, viewMatrix, std::move(shape));
Robert Phillips20390c32018-08-17 11:01:03 -04001514}
1515
Brian Salomoneebe7352020-12-09 16:37:04 -05001516void GrSurfaceDrawContext::drawShape(const GrClip* clip,
1517 GrPaint&& paint,
1518 GrAA aa,
1519 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001520 GrStyledShape&& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04001521 ASSERT_SINGLE_OWNER
1522 RETURN_IF_ABANDONED
1523 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001524 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawShape", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001525
Brian Salomon2fad74a2017-12-20 13:28:55 -05001526 if (shape.isEmpty()) {
1527 if (shape.inverseFilled()) {
1528 this->drawPaint(clip, std::move(paint), viewMatrix);
1529 }
1530 return;
robertphillipsea461502015-05-26 11:38:03 -07001531 }
1532
Robert Phillips72152832017-01-25 17:31:35 -05001533 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001534
Michael Ludwig6a6de652020-04-30 20:16:36 -04001535 // If we get here in drawShape(), we definitely need to use path rendering
Chris Dalton21859012021-01-29 23:44:53 -07001536 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, std::move(shape),
Chris Dalton9f704a82021-02-04 15:07:56 -07001537 /* attemptDrawSimple */ true);
robertphillipsea461502015-05-26 11:38:03 -07001538}
1539
Brian Salomoneebe7352020-12-09 16:37:04 -05001540static SkIRect get_clip_bounds(const GrSurfaceDrawContext* rtc, const GrClip* clip) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001541 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
1542}
1543
Brian Salomoneebe7352020-12-09 16:37:04 -05001544bool GrSurfaceDrawContext::drawAndStencilPath(const GrHardClip* clip,
1545 const GrUserStencilSettings* ss,
1546 SkRegion::Op op,
1547 bool invert,
1548 GrAA aa,
1549 const SkMatrix& viewMatrix,
1550 const SkPath& path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001551 ASSERT_SINGLE_OWNER
1552 RETURN_FALSE_IF_ABANDONED
1553 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001554 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAndStencilPath", fContext);
robertphillips391395d2016-03-02 09:26:36 -08001555
1556 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001557 GrPaint paint;
1558 paint.setCoverageSetOpXPFactory(op, invert);
1559 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon70fe17e2020-11-30 14:33:58 -05001560 SkRect::Make(this->dimensions()));
robertphillips391395d2016-03-02 09:26:36 -08001561 return true;
1562 }
1563
Brian Salomon70fe17e2020-11-30 14:33:58 -05001564 AutoCheckFlush acf(this->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001565
1566 // An Assumption here is that path renderer would use some form of tweaking
1567 // the src color (either the input alpha or in the frag shader) to implement
1568 // aa. If we have some future driver-mojo path AA that can do the right
1569 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon70fe17e2020-11-30 14:33:58 -05001570 GrAAType aaType = this->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001571 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001572
Brian Salomon70fe17e2020-11-30 14:33:58 -05001573 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001574
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001575 GrPaint paint;
1576 paint.setCoverageSetOpXPFactory(op, invert);
1577
Michael Ludwig2686d692020-04-17 20:21:37 +00001578 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001579 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001580 canDrawArgs.fCaps = this->caps();
1581 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08001582 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001583 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001584 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001585 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001586 canDrawArgs.fAAType = aaType;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001587 SkASSERT(!this->wrapsVkSecondaryCB());
Greg Danielbe7fc462019-01-03 16:40:42 -05001588 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001589 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001590
1591 // Don't allow the SW renderer
Brian Salomon70fe17e2020-11-30 14:33:58 -05001592 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001593 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001594 if (!pr) {
1595 return false;
1596 }
1597
Brian Salomon70fe17e2020-11-30 14:33:58 -05001598 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001599 std::move(paint),
1600 ss,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001601 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001602 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04001603 &clipConservativeBounds,
1604 &viewMatrix,
1605 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001606 aaType,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001607 this->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001608 pr->drawPath(args);
1609 return true;
1610}
1611
Brian Salomoneebe7352020-12-09 16:37:04 -05001612SkBudgeted GrSurfaceDrawContext::isBudgeted() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001613 ASSERT_SINGLE_OWNER
robertphillips714712b2016-08-04 06:20:45 -07001614
Brian Salomon70fe17e2020-11-30 14:33:58 -05001615 if (fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001616 return SkBudgeted::kNo;
1617 }
1618
Brian Salomon70fe17e2020-11-30 14:33:58 -05001619 SkDEBUGCODE(this->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001620
Brian Salomon70fe17e2020-11-30 14:33:58 -05001621 return this->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001622}
1623
Chris Dalton319d0202021-01-29 23:49:47 -07001624void GrSurfaceDrawContext::drawStrokedLine(const GrClip* clip, GrPaint&& paint,
1625 GrAA aa, const SkMatrix& viewMatrix,
1626 const SkPoint points[2], const SkStrokeRec& stroke) {
1627 ASSERT_SINGLE_OWNER
1628
1629 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style);
1630 SkASSERT(stroke.getWidth() > 0);
1631 // Adding support for round capping would require a
1632 // GrSurfaceDrawContext::fillRRectWithLocalMatrix entry point
1633 SkASSERT(SkPaint::kRound_Cap != stroke.getCap());
1634
1635 const SkScalar halfWidth = 0.5f * stroke.getWidth();
1636 if (halfWidth <= 0.f) {
1637 // Prevents underflow when stroke width is epsilon > 0 (so technically not a hairline).
1638 // The CTM would need to have a scale near 1/epsilon in order for this to have meaningful
1639 // coverage (although that would likely overflow elsewhere and cause the draw to drop due
1640 // to non-finite bounds). At any other scale, this line is so thin, it's coverage is
1641 // negligible, so discarding the draw is visually equivalent.
1642 return;
1643 }
1644
1645 SkVector parallel = points[1] - points[0];
1646
1647 if (!SkPoint::Normalize(&parallel)) {
1648 parallel.fX = 1.0f;
1649 parallel.fY = 0.0f;
1650 }
1651 parallel *= halfWidth;
1652
1653 SkVector ortho = { parallel.fY, -parallel.fX };
1654 if (SkPaint::kButt_Cap == stroke.getCap()) {
1655 // No extra extension for butt caps
1656 parallel = {0.f, 0.f};
1657 }
1658 // Order is TL, TR, BR, BL where arbitrarily "down" is p0 to p1 and "right" is positive
1659 SkPoint corners[4] = { points[0] - ortho - parallel,
1660 points[0] + ortho - parallel,
1661 points[1] + ortho + parallel,
1662 points[1] - ortho + parallel };
1663
1664 GrQuadAAFlags edgeAA = (aa == GrAA::kYes) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
1665 this->fillQuadWithEdgeAA(clip, std::move(paint), aa, edgeAA, viewMatrix, corners, nullptr);
1666}
1667
Chris Daltonbfe0d372021-02-04 12:24:54 -07001668bool GrSurfaceDrawContext::drawSimpleShape(const GrClip* clip, GrPaint* paint, GrAA aa,
1669 const SkMatrix& viewMatrix, const GrStyledShape& shape) {
1670 if (!shape.style().hasPathEffect()) {
1671 GrAAType aaType = this->chooseAAType(aa);
1672 SkPoint linePts[2];
1673 SkRRect rrect;
1674 // We can ignore the starting point and direction since there is no path effect.
1675 bool inverted;
1676 if (shape.asLine(linePts, &inverted) && !inverted &&
1677 shape.style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style &&
1678 shape.style().strokeRec().getCap() != SkPaint::kRound_Cap) {
1679 // The stroked line is an oriented rectangle, which looks the same or better (if
1680 // perspective) compared to path rendering. The exception is subpixel/hairline lines
1681 // that are non-AA or MSAA, in which case the default path renderer achieves higher
1682 // quality.
1683 // FIXME(michaelludwig): If the fill rect op could take an external coverage, or checks
1684 // for and outsets thin non-aa rects to 1px, the path renderer could be skipped.
1685 SkScalar coverage;
1686 if (aaType == GrAAType::kCoverage ||
1687 !SkDrawTreatAAStrokeAsHairline(shape.style().strokeRec().getWidth(), viewMatrix,
1688 &coverage)) {
1689 this->drawStrokedLine(clip, std::move(*paint), aa, viewMatrix, linePts,
1690 shape.style().strokeRec());
1691 return true;
1692 }
1693 } else if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1694 if (rrect.isRect()) {
1695 this->drawRect(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1696 &shape.style());
1697 return true;
1698 } else if (rrect.isOval()) {
1699 this->drawOval(clip, std::move(*paint), aa, viewMatrix, rrect.rect(),
1700 shape.style());
1701 return true;
1702 }
1703 this->drawRRect(clip, std::move(*paint), aa, viewMatrix, rrect, shape.style());
1704 return true;
1705 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1706 viewMatrix.rectStaysRect()) {
1707 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply the
1708 // matrix to all the points individually rather than just to the rect
1709 SkRect rects[2];
1710 if (shape.asNestedRects(rects)) {
1711 // Concave AA paths are expensive - try to avoid them for special cases
1712 GrOp::Owner op = GrStrokeRectOp::MakeNested(
1713 fContext, std::move(*paint), viewMatrix, rects);
1714 if (op) {
1715 this->addDrawOp(clip, std::move(op));
1716 }
1717 // Returning here indicates that there is nothing to draw in this case.
1718 return true;
1719 }
1720 }
1721 }
1722 return false;
1723}
1724
Brian Salomoneebe7352020-12-09 16:37:04 -05001725void GrSurfaceDrawContext::drawShapeUsingPathRenderer(const GrClip* clip,
1726 GrPaint&& paint,
1727 GrAA aa,
1728 const SkMatrix& viewMatrix,
Chris Dalton21859012021-01-29 23:44:53 -07001729 GrStyledShape&& shape,
Chris Daltonbfe0d372021-02-04 12:24:54 -07001730 bool attemptDrawSimple) {
joshualitt1de610a2016-01-06 08:26:09 -08001731 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001732 RETURN_IF_ABANDONED
Brian Salomoneebe7352020-12-09 16:37:04 -05001733 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "internalDrawPath", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001734
Chris Dalton21859012021-01-29 23:44:53 -07001735 if (!viewMatrix.isFinite() || !shape.bounds().isFinite()) {
Jim Van Verthf86073a2018-10-02 13:05:38 -04001736 return;
1737 }
1738
Michael Ludwig7c12e282020-05-29 09:54:07 -04001739 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001740
Chris Dalton6ce447a2019-06-23 18:07:38 -06001741 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06001742
robertphillips68737822015-10-29 12:12:21 -07001743 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001744 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05001745 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07001746 canDrawArgs.fViewMatrix = &viewMatrix;
Chris Dalton21859012021-01-29 23:44:53 -07001747 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001748 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001749 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001750 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001751 canDrawArgs.fHasUserStencilSettings = false;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001752 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001753
Chris Daltone6ae4762021-02-05 14:56:21 -07001754 constexpr static bool kDisallowSWPathRenderer = false;
1755 constexpr static bool kAllowSWPathRenderer = true;
1756 using DrawType = GrPathRendererChain::DrawType;
1757
1758 GrPathRenderer* pr = nullptr;
1759
1760 if (!shape.style().strokeRec().isFillStyle() && !shape.isEmpty()) {
1761 // Give the tessellation path renderer a chance to claim this stroke before we simplify it.
1762 GrPathRenderer* tess = this->drawingManager()->getTessellationPathRenderer();
1763 if (tess && tess->canDrawPath(canDrawArgs) == GrPathRenderer::CanDrawPath::kYes) {
1764 pr = tess;
1765 }
1766 }
1767
1768 if (!pr) {
1769 // The shape isn't a stroke that can be drawn directly. Simplify if possible.
Chris Daltonfbc3a832021-02-15 23:16:17 -07001770 shape.simplify();
Chris Daltone6ae4762021-02-05 14:56:21 -07001771
1772 if (shape.isEmpty() && !shape.inverseFilled()) {
1773 return;
1774 }
1775
1776 if (attemptDrawSimple || shape.simplified()) {
1777 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too complex
1778 // for dedicated draw ops. However, if GrStyledShape was able to reduce something we
1779 // ought to try again instead of going right to path rendering.
1780 if (this->drawSimpleShape(clip, &paint, aa, viewMatrix, shape)) {
1781 return;
1782 }
1783 }
1784
1785 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1786 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1787 DrawType::kColor);
1788 }
1789
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001790 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -05001791 if (styleScale == 0.0f) {
1792 return;
1793 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001794
Chris Dalton21859012021-01-29 23:44:53 -07001795 if (!pr && shape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001796 // It didn't work above, so try again with the path effect applied.
Chris Dalton21859012021-01-29 23:44:53 -07001797 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1798 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001799 return;
1800 }
Chris Daltone6ae4762021-02-05 14:56:21 -07001801 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kDisallowSWPathRenderer,
1802 DrawType::kColor);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001803 }
1804 if (!pr) {
Chris Dalton21859012021-01-29 23:44:53 -07001805 if (shape.style().applies()) {
1806 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1807 if (shape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001808 return;
1809 }
Brian Salomone7df0bb2018-05-07 14:44:57 -04001810 // This time, allow SW renderer
Chris Daltone6ae4762021-02-05 14:56:21 -07001811 pr = this->drawingManager()->getPathRenderer(canDrawArgs, kAllowSWPathRenderer,
1812 DrawType::kColor);
Brian Salomone7df0bb2018-05-07 14:44:57 -04001813 } else {
1814 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04001815#if GR_PATH_RENDERER_SPEW
1816 SkDebugf("falling back to: %s\n", pr->name());
1817#endif
bsalomon6663acf2016-05-10 09:14:17 -07001818 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001819 }
robertphillipsea461502015-05-26 11:38:03 -07001820
bsalomon8acedde2016-06-24 10:42:16 -07001821 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001822#ifdef SK_DEBUG
1823 SkDebugf("Unable to find path renderer compatible with path.\n");
1824#endif
1825 return;
1826 }
1827
Robert Phillips256c37b2017-03-01 14:32:46 -05001828 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001829 std::move(paint),
1830 &GrUserStencilSettings::kUnused,
1831 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001832 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001833 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001834 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001835 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001836 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001837 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001838 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001839}
1840
Brian Salomon467921e2017-03-06 16:17:12 -05001841static void op_bounds(SkRect* bounds, const GrOp* op) {
1842 *bounds = op->bounds();
1843 if (op->hasZeroArea()) {
1844 if (op->hasAABloat()) {
1845 bounds->outset(0.5f, 0.5f);
1846 } else {
1847 // We don't know which way the particular GPU will snap lines or points at integer
1848 // coords. So we ensure that the bounds is large enough for either snap.
1849 SkRect before = *bounds;
1850 bounds->roundOut(bounds);
1851 if (bounds->fLeft == before.fLeft) {
1852 bounds->fLeft -= 1;
1853 }
1854 if (bounds->fTop == before.fTop) {
1855 bounds->fTop -= 1;
1856 }
1857 if (bounds->fRight == before.fRight) {
1858 bounds->fRight += 1;
1859 }
1860 if (bounds->fBottom == before.fBottom) {
1861 bounds->fBottom += 1;
1862 }
1863 }
1864 }
1865}
1866
Brian Salomoneebe7352020-12-09 16:37:04 -05001867void GrSurfaceDrawContext::addDrawOp(const GrClip* clip,
1868 GrOp::Owner op,
1869 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001870 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001871 if (fContext->abandoned()) {
Brian Salomon348a0372018-10-31 10:42:18 -04001872 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001873 }
Herb Derbyc76d4092020-10-07 16:46:15 -04001874 GrDrawOp* drawOp = (GrDrawOp*)op.get();
robertphillips2e1e51f2015-10-15 08:01:48 -07001875 SkDEBUGCODE(this->validate();)
Herb Derbyc76d4092020-10-07 16:46:15 -04001876 SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;)
Brian Salomoneebe7352020-12-09 16:37:04 -05001877 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001878
Brian Salomon467921e2017-03-06 16:17:12 -05001879 // Setup clip
1880 SkRect bounds;
1881 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001882 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Herb Derbyc76d4092020-10-07 16:46:15 -04001883 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = drawOp->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06001884 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07001885 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06001886
Chris Dalton9acf6822019-11-12 11:52:40 -07001887 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06001888 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06001889 }
1890
Michael Ludwig7c12e282020-05-29 09:54:07 -04001891 bool skipDraw = false;
1892 if (clip) {
1893 // Have a complex clip, so defer to its early clip culling
Brian Salomon590f5672020-12-16 11:44:47 -05001894 GrAAType aaType;
1895 if (usesHWAA) {
1896 aaType = GrAAType::kMSAA;
1897 } else {
1898 aaType = op->hasAABloat() ? GrAAType::kCoverage : GrAAType::kNone;
1899 }
Michael Ludwig6397e802020-08-05 15:50:01 -04001900 skipDraw = clip->apply(fContext, this, aaType, usesUserStencilBits,
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001901 &appliedClip, &bounds) == GrClip::Effect::kClippedOut;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001902 } else {
1903 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001904 skipDraw = !bounds.intersect(this->asSurfaceProxy()->getBoundsRect());
Michael Ludwig7c12e282020-05-29 09:54:07 -04001905 }
1906
1907 if (skipDraw) {
Brian Salomon348a0372018-10-31 10:42:18 -04001908 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001909 }
1910
Chris Dalton9acf6822019-11-12 11:52:40 -07001911 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
1912 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
1913
1914 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
1915 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
1916 // that uses a stencil test when the stencil buffer is multisampled.)
1917 bool hasMixedSampledCoverage = (
1918 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05001919 SkASSERT(!hasMixedSampledCoverage ||
1920 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04001921
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001922 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Herb Derbyc76d4092020-10-07 16:46:15 -04001923 GrProcessorSet::Analysis analysis = drawOp->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06001924 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
1925
Michael Ludwig28e5f112020-06-09 10:57:24 -04001926 // Must be called before setDstProxyView so that it sees the final bounds of the op.
1927 op->setClippedBounds(bounds);
1928
Greg Daniel524e28b2019-11-01 11:48:53 -04001929 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07001930 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04001931 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Brian Salomon348a0372018-10-31 10:42:18 -04001932 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001933 }
1934 }
1935
Greg Danielf41b2bd2019-08-22 16:19:24 -04001936 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04001937 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04001938 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04001939 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04001940 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -04001941 dstProxyView, GrTextureResolveManager(this->drawingManager()),
1942 *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04001943}
1944
Brian Salomoneebe7352020-12-09 16:37:04 -05001945bool GrSurfaceDrawContext::setupDstProxyView(const GrOp& op,
1946 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001947 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
1948 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
1949 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05001950 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001951 return false;
1952 }
1953
Greg Danield358cbe2020-09-11 09:33:54 -04001954 if (fDstSampleType == GrDstSampleType::kNone) {
1955 fDstSampleType = this->caps()->getDstSampleTypeForProxy(this->asRenderTargetProxy());
Brian Salomon467921e2017-03-06 16:17:12 -05001956 }
Greg Danield358cbe2020-09-11 09:33:54 -04001957 SkASSERT(fDstSampleType != GrDstSampleType::kNone);
1958
1959 if (GrDstSampleTypeDirectlySamplesDst(fDstSampleType)) {
1960 // The render target is a texture or input attachment, so we can read from it directly in
1961 // the shader. The XP will be responsible to detect this situation and request a texture
1962 // barrier.
1963 dstProxyView->setProxyView(this->readSurfaceView());
1964 dstProxyView->setOffset(0, 0);
1965 dstProxyView->setDstSampleType(fDstSampleType);
1966 return true;
1967 }
1968 SkASSERT(fDstSampleType == GrDstSampleType::kAsTextureCopy);
Brian Salomon467921e2017-03-06 16:17:12 -05001969
Greg Daniel3155f7f2020-01-16 16:54:26 -05001970 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05001971 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1972 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04001973 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05001974 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05001975
Michael Ludwig28e5f112020-06-09 10:57:24 -04001976 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001977 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04001978 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
1979 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
1980 // centers during rasterization.
1981 SkIRect conservativeDrawBounds = op.bounds().roundOut();
1982 conservativeDrawBounds.outset(1, 1);
1983 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07001984 }
Brian Salomon467921e2017-03-06 16:17:12 -05001985
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001986 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001987 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04001988 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07001989 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001990 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001991 } else {
Eric Karl74480882017-04-03 14:49:05 -07001992 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001993 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001994 }
Brian Salomon982127b2021-01-21 10:43:35 -05001995 auto copy = GrSurfaceProxy::Copy(fContext,
1996 this->asSurfaceProxyRef(),
1997 this->origin(),
1998 GrMipmapped::kNo,
1999 copyRect,
2000 fit,
2001 SkBudgeted::kYes,
2002 restrictions.fRectsMustMatch);
Brian Salomonc5243782020-04-02 12:50:34 -04002003 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002004
Brian Salomonc5243782020-04-02 12:50:34 -04002005 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002006 dstProxyView->setOffset(dstOffset);
Greg Danield358cbe2020-09-11 09:33:54 -04002007 dstProxyView->setDstSampleType(fDstSampleType);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002008 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002009}