blob: 96ba62a98b914f0d7aed183fc98ea4ea22f82e3e [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomoneebe7352020-12-09 16:37:04 -05008#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
Brian Osmanf5ecf512020-04-01 09:29:13 -040011#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrBackendSemaphore.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040013#include "include/gpu/GrDirectContext.h"
14#include "include/gpu/GrRecordingContext.h"
Brian Salomond005b692020-04-01 15:47:05 -040015#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkShadowFlags.h"
17#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040018#include "src/core/SkAutoPixmapStorage.h"
19#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkDrawShadowInfo.h"
21#include "src/core/SkGlyphRunPainter.h"
22#include "src/core/SkLatticeIter.h"
23#include "src/core/SkMatrixPriv.h"
Herb Derby64688222020-06-08 14:19:57 -040024#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkRRectPriv.h"
26#include "src/core/SkSurfacePriv.h"
27#include "src/gpu/GrAppliedClip.h"
Greg Danielc0d69152020-10-08 14:59:00 -040028#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040029#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrBlurUtils.h"
31#include "src/gpu/GrCaps.h"
Michael Ludwig58f569b2020-05-21 17:03:08 -040032#include "src/gpu/GrClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrColor.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040034#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040035#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040038#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040039#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrMemoryPool.h"
41#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040042#include "src/gpu/GrProxyProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040043#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/GrStyle.h"
46#include "src/gpu/GrTracing.h"
47#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040048#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/effects/GrRRectEffect.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040050#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000051#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000052#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "src/gpu/ops/GrAtlasTextOp.h"
54#include "src/gpu/ops/GrClearOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#include "src/gpu/ops/GrDrawAtlasOp.h"
56#include "src/gpu/ops/GrDrawOp.h"
57#include "src/gpu/ops/GrDrawVerticesOp.h"
58#include "src/gpu/ops/GrDrawableOp.h"
59#include "src/gpu/ops/GrFillRRectOp.h"
60#include "src/gpu/ops/GrFillRectOp.h"
61#include "src/gpu/ops/GrLatticeOp.h"
62#include "src/gpu/ops/GrOp.h"
63#include "src/gpu/ops/GrOvalOpFactory.h"
64#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050065#include "src/gpu/ops/GrShadowRRectOp.h"
66#include "src/gpu/ops/GrStencilPathOp.h"
67#include "src/gpu/ops/GrStrokeRectOp.h"
68#include "src/gpu/ops/GrTextureOp.h"
Herb Derbya08bde62020-06-12 15:46:06 -040069#include "src/gpu/text/GrSDFTOptions.h"
Herb Derby64688222020-06-08 14:19:57 -040070#include "src/gpu/text/GrTextBlobCache.h"
joshualittbc907352016-01-13 06:45:40 -080071
Robert Phillips72152832017-01-25 17:31:35 -050072#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -040073#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillips9eb00022020-06-30 15:30:12 -040074#define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
Robert Phillips9eb00022020-06-30 15:30:12 -040075#define RETURN_FALSE_IF_ABANDONED if (fContext->abandoned()) { return false; }
Robert Phillips9eb00022020-06-30 15:30:12 -040076#define RETURN_NULL_IF_ABANDONED if (fContext->abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -070077
Brian Salomone225b562017-06-14 13:00:03 -040078//////////////////////////////////////////////////////////////////////////////
79
robertphillipsea461502015-05-26 11:38:03 -070080class AutoCheckFlush {
81public:
halcanary9d524f22016-03-29 09:03:52 -070082 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -070083 SkASSERT(fDrawingManager);
84 }
bsalomonb77a9072016-09-07 10:02:04 -070085 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -070086
87private:
robertphillips77a2e522015-10-17 07:43:27 -070088 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070089};
90
Brian Salomoneebe7352020-12-09 16:37:04 -050091std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(GrRecordingContext* context,
92 GrColorType colorType,
93 sk_sp<SkColorSpace> colorSpace,
94 sk_sp<GrSurfaceProxy> proxy,
95 GrSurfaceOrigin origin,
96 const SkSurfaceProps* surfaceProps,
97 bool flushTimeOpsTask) {
Greg Danielba0ff782020-01-07 15:42:57 -050098 if (!proxy) {
99 return nullptr;
100 }
101
102 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400103 GrSwizzle readSwizzle, writeSwizzle;
104 if (colorType != GrColorType::kUnknown) {
105 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
106 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
107 }
Greg Danielba0ff782020-01-07 15:42:57 -0500108
Brian Salomon07bc9a22020-12-02 13:37:16 -0500109 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400110 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500111
Brian Salomoneebe7352020-12-09 16:37:04 -0500112 return std::make_unique<GrSurfaceDrawContext>(context,
113 std::move(readView),
114 std::move(writeView),
115 colorType,
116 std::move(colorSpace),
117 surfaceProps,
118 flushTimeOpsTask);
Greg Danielba0ff782020-01-07 15:42:57 -0500119}
120
Brian Salomoneebe7352020-12-09 16:37:04 -0500121std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Danielba0ff782020-01-07 15:42:57 -0500122 GrRecordingContext* context,
Greg Danielba0ff782020-01-07 15:42:57 -0500123 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500124 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500125 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500126 const GrBackendFormat& format,
127 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400128 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500129 GrProtected isProtected,
Brian Salomon07bc9a22020-12-02 13:37:16 -0500130 GrSwizzle readSwizzle,
131 GrSwizzle writeSwizzle,
Greg Daniele20fcad2020-01-08 11:52:34 -0500132 GrSurfaceOrigin origin,
133 SkBudgeted budgeted,
134 const SkSurfaceProps* surfaceProps) {
135 // It is probably not necessary to check if the context is abandoned here since uses of the
Brian Salomoneebe7352020-12-09 16:37:04 -0500136 // GrSurfaceDrawContext which need the context will mostly likely fail later on without an
Greg Daniele20fcad2020-01-08 11:52:34 -0500137 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
138 // an abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -0400139 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500140 return nullptr;
141 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500142
Greg Daniele20fcad2020-01-08 11:52:34 -0500143 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomon07bc9a22020-12-02 13:37:16 -0500144 format,
145 dimensions,
146 GrRenderable::kYes,
147 sampleCnt,
148 mipMapped,
149 fit,
150 budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500151 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500152 if (!proxy) {
153 return nullptr;
154 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500155
Brian Salomon07bc9a22020-12-02 13:37:16 -0500156 GrSurfaceProxyView readView ( proxy, origin, readSwizzle);
157 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
158
Brian Salomoneebe7352020-12-09 16:37:04 -0500159 auto rtc = std::make_unique<GrSurfaceDrawContext>(context,
160 std::move(readView),
161 std::move(writeView),
162 GrColorType::kUnknown,
163 std::move(colorSpace),
164 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500165 rtc->discard();
166 return rtc;
167}
168
Brian Salomoneebe7352020-12-09 16:37:04 -0500169std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500170 GrRecordingContext* context,
171 GrColorType colorType,
172 sk_sp<SkColorSpace> colorSpace,
173 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500174 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500175 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400176 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500177 GrProtected isProtected,
178 GrSurfaceOrigin origin,
179 SkBudgeted budgeted,
180 const SkSurfaceProps* surfaceProps) {
181 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
182 if (!format.isValid()) {
183 return nullptr;
184 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500185 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(format,
186 dimensions,
187 GrRenderable::kYes,
188 sampleCnt,
189 mipMapped,
190 fit,
191 budgeted,
192 isProtected);
Brian Salomon07bc9a22020-12-02 13:37:16 -0500193 if (!proxy) {
194 return nullptr;
195 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500196
Brian Salomoneebe7352020-12-09 16:37:04 -0500197 return GrSurfaceDrawContext::Make(context,
198 colorType,
199 std::move(colorSpace),
200 std::move(proxy),
201 origin,
202 surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500203}
204
205static inline GrColorType color_type_fallback(GrColorType ct) {
206 switch (ct) {
207 // kRGBA_8888 is our default fallback for many color types that may not have renderable
208 // backend formats.
209 case GrColorType::kAlpha_8:
210 case GrColorType::kBGR_565:
211 case GrColorType::kABGR_4444:
212 case GrColorType::kBGRA_8888:
213 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400214 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500215 case GrColorType::kRGBA_F16:
216 case GrColorType::kRGBA_F16_Clamped:
217 return GrColorType::kRGBA_8888;
218 case GrColorType::kAlpha_F16:
219 return GrColorType::kRGBA_F16;
220 case GrColorType::kGray_8:
221 return GrColorType::kRGB_888x;
222 default:
223 return GrColorType::kUnknown;
224 }
225}
226
Brian Salomoneebe7352020-12-09 16:37:04 -0500227std::tuple<GrColorType, GrBackendFormat> GrSurfaceDrawContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400228 GrImageContext* context, GrColorType colorType, int sampleCnt) {
229 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400230 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400231 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
232 // We continue to the fallback color type if there no default renderable format or we
233 // requested msaa and the format doesn't support msaa.
234 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400235 return {colorType, format};
236 }
237 colorType = color_type_fallback(colorType);
238 } while (colorType != GrColorType::kUnknown);
239 return {GrColorType::kUnknown, {}};
240}
241
Brian Salomoneebe7352020-12-09 16:37:04 -0500242std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeWithFallback(
Greg Daniele20fcad2020-01-08 11:52:34 -0500243 GrRecordingContext* context,
244 GrColorType colorType,
245 sk_sp<SkColorSpace> colorSpace,
246 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500247 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500248 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400249 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500250 GrProtected isProtected,
251 GrSurfaceOrigin origin,
252 SkBudgeted budgeted,
253 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400254 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400255 if (ct == GrColorType::kUnknown) {
256 return nullptr;
257 }
Brian Salomoneebe7352020-12-09 16:37:04 -0500258 return GrSurfaceDrawContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
259 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500260}
261
Brian Salomoneebe7352020-12-09 16:37:04 -0500262std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendTexture(
Greg Danielba0ff782020-01-07 15:42:57 -0500263 GrRecordingContext* context,
264 GrColorType colorType,
265 sk_sp<SkColorSpace> colorSpace,
266 const GrBackendTexture& tex,
267 int sampleCnt,
268 GrSurfaceOrigin origin,
269 const SkSurfaceProps* surfaceProps,
Robert Phillipsa1121332020-06-29 13:05:29 -0400270 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500271 SkASSERT(sampleCnt > 0);
272 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400273 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
274 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500275 if (!proxy) {
276 return nullptr;
277 }
278
Brian Salomoneebe7352020-12-09 16:37:04 -0500279 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
280 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500281}
282
Brian Salomoneebe7352020-12-09 16:37:04 -0500283std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromBackendRenderTarget(
Greg Danielba0ff782020-01-07 15:42:57 -0500284 GrRecordingContext* context,
285 GrColorType colorType,
286 sk_sp<SkColorSpace> colorSpace,
287 const GrBackendRenderTarget& rt,
288 GrSurfaceOrigin origin,
289 const SkSurfaceProps* surfaceProps,
Brian Salomon8ba54ea2020-10-01 09:48:21 -0400290 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400291 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsa1121332020-06-29 13:05:29 -0400292 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500293 if (!proxy) {
294 return nullptr;
295 }
296
Brian Salomoneebe7352020-12-09 16:37:04 -0500297 return GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
298 origin, surfaceProps);
Greg Danielba0ff782020-01-07 15:42:57 -0500299}
300
Brian Salomoneebe7352020-12-09 16:37:04 -0500301std::unique_ptr<GrSurfaceDrawContext> GrSurfaceDrawContext::MakeFromVulkanSecondaryCB(
Greg Danielba0ff782020-01-07 15:42:57 -0500302 GrRecordingContext* context,
303 const SkImageInfo& imageInfo,
304 const GrVkDrawableInfo& vkInfo,
305 const SkSurfaceProps* props) {
306 sk_sp<GrSurfaceProxy> proxy(
307 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
308 vkInfo));
309 if (!proxy) {
310 return nullptr;
311 }
312
Brian Salomoneebe7352020-12-09 16:37:04 -0500313 return GrSurfaceDrawContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
314 imageInfo.refColorSpace(), std::move(proxy),
315 kTopLeft_GrSurfaceOrigin, props);
Greg Danielba0ff782020-01-07 15:42:57 -0500316}
317
Greg Danielf41b2bd2019-08-22 16:19:24 -0400318// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
319// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
320// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
321// when the renderTargetContext attempts to use it (via getOpsTask).
Brian Salomoneebe7352020-12-09 16:37:04 -0500322GrSurfaceDrawContext::GrSurfaceDrawContext(GrRecordingContext* context,
323 GrSurfaceProxyView readView,
324 GrSurfaceProxyView writeView,
325 GrColorType colorType,
326 sk_sp<SkColorSpace> colorSpace,
327 const SkSurfaceProps* surfaceProps,
328 bool flushTimeOpsTask)
Brian Salomon14f99fc2020-12-07 12:19:47 -0500329 : GrSurfaceContext(context,
330 std::move(readView),
331 {colorType, kPremul_SkAlphaType, std::move(colorSpace)})
Brian Salomon8afde5f2020-04-01 16:22:00 -0400332 , fWriteView(std::move(writeView))
Brian Salomonf3569f02017-10-24 12:52:33 -0400333 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Robert Phillips19006652020-11-19 14:20:57 -0500334 , fFlushTimeOpsTask(flushTimeOpsTask)
Herb Derbyd29207a2020-06-08 13:50:19 -0400335 , fGlyphPainter(*this) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400336 fOpsTask = sk_ref_sp(context->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
Brian Salomon8afde5f2020-04-01 16:22:00 -0400337 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
338 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500339
robertphillips2e1e51f2015-10-15 08:01:48 -0700340 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700341}
342
robertphillips2e1e51f2015-10-15 08:01:48 -0700343#ifdef SK_DEBUG
Brian Salomoneebe7352020-12-09 16:37:04 -0500344void GrSurfaceDrawContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400345 if (fOpsTask && !fOpsTask->isClosed()) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400346 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700347 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700348}
349#endif
350
Brian Salomoneebe7352020-12-09 16:37:04 -0500351GrSurfaceDrawContext::~GrSurfaceDrawContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800352 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700353}
354
Brian Salomoneebe7352020-12-09 16:37:04 -0500355inline GrAAType GrSurfaceDrawContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600356 if (GrAA::kNo == aa) {
357 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
358 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600359 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600360 return GrAAType::kMSAA;
361 }
362 return GrAAType::kNone;
363 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600364 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600365}
366
Brian Salomoneebe7352020-12-09 16:37:04 -0500367GrMipmapped GrSurfaceDrawContext::mipmapped() const {
Greg Daniele252f082017-10-23 16:05:23 -0400368 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400369 return proxy->mipmapped();
Greg Daniele252f082017-10-23 16:05:23 -0400370 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400371 return GrMipmapped::kNo;
Greg Daniele252f082017-10-23 16:05:23 -0400372}
373
Brian Salomoneebe7352020-12-09 16:37:04 -0500374GrOpsTask* GrSurfaceDrawContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800375 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700376 SkDEBUGCODE(this->validate();)
377
Adlai Holler6ecfddb2020-11-04 12:32:30 -0500378 if (!fOpsTask || fOpsTask->isClosed()) {
Robert Phillips19006652020-11-19 14:20:57 -0500379 sk_sp<GrOpsTask> newOpsTask = this->drawingManager()->newOpsTask(this->writeSurfaceView(),
380 fFlushTimeOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600381 if (fOpsTask && fNumStencilSamples > 0) {
382 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600383 fOpsTask->setMustPreserveStencil();
384 // Reload the stencil buffer content at the beginning of newOpsTask.
385 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
386 // values?
387 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
388 }
389 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700390 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400391 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400392 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700393}
394
Herb Derby64688222020-06-08 14:19:57 -0400395static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
396 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
397 if (lcd) {
398 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
399 // can be modified. For now we just regenerate if any run in a textblob has LCD.
400 // TODO figure out where all of these modifications are and see if we can incorporate that
401 // logic at a higher level *OR* use sRGB
402 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
403
404 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
405 // see the note above. We pick a dummy value for LCD text to ensure we always match the
406 // same key
407 return SK_ColorTRANSPARENT;
408 } else {
409 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
410 // gamma corrected masks anyways, nor color
411 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
412 SkColorGetG(canonicalColor),
413 SkColorGetB(canonicalColor));
414 // reduce to our finite number of bits
415 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
416 }
417 return canonicalColor;
418}
419
Brian Salomoneebe7352020-12-09 16:37:04 -0500420void GrSurfaceDrawContext::drawGlyphRunList(const GrClip* clip,
421 const SkMatrixProvider& viewMatrix,
422 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800423 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700424 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700425 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500426 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700427
Greg Danielbe7fc462019-01-03 16:40:42 -0500428 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
429 // secondary command buffers because it would require stopping and starting a render pass which
430 // we don't have access to.
431 if (this->wrapsVkSecondaryCB()) {
432 return;
433 }
434
Herb Derbya08bde62020-06-12 15:46:06 -0400435 GrSDFTOptions options = fContext->priv().SDFTOptions();
Herb Derby64688222020-06-08 14:19:57 -0400436 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
437
438 // Get the first paint to use as the key paint.
Herb Derby281583a2020-11-19 11:40:07 -0500439 const SkPaint& drawPaint = glyphRunList.paint();
Herb Derby64688222020-06-08 14:19:57 -0400440
441 SkMaskFilterBase::BlurRec blurRec;
442 // It might be worth caching these things, but its not clear at this time
443 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
Herb Derby281583a2020-11-19 11:40:07 -0500444 const SkMaskFilter* mf = drawPaint.getMaskFilter();
Herb Derby64688222020-06-08 14:19:57 -0400445 bool canCache = glyphRunList.canCache() &&
Herb Derby281583a2020-11-19 11:40:07 -0500446 !(drawPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derby64688222020-06-08 14:19:57 -0400447
448 // If we're doing linear blending, then we can disable the gamma hacks.
449 // Otherwise, leave them on. In either case, we still want the contrast boost:
450 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
451 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
452 ? SkScalerContextFlags::kBoostContrast
453 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
454
455 sk_sp<GrTextBlob> blob;
456 GrTextBlob::Key key;
457 if (canCache) {
458 bool hasLCD = glyphRunList.anyRunsLCD();
459
460 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
461 SkPixelGeometry pixelGeometry =
462 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
463
Herb Derby281583a2020-11-19 11:40:07 -0500464 GrColor canonicalColor = compute_canonical_color(drawPaint, hasLCD);
Herb Derby64688222020-06-08 14:19:57 -0400465
466 key.fPixelGeometry = pixelGeometry;
467 key.fUniqueID = glyphRunList.uniqueID();
Herb Derby281583a2020-11-19 11:40:07 -0500468 key.fStyle = drawPaint.getStyle();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400469 if (key.fStyle != SkPaint::kFill_Style) {
Herb Derby281583a2020-11-19 11:40:07 -0500470 key.fFrameWidth = drawPaint.getStrokeWidth();
471 key.fMiterLimit = drawPaint.getStrokeMiter();
472 key.fJoin = drawPaint.getStrokeJoin();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400473 }
Herb Derby64688222020-06-08 14:19:57 -0400474 key.fHasBlur = SkToBool(mf);
Herb Derby00b2fdb2020-09-03 11:54:01 -0400475 if (key.fHasBlur) {
476 key.fBlurRec = blurRec;
477 }
Herb Derby64688222020-06-08 14:19:57 -0400478 key.fCanonicalColor = canonicalColor;
479 key.fScalerContextFlags = scalerContextFlags;
480 blob = textBlobCache->find(key);
481 }
482
Herb Derby734f8e32020-09-09 17:43:05 -0400483 SkMatrix drawMatrix(viewMatrix.localToDevice());
Herb Derby281583a2020-11-19 11:40:07 -0500484 SkPoint drawOrigin = glyphRunList.origin();
Herb Derby734f8e32020-09-09 17:43:05 -0400485 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derby281583a2020-11-19 11:40:07 -0500486 if (blob == nullptr || !blob->canReuse(drawPaint, drawMatrix)) {
Herb Derby64688222020-06-08 14:19:57 -0400487 if (blob != nullptr) {
488 // We have to remake the blob because changes may invalidate our masks.
489 // TODO we could probably get away with reuse most of the time if the pointer is unique,
490 // but we'd have to clear the SubRun information
491 textBlobCache->remove(blob.get());
492 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400493
494 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby64688222020-06-08 14:19:57 -0400495 if (canCache) {
Herb Derby00b2fdb2020-09-03 11:54:01 -0400496 blob->addKey(key);
497 textBlobCache->add(glyphRunList, blob);
Herb Derby64688222020-06-08 14:19:57 -0400498 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400499
Herb Derby734f8e32020-09-09 17:43:05 -0400500 // TODO(herb): redo processGlyphRunList to handle shifted draw matrix.
Herb Derby64688222020-06-08 14:19:57 -0400501 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
Herb Derby281583a2020-11-19 11:40:07 -0500502 for (auto& glyphRun : glyphRunList) {
503 fGlyphPainter.processGlyphRun(glyphRun,
504 viewMatrix.localToDevice(),
505 drawOrigin,
506 drawPaint,
Herb Derby734f8e32020-09-09 17:43:05 -0400507 fSurfaceProps,
508 supportsSDFT,
509 options,
510 blob.get());
Herb Derby281583a2020-11-19 11:40:07 -0500511 }
Herb Derby64688222020-06-08 14:19:57 -0400512 }
513
Herb Derby3d00a972020-07-14 11:43:14 -0400514 for (GrSubRun* subRun : blob->subRunList()) {
Herb Derby2604a892020-07-13 12:13:30 -0400515 subRun->draw(clip, viewMatrix, glyphRunList, this);
Herb Derbye5b768b2020-07-07 15:56:07 -0400516 }
robertphillipsea461502015-05-26 11:38:03 -0700517}
518
Brian Salomoneebe7352020-12-09 16:37:04 -0500519void GrSurfaceDrawContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800520 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700521 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700522 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500523 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700524
Robert Phillips72152832017-01-25 17:31:35 -0500525 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400526
Greg Danielf41b2bd2019-08-22 16:19:24 -0400527 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700528}
529
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500530static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
531 paint->setColor4f(color);
532 if (color.isOpaque()) {
533 // Can just rely on the src-over blend mode to do the right thing
534 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
535 } else {
536 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
537 // were src blended
538 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
539 }
540}
541
Michael Ludwig81d41722020-05-26 16:57:38 -0400542// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
543// premul alpha type. If we ever support different alpha type render targets, this function should
544// transform the color as appropriate.
Brian Salomoneebe7352020-12-09 16:37:04 -0500545void GrSurfaceDrawContext::internalClear(const SkIRect* scissor,
546 const SkPMColor4f& color,
547 bool upgradePartialToFull) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400548 ASSERT_SINGLE_OWNER
549 RETURN_IF_ABANDONED
550 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500551 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "clear", fContext);
Michael Ludwig81d41722020-05-26 16:57:38 -0400552
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400553 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
554 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
555 // supports then. Drawing an axis-aligned rect is the fallback path.
556 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
557 if (scissor && !scissorState.set(*scissor)) {
558 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
559 // except clear ops are not draw ops).
560 return;
561 }
562
563 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
564 // the test. We only do this when the clear would be handled by a load op or natively.
565 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
566 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
567 this->caps()->shouldInitializeTextures())) {
568 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
569 // only clear the entire target if we knew it had not been cleared before. As
570 // is this could end up doing a lot of redundant clears.
571 scissorState.setDisabled();
572 } else {
573 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
574 // render target to overflow into any approx-fit padding of the backing store dimensions
575 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400576 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500577 }
578
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400579 if (!scissorState.enabled()) {
580 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
581 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400582 GrOpsTask* opsTask = this->getOpsTask();
583 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500584 !this->caps()->performColorClearsAsDraws()) {
Brian Salomon07bc9a22020-12-02 13:37:16 -0500585 SkPMColor4f clearColor = this->writeSurfaceView().swizzle().applyTo(color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500586 // The op list was emptied and native clears are allowed, so just use the load op
Brian Salomon07bc9a22020-12-02 13:37:16 -0500587 opsTask->setColorLoadOp(GrLoadOp::kClear, clearColor.array());
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500588 return;
589 } else {
590 // Will use an op for the clear, reset the load op to discard since the op will
591 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400592 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500593 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400594 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500595
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400596 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
597 // as a load op.
598 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
599 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
600 if (clearAsDraw) {
601 GrPaint paint;
602 clear_to_grpaint(color, &paint);
603 this->addDrawOp(nullptr,
604 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
605 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500606 } else {
Brian Salomon07bc9a22020-12-02 13:37:16 -0500607 SkPMColor4f clearColor = this->writeSurfaceView().swizzle().applyTo(color);
608 this->addOp(GrClearOp::MakeColor(fContext, scissorState, clearColor.array()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500609 }
610}
611
Brian Salomoneebe7352020-12-09 16:37:04 -0500612void GrSurfaceDrawContext::drawPaint(const GrClip* clip,
613 GrPaint&& paint,
614 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000615 // Start with the render target, since that is the maximum content we could possibly fill.
616 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500617 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400618 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000619 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
620 // inverse view matrix.
621 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
622 } else {
623 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
624 SkMatrix localMatrix;
625 if (!viewMatrix.invert(&localMatrix)) {
626 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400627 }
Michael Ludwig61328202019-06-19 14:48:58 +0000628 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
629 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700630 }
robertphillipsea461502015-05-26 11:38:03 -0700631}
632
Brian Salomoneebe7352020-12-09 16:37:04 -0500633enum class GrSurfaceDrawContext::QuadOptimization {
Michael Ludwig61328202019-06-19 14:48:58 +0000634 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
635 kDiscarded,
636 // The rect to draw was converted to some other op and appended to the oplist, so no additional
637 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
638 // a constColor is provided.
639 kSubmitted,
640 // The clip was folded into the device quad, with updated edge flags and local coords, and
641 // caller is responsible for adding an appropriate op.
642 kClipApplied,
643 // No change to clip, but quad updated to better fit clip/render target, and caller is
644 // responsible for adding an appropriate op.
645 kCropped
646};
Michael Ludwig61a16512019-01-15 11:15:13 -0500647
Brian Salomoneebe7352020-12-09 16:37:04 -0500648GrSurfaceDrawContext::QuadOptimization GrSurfaceDrawContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400649 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500650 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000651 // Optimization requirements:
652 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500653 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
654 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
655 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
656 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
657 // 5. kCropped in all other scenarios (although a crop may be a no-op)
658
659 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
660 // better to just keep the old flags instead of introducing mixed edge flags.
661 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000662
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400663 // Use the logical size of the render target, which allows for "fullscreen" clears even if
664 // the render target has an approximate backing fit
665 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400666
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500667 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400668 if (!quad->fDevice.isFinite() || drawBounds.isEmpty() ||
669 GrClip::IsOutsideClip(rtRect, drawBounds)) {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000670 return QuadOptimization::kDiscarded;
671 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400672 auto conservativeCrop = [&]() {
673 static constexpr int kLargeDrawLimit = 15000;
674 // Crop the quad to the render target. This doesn't change the visual results of drawing but
675 // is meant to help numerical stability for excessively large draws.
676 if (drawBounds.width() > kLargeDrawLimit || drawBounds.height() > kLargeDrawLimit) {
677 GrQuadUtils::CropToRect(rtRect, *aa, quad, /* compute local */ !constColor);
678 SkASSERT(quad->fEdgeFlags == oldFlags);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000679 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400680 };
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000681
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400682 bool simpleColor = !stencilSettings && constColor;
Michael Ludwig6397e802020-08-05 15:50:01 -0400683 GrClip::PreClipResult result = clip ? clip->preApply(drawBounds, *aa)
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400684 : GrClip::PreClipResult(GrClip::Effect::kUnclipped);
685 switch(result.fEffect) {
686 case GrClip::Effect::kClippedOut:
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000687 return QuadOptimization::kDiscarded;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400688 case GrClip::Effect::kUnclipped:
689 if (!simpleColor) {
690 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000691 return QuadOptimization::kClipApplied;
692 } else {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400693 // Update result to store the render target bounds in order and then fall
694 // through to attempt the draw->native clear optimization
695 result = GrClip::PreClipResult(SkRRect::MakeRect(rtRect), *aa);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000696 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400697 break;
698 case GrClip::Effect::kClipped:
699 if (!result.fIsRRect || (stencilSettings && result.fAA != *aa) ||
700 (!result.fRRect.isRect() && !simpleColor)) {
701 // The clip and draw state are too complicated to try and reduce
702 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000703 return QuadOptimization::kCropped;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400704 } // Else fall through to attempt to combine the draw and clip geometry together
705 break;
706 default:
707 SkUNREACHABLE;
708 }
709
710 // If we reached here, we know we're an axis-aligned clip that is either a rect or a round rect,
711 // so we can potentially combine it with the draw geometry so that no clipping is needed.
712 SkASSERT(result.fEffect == GrClip::Effect::kClipped && result.fIsRRect);
713 SkRect clippedBounds = result.fRRect.getBounds();
714 clippedBounds.intersect(rtRect);
715 if (result.fRRect.isRect()) {
716 // No rounded corners, so we might be able to become a native clear or we might be able to
717 // modify geometry and edge flags to represent intersected shape of clip and draw.
718 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
719 /*compute local*/ !constColor)) {
720 if (simpleColor && quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
721 // Clear optimization is possible
722 drawBounds = quad->fDevice.bounds();
723 if (drawBounds.contains(rtRect)) {
724 // Fullscreen clear
725 this->clear(*constColor);
726 return QuadOptimization::kSubmitted;
727 } else if (GrClip::IsPixelAligned(drawBounds) &&
728 drawBounds.width() > 256 && drawBounds.height() > 256) {
729 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
730 SkIRect scissorRect;
731 drawBounds.round(&scissorRect);
732 this->clear(scissorRect, *constColor);
733 return QuadOptimization::kSubmitted;
734 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000735 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400736
737 // else the draw and clip were combined so just update the AA to reflect combination
738 if (*aa == GrAA::kNo && result.fAA == GrAA::kYes &&
739 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
740 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
741 // properly reflect the smooth edge of the clip.
742 *aa = GrAA::kYes;
743 }
744 // We intentionally do not downgrade AA here because we don't know if we need to
745 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
746 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
747 // deviceQuad is exactly the intersection of original quad and clip, so it can be
748 // drawn with no clip (submitted by caller)
749 return QuadOptimization::kClipApplied;
750 }
751 } else {
752 // Rounded corners and constant filled color (limit ourselves to solid colors because
753 // there is no way to use custom local coordinates with drawRRect).
754 SkASSERT(simpleColor);
755 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
756 /* compute local */ false) &&
757 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
758 quad->fDevice.bounds().contains(clippedBounds)) {
759 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
760 // we can draw the rrect directly and ignore the edge flags
761 GrPaint paint;
762 clear_to_grpaint(*constColor, &paint);
763 this->drawRRect(nullptr, std::move(paint), result.fAA, SkMatrix::I(), result.fRRect,
764 GrStyle::SimpleFill());
765 return QuadOptimization::kSubmitted;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000766 }
767 }
768
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400769 // The quads have been updated to better fit the clip bounds, but can't get rid of
770 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500771 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000772 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000773}
774
Brian Salomoneebe7352020-12-09 16:37:04 -0500775void GrSurfaceDrawContext::drawFilledQuad(const GrClip* clip,
776 GrPaint&& paint,
777 GrAA aa,
778 DrawQuad* quad,
779 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000780 ASSERT_SINGLE_OWNER
781 RETURN_IF_ABANDONED
782 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500783 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500784
Michael Ludwig61328202019-06-19 14:48:58 +0000785 AutoCheckFlush acf(this->drawingManager());
786
787 SkPMColor4f* constColor = nullptr;
788 SkPMColor4f paintColor;
John Stiles41d91b62020-07-21 14:39:40 -0400789 if (!ss && !paint.hasCoverageFragmentProcessor() && paint.isConstantBlendedColor(&paintColor)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000790 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
791 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500792 }
793
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500794 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000795 if (opt >= QuadOptimization::kClipApplied) {
796 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400797 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000798 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
799 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500800 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
801 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700802 }
Michael Ludwig61328202019-06-19 14:48:58 +0000803 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800804}
805
Brian Salomoneebe7352020-12-09 16:37:04 -0500806void GrSurfaceDrawContext::drawTexturedQuad(const GrClip* clip,
807 GrSurfaceProxyView proxyView,
808 SkAlphaType srcAlphaType,
809 sk_sp<GrColorSpaceXform> textureXform,
810 GrSamplerState::Filter filter,
811 GrSamplerState::MipmapMode mm,
812 const SkPMColor4f& color,
813 SkBlendMode blendMode,
814 GrAA aa,
815 DrawQuad* quad,
816 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000817 ASSERT_SINGLE_OWNER
818 RETURN_IF_ABANDONED
819 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400820 SkASSERT(proxyView.asTextureProxy());
Brian Salomoneebe7352020-12-09 16:37:04 -0500821 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTexturedQuad", fContext);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000822
823 AutoCheckFlush acf(this->drawingManager());
824
825 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
826 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500827 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000828
829 SkASSERT(opt != QuadOptimization::kSubmitted);
830 if (opt != QuadOptimization::kDiscarded) {
831 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400832 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000833 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400834 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400835 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
836 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400837 // Use the provided subset, although hypothetically we could detect that the cropped local
838 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500839 this->addDrawOp(finalClip,
840 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400841 std::move(textureXform), filter, mm, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400842 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000843 }
844}
845
Brian Salomoneebe7352020-12-09 16:37:04 -0500846void GrSurfaceDrawContext::drawRect(const GrClip* clip,
847 GrPaint&& paint,
848 GrAA aa,
849 const SkMatrix& viewMatrix,
850 const SkRect& rect,
851 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700852 if (!style) {
853 style = &GrStyle::SimpleFill();
854 }
joshualitt1de610a2016-01-06 08:26:09 -0800855 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700856 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700857 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500858 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700859
bsalomon6663acf2016-05-10 09:14:17 -0700860 // Path effects should've been devolved to a path in SkGpuDevice
861 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700862
Robert Phillips72152832017-01-25 17:31:35 -0500863 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700864
bsalomon6663acf2016-05-10 09:14:17 -0700865 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400866 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000867 // Fills the rect, using rect as its own local coordinates
868 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500869 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400870 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
871 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
872 (rect.width() && rect.height())) {
873 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
874 // GrStyledShape to handle stroke caps and dashing properly.
Chris Dalton7d6748e2019-03-13 00:34:52 -0600875 GrAAType aaType = this->chooseAAType(aa);
Herb Derbyc76d4092020-10-07 16:46:15 -0400876 GrOp::Owner op = GrStrokeRectOp::Make(
877 fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500878 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
879 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500880 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400881 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700882 return;
robertphillips4bc31812016-03-01 12:22:49 -0800883 }
robertphillips4bc31812016-03-01 12:22:49 -0800884 }
Mike Klein16885072018-12-11 09:54:31 -0500885 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000886 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
887 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700888}
889
Brian Salomoneebe7352020-12-09 16:37:04 -0500890void GrSurfaceDrawContext::drawQuadSet(const GrClip* clip,
891 GrPaint&& paint,
892 GrAA aa,
893 const SkMatrix& viewMatrix,
894 const QuadSetEntry quads[],
895 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600896 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500897
898 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
899 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500900}
901
Brian Salomoneebe7352020-12-09 16:37:04 -0500902int GrSurfaceDrawContext::maxWindowRectangles() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500903 return this->asRenderTargetProxy()->maxWindowRectangles(*this->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500904}
905
Brian Salomoneebe7352020-12-09 16:37:04 -0500906GrOpsTask::CanDiscardPreviousOps GrSurfaceDrawContext::canDiscardPreviousOpsOnFullClear() const {
Chris Dalton6b982802019-06-27 13:53:46 -0600907#if GR_TEST_UTILS
908 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400909 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600910 }
911#endif
912 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
913 // would normally be overwritten. The one exception is if the render target context is marked as
914 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
915 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
916 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
917 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400918 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600919}
920
Brian Salomoneebe7352020-12-09 16:37:04 -0500921void GrSurfaceDrawContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600922 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600923 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600924 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600925
Chris Daltoneffee202019-07-01 22:28:03 -0600926 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600927 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500928 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600929 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400930 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600931 }
932 SkASSERT(numRequiredSamples > 0);
933
934 if (numRequiredSamples > fNumStencilSamples) {
935 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500936 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600937 }
Chris Dalton6b982802019-06-27 13:53:46 -0600938
Chris Dalton674f77a2019-09-30 20:49:39 -0600939 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600940 if (this->caps()->performStencilClearsAsDraws()) {
941 // There is a driver bug with clearing stencil. We must use an op to manually clear the
942 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400943 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600944 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600945 this->getOpsTask()->setInitialStencilContent(
946 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600947 }
948 }
949}
950
Brian Salomoneebe7352020-12-09 16:37:04 -0500951void GrSurfaceDrawContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600952 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
953
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400954 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
955 if (scissor && !scissorState.set(*scissor)) {
956 // The requested clear region is off screen, so nothing to do.
957 return;
958 }
959
Michael Ludwig1e632792020-05-21 12:45:31 -0400960 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400961 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400962 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500963 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500964
965 // Configure the paint to have no impact on the color buffer
966 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400967 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400968 this->addDrawOp(nullptr,
969 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
970 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500971 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400972 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500973 }
robertphillips976f5f02016-06-03 10:59:20 -0700974}
975
Brian Salomoneebe7352020-12-09 16:37:04 -0500976void GrSurfaceDrawContext::stencilPath(const GrHardClip* clip,
977 GrAA doStencilMSAA,
978 const SkMatrix& viewMatrix,
979 sk_sp<const GrPath> path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -0500980 ASSERT_SINGLE_OWNER
981 RETURN_IF_ABANDONED
982 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -0500983 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "stencilPath", fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500984
Brian Salomon467921e2017-03-06 16:17:12 -0500985 // TODO: extract portions of checkDraw that are relevant to path stenciling.
986 SkASSERT(path);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500987 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
Brian Salomon467921e2017-03-06 16:17:12 -0500988
989 // FIXME: Use path bounds instead of this WAR once
990 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500991 SkIRect bounds = SkIRect::MakeSize(this->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -0500992
Michael Ludwig7c12e282020-05-29 09:54:07 -0400993 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
994 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
995 // during this clip application.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500996 GrAppliedHardClip appliedClip(this->dimensions(),
997 this->asSurfaceProxy()->backingStoreDimensions());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400998
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400999 if (clip && GrClip::Effect::kClippedOut == clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001000 return;
1001 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001002 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1003 // but as it is, we're just using the full render target so intersecting the two bounds would
1004 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001005
Brian Salomon70fe17e2020-11-30 14:33:58 -05001006 GrOp::Owner op = GrStencilPathOp::Make(fContext,
Herb Derbyc76d4092020-10-07 16:46:15 -04001007 viewMatrix,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001008 doStencilMSAA == GrAA::kYes,
Herb Derbyc76d4092020-10-07 16:46:15 -04001009 appliedClip.hasStencilClip(),
1010 appliedClip.scissorState(),
1011 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001012 if (!op) {
1013 return;
1014 }
Michael Ludwig6397e802020-08-05 15:50:01 -04001015 op->setClippedBounds(SkRect::Make(bounds));
Chris Dalton6b982802019-06-27 13:53:46 -06001016
Brian Salomon70fe17e2020-11-30 14:33:58 -05001017 this->setNeedsStencil(GrAA::kYes == doStencilMSAA);
1018 this->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001019}
1020
Brian Salomoneebe7352020-12-09 16:37:04 -05001021void GrSurfaceDrawContext::drawTextureSet(const GrClip* clip,
1022 TextureSetEntry set[],
1023 int cnt,
1024 int proxyRunCnt,
1025 GrSamplerState::Filter filter,
1026 GrSamplerState::MipmapMode mm,
1027 SkBlendMode mode,
1028 GrAA aa,
1029 SkCanvas::SrcRectConstraint constraint,
1030 const SkMatrix& viewMatrix,
1031 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001032 ASSERT_SINGLE_OWNER
1033 RETURN_IF_ABANDONED
1034 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001035 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001036
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001037 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1038 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1039 AutoCheckFlush acf(this->drawingManager());
1040 GrAAType aaType = this->chooseAAType(aa);
1041 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1042 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1043 : GrTextureOp::Saturate::kNo;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001044 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, mm, saturate,
Michael Ludwig379e4962019-12-06 13:21:26 -05001045 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001046}
1047
Brian Salomoneebe7352020-12-09 16:37:04 -05001048void GrSurfaceDrawContext::drawVertices(const GrClip* clip,
1049 GrPaint&& paint,
1050 const SkMatrixProvider& matrixProvider,
1051 sk_sp<SkVertices> vertices,
1052 GrPrimitiveType* overridePrimType,
1053 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001054 ASSERT_SINGLE_OWNER
1055 RETURN_IF_ABANDONED
1056 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001057 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001058
1059 AutoCheckFlush acf(this->drawingManager());
1060
1061 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001062 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001063 GrOp::Owner op =
Brian Osman449b1152020-04-15 16:43:00 -04001064 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001065 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001066 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001067 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001068}
1069
1070///////////////////////////////////////////////////////////////////////////////
1071
Brian Salomoneebe7352020-12-09 16:37:04 -05001072void GrSurfaceDrawContext::drawAtlas(const GrClip* clip,
1073 GrPaint&& paint,
1074 const SkMatrix& viewMatrix,
1075 int spriteCount,
1076 const SkRSXform xform[],
1077 const SkRect texRect[],
1078 const SkColor colors[]) {
Brian Osman4d92b892019-03-24 00:53:23 +00001079 ASSERT_SINGLE_OWNER
1080 RETURN_IF_ABANDONED
1081 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001082 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAtlas", fContext);
Brian Osman4d92b892019-03-24 00:53:23 +00001083
1084 AutoCheckFlush acf(this->drawingManager());
1085
1086 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001087 GrOp::Owner op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1088 aaType, spriteCount, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +00001089 this->addDrawOp(clip, std::move(op));
1090}
1091
1092///////////////////////////////////////////////////////////////////////////////
1093
Brian Salomoneebe7352020-12-09 16:37:04 -05001094void GrSurfaceDrawContext::drawRRect(const GrClip* origClip,
1095 GrPaint&& paint,
1096 GrAA aa,
1097 const SkMatrix& viewMatrix,
1098 const SkRRect& rrect,
1099 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001100 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001101 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001102 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001103 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001104
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001105 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1106
Robert Phillips85290802018-07-02 13:14:28 -04001107 const SkStrokeRec& stroke = style.strokeRec();
1108 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001109 return;
1110 }
1111
Michael Ludwig7c12e282020-05-29 09:54:07 -04001112 const GrClip* clip = origClip;
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001113 // It is not uncommon to clip to a round rect and then draw that same round rect. Since our
1114 // lower level clip code works from op bounds, which are SkRects, it doesn't detect that the
1115 // clip can be ignored. The following test attempts to mitigate the stencil clip cost but only
1116 // works for axis-aligned round rects. This also only works for filled rrects since the stroke
1117 // width outsets beyond the rrect itself.
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001118 // TODO: skbug.com/10462 - There was mixed performance wins and regressions when this
1119 // optimization was turned on outside of Android Framework. I (michaelludwig) believe this is
1120 // do to the overhead in determining if an SkClipStack is just a rrect. Once that is improved,
1121 // re-enable this and see if we avoid the regressions.
1122#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
bsalomon7f0d9f32016-08-15 14:49:10 -07001123 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001124 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001125 rrect.transform(viewMatrix, &devRRect)) {
Michael Ludwig6397e802020-08-05 15:50:01 -04001126 GrClip::PreClipResult result = clip->preApply(devRRect.getBounds(), aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001127 switch(result.fEffect) {
1128 case GrClip::Effect::kClippedOut:
1129 return;
1130 case GrClip::Effect::kUnclipped:
1131 clip = nullptr;
1132 break;
1133 case GrClip::Effect::kClipped:
1134 // Currently there's no general-purpose rrect-to-rrect contains function, and if we
1135 // got here, we know the devRRect's bounds aren't fully contained by the clip.
1136 // Testing for equality between the two is a reasonable stop-gap for now.
1137 if (result.fIsRRect && result.fRRect == devRRect) {
1138 // NOTE: On the android framework, we allow this optimization even when the clip
1139 // is non-AA and the draw is AA.
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001140 if (result.fAA == aa || (result.fAA == GrAA::kNo && aa == GrAA::kYes)) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001141 clip = nullptr;
1142 }
1143 }
1144 break;
1145 default:
1146 SkUNREACHABLE;
1147 }
1148 }
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001149#endif
ksakamotoec7f2ac2016-07-05 03:54:53 -07001150
Robert Phillips72152832017-01-25 17:31:35 -05001151 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001152
Chris Dalton7d6748e2019-03-13 00:34:52 -06001153 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001154
Herb Derbyc76d4092020-10-07 16:46:15 -04001155 GrOp::Owner op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001156 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1157 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1158 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1159 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1160 // to avoid perf regressions on some platforms.
1161 assert_alive(paint);
1162 op = GrOvalOpFactory::MakeCircularRRectOp(
1163 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1164 }
1165 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001166 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001167 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001168 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001169 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001170 assert_alive(paint);
1171 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001172 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001173 }
1174 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001175 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001176 return;
robertphillipsea461502015-05-26 11:38:03 -07001177 }
robertphillipsb56f9272016-02-25 11:03:52 -08001178
Mike Klein16885072018-12-11 09:54:31 -05001179 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001180 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001181 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001182}
1183
Jim Van Verthc5903412016-11-17 15:27:09 -05001184///////////////////////////////////////////////////////////////////////////////
1185
Jim Van Verth3af1af92017-05-18 15:06:54 -04001186static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1187 SkPoint3 result;
1188 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1189 result.fZ = pt.fZ;
1190 return result;
1191}
1192
Brian Salomoneebe7352020-12-09 16:37:04 -05001193bool GrSurfaceDrawContext::drawFastShadow(const GrClip* clip,
1194 const SkMatrix& viewMatrix,
1195 const SkPath& path,
1196 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001197 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001198 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001199 return true;
1200 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001201 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001202 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203
1204 // check z plane
1205 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1206 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1207 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1208 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1209 return false;
1210 }
1211
1212 SkRRect rrect;
1213 SkRect rect;
1214 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001215 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001216 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1217 if (!isRRect &&
1218 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1219 rect.width() > SK_ScalarNearlyZero) {
1220 rrect.setOval(rect);
1221 isRRect = true;
1222 }
1223 if (!isRRect && path.isRect(&rect)) {
1224 rrect.setRect(rect);
1225 isRRect = true;
1226 }
1227
1228 if (!isRRect) {
1229 return false;
1230 }
1231
Jim Van Verthc5903412016-11-17 15:27:09 -05001232 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001233 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001234 }
1235
Robert Phillips72152832017-01-25 17:31:35 -05001236 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001237
Jim Van Verth3af1af92017-05-18 15:06:54 -04001238 // transform light
1239 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1240
1241 // 1/scale
1242 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001243 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001244 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1245 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1246
1247 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1249
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001250 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001251 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1252 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1253 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254
1255 // Outset the shadow rrect to the border of the penumbra
1256 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1257 SkRRect ambientRRect;
1258 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1259 // If the rrect was an oval then its outset will also be one.
1260 // We set it explicitly to avoid errors.
1261 if (rrect.isOval()) {
1262 ambientRRect = SkRRect::MakeOval(outsetRect);
1263 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001264 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001265 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1266 }
1267
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001268 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001269 if (transparent) {
1270 // set a large inset to force a fill
1271 devSpaceInsetWidth = ambientRRect.width();
1272 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001273
Herb Derbyc76d4092020-10-07 16:46:15 -04001274 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1275 ambientColor,
1276 viewMatrix,
1277 ambientRRect,
1278 devSpaceAmbientBlur,
1279 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001280 if (op) {
1281 this->addDrawOp(clip, std::move(op));
1282 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001283 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001284
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001285 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001286 SkScalar devSpaceSpotBlur;
1287 SkScalar spotScale;
1288 SkVector spotOffset;
1289 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1290 devLightPos.fZ, rec.fLightRadius,
1291 &devSpaceSpotBlur, &spotScale, &spotOffset);
1292 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001293 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1294
Jim Van Verth3af1af92017-05-18 15:06:54 -04001295 // Adjust translate for the effect of the scale.
1296 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1297 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1298 // This offset is in dev space, need to transform it into source space.
1299 SkMatrix ctmInverse;
1300 if (viewMatrix.invert(&ctmInverse)) {
1301 ctmInverse.mapPoints(&spotOffset, 1);
1302 } else {
1303 // Since the matrix is a similarity, this should never happen, but just in case...
1304 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1305 SkASSERT(false);
1306 }
1307
1308 // Compute the transformed shadow rrect
1309 SkRRect spotShadowRRect;
1310 SkMatrix shadowTransform;
1311 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1312 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001313 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001314
1315 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001316 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001317 SkScalar insetWidth = blurOutset;
1318 if (transparent) {
1319 // If transparent, just do a fill
1320 insetWidth += spotShadowRRect.width();
1321 } else {
1322 // For shadows, instead of using a stroke we specify an inset from the penumbra
1323 // border. We want to extend this inset area so that it meets up with the caster
1324 // geometry. The inset geometry will by default already be inset by the blur width.
1325 //
1326 // We compare the min and max corners inset by the radius between the original
1327 // rrect and the shadow rrect. The distance between the two plus the difference
1328 // between the scaled radius and the original radius gives the distance from the
1329 // transformed shadow shape to the original shape in that corner. The max
1330 // of these gives the maximum distance we need to cover.
1331 //
1332 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1333 // that to get the full insetWidth.
1334 SkScalar maxOffset;
1335 if (rrect.isRect()) {
1336 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001337 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001338 rrect.rect().fLeft),
1339 SkTAbs(spotShadowRRect.rect().fTop -
1340 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001341 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 rrect.rect().fRight),
1343 SkTAbs(spotShadowRRect.rect().fBottom -
1344 rrect.rect().fBottom)));
1345 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001346 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001347 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1348 rrect.rect().fLeft + dr,
1349 spotShadowRRect.rect().fTop -
1350 rrect.rect().fTop + dr);
1351 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1352 rrect.rect().fRight - dr,
1353 spotShadowRRect.rect().fBottom -
1354 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001355 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001356 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001357 }
Brian Osman788b9162020-02-07 10:36:46 -05001358 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001359 }
1360
1361 // Outset the shadow rrect to the border of the penumbra
1362 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1363 if (spotShadowRRect.isOval()) {
1364 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1365 } else {
1366 SkScalar outsetRad = spotRadius + blurOutset;
1367 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1368 }
1369
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001370 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001371
Herb Derbyc76d4092020-10-07 16:46:15 -04001372 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1373 spotColor,
1374 viewMatrix,
1375 spotShadowRRect,
Brian Salomoneebe7352020-12-09 16:37:04 -05001376 2.0f * devSpaceSpotBlur,
Herb Derbyc76d4092020-10-07 16:46:15 -04001377 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001378 if (op) {
1379 this->addDrawOp(clip, std::move(op));
1380 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001381 }
1382
1383 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001384}
1385
1386///////////////////////////////////////////////////////////////////////////////
1387
Brian Salomoneebe7352020-12-09 16:37:04 -05001388bool GrSurfaceDrawContext::drawFilledDRRect(const GrClip* clip,
1389 GrPaint&& paint,
1390 GrAA aa,
1391 const SkMatrix& viewMatrix,
1392 const SkRRect& origOuter,
1393 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001394 SkASSERT(!origInner.isEmpty());
1395 SkASSERT(!origOuter.isEmpty());
1396
Brian Salomon65749212017-12-01 16:01:47 -05001397 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1398
Chris Dalton7d6748e2019-03-13 00:34:52 -06001399 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001400
1401 if (GrAAType::kMSAA == aaType) {
1402 return false;
1403 }
1404
Greg Daniel2655ede2019-04-10 00:49:28 +00001405 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1406 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001407 auto outerR = outer->width() / 2.f;
1408 auto innerR = inner->width() / 2.f;
1409 auto cx = outer->getBounds().fLeft + outerR;
1410 auto cy = outer->getBounds().fTop + outerR;
1411 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1412 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1413 auto avgR = (innerR + outerR) / 2.f;
1414 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1415 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1416 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001417 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001418 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001419 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001420 if (op) {
1421 this->addDrawOp(clip, std::move(op));
1422 return true;
1423 }
Mike Klein16885072018-12-11 09:54:31 -05001424 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001425 }
1426 }
1427
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001428 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001429 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001430 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1431 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001432 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001433 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1434 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001435 }
robertphillips00095892016-02-29 13:50:40 -08001436
robertphillips00095892016-02-29 13:50:40 -08001437 SkMatrix inverseVM;
1438 if (!viewMatrix.isIdentity()) {
1439 if (!origInner.transform(viewMatrix, inner.writable())) {
1440 return false;
1441 }
1442 if (!origOuter.transform(viewMatrix, outer.writable())) {
1443 return false;
1444 }
1445 if (!viewMatrix.invert(&inverseVM)) {
1446 return false;
1447 }
1448 } else {
1449 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001450 }
robertphillips00095892016-02-29 13:50:40 -08001451
Ethan Nicholaseace9352018-10-15 20:09:54 +00001452 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001453 // TODO these need to be a geometry processors
John Stiles851b90e2020-06-17 13:53:37 -04001454 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, innerEdgeType, *inner, caps);
1455 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001456 return false;
1457 }
1458
John Stiles851b90e2020-06-17 13:53:37 -04001459 std::tie(success, fp) = GrRRectEffect::Make(std::move(fp), outerEdgeType, *outer, caps);
1460 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001461 return false;
1462 }
1463
John Stiles41d91b62020-07-21 14:39:40 -04001464 paint.setCoverageFragmentProcessor(std::move(fp));
robertphillips00095892016-02-29 13:50:40 -08001465
1466 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001467 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001468 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1469 }
halcanary9d524f22016-03-29 09:03:52 -07001470
Brian Salomon82f44312017-01-11 13:42:54 -05001471 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1472 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001473 return true;
1474}
1475
Brian Salomoneebe7352020-12-09 16:37:04 -05001476void GrSurfaceDrawContext::drawDRRect(const GrClip* clip,
1477 GrPaint&& paint,
1478 GrAA aa,
1479 const SkMatrix& viewMatrix,
1480 const SkRRect& outer,
1481 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001482 ASSERT_SINGLE_OWNER
1483 RETURN_IF_ABANDONED
1484 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001485 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001486
1487 SkASSERT(!outer.isEmpty());
1488 SkASSERT(!inner.isEmpty());
1489
Robert Phillips72152832017-01-25 17:31:35 -05001490 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001491
Brian Salomon82f44312017-01-11 13:42:54 -05001492 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001493 return;
1494 }
Mike Klein16885072018-12-11 09:54:31 -05001495 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001496
1497 SkPath path;
1498 path.setIsVolatile(true);
1499 path.addRRect(inner);
1500 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001501 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001502 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001503}
1504
robertphillipsea461502015-05-26 11:38:03 -07001505///////////////////////////////////////////////////////////////////////////////
1506
Brian Salomoneebe7352020-12-09 16:37:04 -05001507void GrSurfaceDrawContext::drawRegion(const GrClip* clip,
1508 GrPaint&& paint,
1509 GrAA aa,
1510 const SkMatrix& viewMatrix,
1511 const SkRegion& region,
1512 const GrStyle& style,
1513 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001514 ASSERT_SINGLE_OWNER
1515 RETURN_IF_ABANDONED
1516 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001517 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001518
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001519 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001520 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001521 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001522 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001523 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1524 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001525 aa = GrAA::kNo;
1526 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001527 }
msarettcc319b92016-08-25 18:07:18 -07001528 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001529 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001530 SkPath path;
1531 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001532 path.setIsVolatile(true);
1533
Brian Salomon82f44312017-01-11 13:42:54 -05001534 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001535 }
1536
Chris Dalton7d6748e2019-03-13 00:34:52 -06001537 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001538 GrOp::Owner op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1539 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001540 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001541}
1542
Brian Salomoneebe7352020-12-09 16:37:04 -05001543void GrSurfaceDrawContext::drawOval(const GrClip* clip,
1544 GrPaint&& paint,
1545 GrAA aa,
1546 const SkMatrix& viewMatrix,
1547 const SkRect& oval,
1548 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001549 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001550 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001551 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001552 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001553
Robert Phillips7484d202018-07-03 09:09:08 -04001554 const SkStrokeRec& stroke = style.strokeRec();
1555
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001556 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001557 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1558 return;
1559 }
1560
1561 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001562 return;
robertphillipsea461502015-05-26 11:38:03 -07001563 }
1564
Robert Phillips72152832017-01-25 17:31:35 -05001565 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001566
Chris Dalton7d6748e2019-03-13 00:34:52 -06001567 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001568
Herb Derbyc76d4092020-10-07 16:46:15 -04001569 GrOp::Owner op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001570 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1571 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001572 // We don't draw true circles as round rects in coverage mode, because it can
1573 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1574 assert_alive(paint);
1575 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1576 this->caps()->shaderCaps());
1577 }
1578 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001579 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1580 // the arc equation. This same special geometry and fragment branch also turn out to be a
1581 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1582 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1583 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001584 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001585 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1586 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001587 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001588 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001589 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001590 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1591 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001592 }
1593 if (op) {
1594 this->addDrawOp(clip, std::move(op));
1595 return;
robertphillipsea461502015-05-26 11:38:03 -07001596 }
robertphillipsb56f9272016-02-25 11:03:52 -08001597
Mike Klein16885072018-12-11 09:54:31 -05001598 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001599 this->drawShapeUsingPathRenderer(
1600 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001601 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001602}
1603
Brian Salomoneebe7352020-12-09 16:37:04 -05001604void GrSurfaceDrawContext::drawArc(const GrClip* clip,
1605 GrPaint&& paint,
1606 GrAA aa,
1607 const SkMatrix& viewMatrix,
1608 const SkRect& oval,
1609 SkScalar startAngle,
1610 SkScalar sweepAngle,
1611 bool useCenter,
1612 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001613 ASSERT_SINGLE_OWNER
1614 RETURN_IF_ABANDONED
1615 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001616 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001617
1618 AutoCheckFlush acf(this->drawingManager());
1619
Chris Dalton7d6748e2019-03-13 00:34:52 -06001620 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001621 if (GrAAType::kCoverage == aaType) {
1622 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Herb Derbyc76d4092020-10-07 16:46:15 -04001623 GrOp::Owner op = GrOvalOpFactory::MakeArcOp(fContext,
1624 std::move(paint),
1625 viewMatrix,
1626 oval,
1627 startAngle,
1628 sweepAngle,
1629 useCenter,
1630 style,
1631 shaderCaps);
Greg Daniel2655ede2019-04-10 00:49:28 +00001632 if (op) {
1633 this->addDrawOp(clip, std::move(op));
1634 return;
1635 }
1636 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001637 }
Brian Salomone4949402018-04-26 15:22:04 -04001638 this->drawShapeUsingPathRenderer(
1639 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001640 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001641}
1642
Brian Salomoneebe7352020-12-09 16:37:04 -05001643void GrSurfaceDrawContext::drawImageLattice(const GrClip* clip,
1644 GrPaint&& paint,
1645 const SkMatrix& viewMatrix,
1646 GrSurfaceProxyView view,
1647 SkAlphaType alphaType,
1648 sk_sp<GrColorSpaceXform> csxf,
1649 GrSamplerState::Filter filter,
1650 std::unique_ptr<SkLatticeIter> iter,
1651 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001652 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001653 RETURN_IF_ABANDONED
1654 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001655 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001656
Robert Phillips72152832017-01-25 17:31:35 -05001657 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001658
Herb Derbyc76d4092020-10-07 16:46:15 -04001659 GrOp::Owner op =
Greg Danieled96bca2019-12-05 15:05:54 -05001660 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001661 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001662 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001663}
1664
Brian Salomoneebe7352020-12-09 16:37:04 -05001665void GrSurfaceDrawContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001666 const SkRect& bounds) {
Herb Derbyc76d4092020-10-07 16:46:15 -04001667 GrOp::Owner op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001668 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001669 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001670}
1671
Brian Salomoneebe7352020-12-09 16:37:04 -05001672bool GrSurfaceDrawContext::waitOnSemaphores(int numSemaphores,
1673 const GrBackendSemaphore waitSemaphores[],
1674 bool deleteSemaphoresAfterWait) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001675 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001676 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001677 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001678 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001679
1680 AutoCheckFlush acf(this->drawingManager());
1681
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001682 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001683 return false;
1684 }
1685
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001686 auto direct = fContext->asDirectContext();
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001687 if (!direct) {
1688 return false;
1689 }
1690
1691 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001692
Greg Daniel414418d2020-07-08 11:44:25 -04001693 GrWrapOwnership ownership =
1694 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
1695
Greg Daniel301015c2019-11-18 14:06:46 -05001696 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1697 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001698 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001699 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -04001700 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001701 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001702 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1703 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001704 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001705}
joshualitt33a5fce2015-11-18 13:28:51 -08001706
Brian Salomoneebe7352020-12-09 16:37:04 -05001707void GrSurfaceDrawContext::drawPath(const GrClip* clip,
1708 GrPaint&& paint,
1709 GrAA aa,
1710 const SkMatrix& viewMatrix,
1711 const SkPath& path,
1712 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001713 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001714 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001715 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001716 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawPath", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001717
Michael Ludwig2686d692020-04-17 20:21:37 +00001718 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001719
Robert Phillips27927a52018-08-20 13:18:12 -04001720 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001721}
1722
Brian Salomoneebe7352020-12-09 16:37:04 -05001723void GrSurfaceDrawContext::drawShape(const GrClip* clip,
1724 GrPaint&& paint,
1725 GrAA aa,
1726 const SkMatrix& viewMatrix,
1727 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04001728 ASSERT_SINGLE_OWNER
1729 RETURN_IF_ABANDONED
1730 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001731 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawShape", fContext);
Robert Phillips20390c32018-08-17 11:01:03 -04001732
Brian Salomon2fad74a2017-12-20 13:28:55 -05001733 if (shape.isEmpty()) {
1734 if (shape.inverseFilled()) {
1735 this->drawPaint(clip, std::move(paint), viewMatrix);
1736 }
1737 return;
robertphillipsea461502015-05-26 11:38:03 -07001738 }
1739
Robert Phillips72152832017-01-25 17:31:35 -05001740 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001741
Brian Salomon2fad74a2017-12-20 13:28:55 -05001742 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06001743 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001744 SkRRect rrect;
1745 // We can ignore the starting point and direction since there is no path effect.
1746 bool inverted;
1747 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1748 if (rrect.isRect()) {
1749 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1750 &shape.style());
1751 return;
1752 } else if (rrect.isOval()) {
1753 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001754 return;
1755 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001756 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1757 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001758 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1759 viewMatrix.rectStaysRect()) {
1760 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1761 // the matrix to all the points individually rather than just to the rect
1762 SkRect rects[2];
1763 if (shape.asNestedRects(rects)) {
1764 // Concave AA paths are expensive - try to avoid them for special cases
Herb Derbyc76d4092020-10-07 16:46:15 -04001765 GrOp::Owner op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04001766 fContext, std::move(paint), viewMatrix, rects);
1767 if (op) {
1768 this->addDrawOp(clip, std::move(op));
1769 }
1770 // Returning here indicates that there is nothing to draw in this case.
1771 return;
1772 }
robertphillipsea461502015-05-26 11:38:03 -07001773 }
1774 }
robertphillips4bc31812016-03-01 12:22:49 -08001775
Michael Ludwig6a6de652020-04-30 20:16:36 -04001776 // If we get here in drawShape(), we definitely need to use path rendering
1777 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
1778 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07001779}
1780
Brian Salomoneebe7352020-12-09 16:37:04 -05001781static SkIRect get_clip_bounds(const GrSurfaceDrawContext* rtc, const GrClip* clip) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001782 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
1783}
1784
Brian Salomoneebe7352020-12-09 16:37:04 -05001785bool GrSurfaceDrawContext::drawAndStencilPath(const GrHardClip* clip,
1786 const GrUserStencilSettings* ss,
1787 SkRegion::Op op,
1788 bool invert,
1789 GrAA aa,
1790 const SkMatrix& viewMatrix,
1791 const SkPath& path) {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001792 ASSERT_SINGLE_OWNER
1793 RETURN_FALSE_IF_ABANDONED
1794 SkDEBUGCODE(this->validate();)
Brian Salomoneebe7352020-12-09 16:37:04 -05001795 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "drawAndStencilPath", fContext);
robertphillips391395d2016-03-02 09:26:36 -08001796
1797 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001798 GrPaint paint;
1799 paint.setCoverageSetOpXPFactory(op, invert);
1800 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon70fe17e2020-11-30 14:33:58 -05001801 SkRect::Make(this->dimensions()));
robertphillips391395d2016-03-02 09:26:36 -08001802 return true;
1803 }
1804
Brian Salomon70fe17e2020-11-30 14:33:58 -05001805 AutoCheckFlush acf(this->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001806
1807 // An Assumption here is that path renderer would use some form of tweaking
1808 // the src color (either the input alpha or in the frag shader) to implement
1809 // aa. If we have some future driver-mojo path AA that can do the right
1810 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon70fe17e2020-11-30 14:33:58 -05001811 GrAAType aaType = this->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001812 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001813
Brian Salomon70fe17e2020-11-30 14:33:58 -05001814 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001815
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001816 GrPaint paint;
1817 paint.setCoverageSetOpXPFactory(op, invert);
1818
Michael Ludwig2686d692020-04-17 20:21:37 +00001819 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001820 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001821 canDrawArgs.fCaps = this->caps();
1822 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08001823 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001824 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001825 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001826 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001827 canDrawArgs.fAAType = aaType;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001828 SkASSERT(!this->wrapsVkSecondaryCB());
Greg Danielbe7fc462019-01-03 16:40:42 -05001829 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001830 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001831
1832 // Don't allow the SW renderer
Brian Salomon70fe17e2020-11-30 14:33:58 -05001833 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001834 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001835 if (!pr) {
1836 return false;
1837 }
1838
Brian Salomon70fe17e2020-11-30 14:33:58 -05001839 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001840 std::move(paint),
1841 ss,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001842 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001843 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04001844 &clipConservativeBounds,
1845 &viewMatrix,
1846 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001847 aaType,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001848 this->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001849 pr->drawPath(args);
1850 return true;
1851}
1852
Brian Salomoneebe7352020-12-09 16:37:04 -05001853SkBudgeted GrSurfaceDrawContext::isBudgeted() const {
Brian Salomon70fe17e2020-11-30 14:33:58 -05001854 ASSERT_SINGLE_OWNER
robertphillips714712b2016-08-04 06:20:45 -07001855
Brian Salomon70fe17e2020-11-30 14:33:58 -05001856 if (fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001857 return SkBudgeted::kNo;
1858 }
1859
Brian Salomon70fe17e2020-11-30 14:33:58 -05001860 SkDEBUGCODE(this->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001861
Brian Salomon70fe17e2020-11-30 14:33:58 -05001862 return this->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001863}
1864
Brian Salomoneebe7352020-12-09 16:37:04 -05001865void GrSurfaceDrawContext::drawShapeUsingPathRenderer(const GrClip* clip,
1866 GrPaint&& paint,
1867 GrAA aa,
1868 const SkMatrix& viewMatrix,
1869 const GrStyledShape& originalShape,
1870 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08001871 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001872 RETURN_IF_ABANDONED
Brian Salomoneebe7352020-12-09 16:37:04 -05001873 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "internalDrawPath", fContext);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001874
Jim Van Verthf86073a2018-10-02 13:05:38 -04001875 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1876 return;
1877 }
1878
Michael Ludwig6a6de652020-04-30 20:16:36 -04001879 if (attemptShapeFallback && originalShape.simplified()) {
1880 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
1881 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
1882 // we ought to try again instead of going right to path rendering.
1883 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
1884 return;
1885 }
1886
Michael Ludwig7c12e282020-05-29 09:54:07 -04001887 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001888
Michael Ludwig2686d692020-04-17 20:21:37 +00001889 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001890 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06001891
robertphillips68737822015-10-29 12:12:21 -07001892 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001893 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05001894 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07001895 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001896 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001897 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001898 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001899 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001900 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001901
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001902 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001903 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001904 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001905 return;
1906 }
1907
Chris Dalton6ce447a2019-06-23 18:07:38 -06001908 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001909
1910 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1911 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1912 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1913
Brian Salomon2fad74a2017-12-20 13:28:55 -05001914 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001915 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001916 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1917 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001918 return;
1919 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001920 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001921 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001922 }
1923 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001924 if (canDrawArgs.fShape->style().applies()) {
1925 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1926 styleScale);
1927 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001928 return;
1929 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001930 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001931 // This time, allow SW renderer
1932 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1933 } else {
1934 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04001935#if GR_PATH_RENDERER_SPEW
1936 SkDebugf("falling back to: %s\n", pr->name());
1937#endif
bsalomon6663acf2016-05-10 09:14:17 -07001938 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001939 }
robertphillipsea461502015-05-26 11:38:03 -07001940
bsalomon8acedde2016-06-24 10:42:16 -07001941 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001942#ifdef SK_DEBUG
1943 SkDebugf("Unable to find path renderer compatible with path.\n");
1944#endif
1945 return;
1946 }
1947
Robert Phillips256c37b2017-03-01 14:32:46 -05001948 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001949 std::move(paint),
1950 &GrUserStencilSettings::kUnused,
1951 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001952 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001953 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001954 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001955 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001956 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001957 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001958 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001959}
1960
Brian Salomon467921e2017-03-06 16:17:12 -05001961static void op_bounds(SkRect* bounds, const GrOp* op) {
1962 *bounds = op->bounds();
1963 if (op->hasZeroArea()) {
1964 if (op->hasAABloat()) {
1965 bounds->outset(0.5f, 0.5f);
1966 } else {
1967 // We don't know which way the particular GPU will snap lines or points at integer
1968 // coords. So we ensure that the bounds is large enough for either snap.
1969 SkRect before = *bounds;
1970 bounds->roundOut(bounds);
1971 if (bounds->fLeft == before.fLeft) {
1972 bounds->fLeft -= 1;
1973 }
1974 if (bounds->fTop == before.fTop) {
1975 bounds->fTop -= 1;
1976 }
1977 if (bounds->fRight == before.fRight) {
1978 bounds->fRight += 1;
1979 }
1980 if (bounds->fBottom == before.fBottom) {
1981 bounds->fBottom += 1;
1982 }
1983 }
1984 }
1985}
1986
Brian Salomoneebe7352020-12-09 16:37:04 -05001987void GrSurfaceDrawContext::addOp(GrOp::Owner op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04001988 GrDrawingManager* drawingMgr = this->drawingManager();
1989 this->getOpsTask()->addOp(drawingMgr,
1990 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06001991}
1992
Brian Salomoneebe7352020-12-09 16:37:04 -05001993void GrSurfaceDrawContext::addDrawOp(const GrClip* clip,
1994 GrOp::Owner op,
1995 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001996 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001997 if (fContext->abandoned()) {
Brian Salomon348a0372018-10-31 10:42:18 -04001998 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001999 }
Herb Derbyc76d4092020-10-07 16:46:15 -04002000 GrDrawOp* drawOp = (GrDrawOp*)op.get();
robertphillips2e1e51f2015-10-15 08:01:48 -07002001 SkDEBUGCODE(this->validate();)
Herb Derbyc76d4092020-10-07 16:46:15 -04002002 SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;)
Brian Salomoneebe7352020-12-09 16:37:04 -05002003 GR_CREATE_TRACE_MARKER_CONTEXT("GrSurfaceDrawContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002004
Brian Salomon467921e2017-03-06 16:17:12 -05002005 // Setup clip
2006 SkRect bounds;
2007 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002008 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Herb Derbyc76d4092020-10-07 16:46:15 -04002009 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = drawOp->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002010 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002011 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002012
Chris Dalton9acf6822019-11-12 11:52:40 -07002013 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002014 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002015 }
2016
Michael Ludwig7c12e282020-05-29 09:54:07 -04002017 bool skipDraw = false;
2018 if (clip) {
2019 // Have a complex clip, so defer to its early clip culling
Michael Ludwig6397e802020-08-05 15:50:01 -04002020 GrAAType aaType = usesHWAA ? GrAAType::kMSAA :
2021 (op->hasAABloat() ? GrAAType::kCoverage :
2022 GrAAType::kNone);
2023 skipDraw = clip->apply(fContext, this, aaType, usesUserStencilBits,
Michael Ludwig4e3cab72020-06-30 11:12:46 -04002024 &appliedClip, &bounds) == GrClip::Effect::kClippedOut;
Michael Ludwig7c12e282020-05-29 09:54:07 -04002025 } else {
2026 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwig4e3cab72020-06-30 11:12:46 -04002027 skipDraw = !bounds.intersect(this->asSurfaceProxy()->getBoundsRect());
Michael Ludwig7c12e282020-05-29 09:54:07 -04002028 }
2029
2030 if (skipDraw) {
Brian Salomon348a0372018-10-31 10:42:18 -04002031 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002032 }
2033
Chris Dalton9acf6822019-11-12 11:52:40 -07002034 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2035 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2036
2037 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2038 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2039 // that uses a stencil test when the stencil buffer is multisampled.)
2040 bool hasMixedSampledCoverage = (
2041 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002042 SkASSERT(!hasMixedSampledCoverage ||
2043 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002044
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002045 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Herb Derbyc76d4092020-10-07 16:46:15 -04002046 GrProcessorSet::Analysis analysis = drawOp->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002047 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2048
Michael Ludwig28e5f112020-06-09 10:57:24 -04002049 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2050 op->setClippedBounds(bounds);
2051
Greg Daniel524e28b2019-11-01 11:48:53 -04002052 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002053 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002054 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Brian Salomon348a0372018-10-31 10:42:18 -04002055 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002056 }
2057 }
2058
Greg Danielf41b2bd2019-08-22 16:19:24 -04002059 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002060 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002061 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002062 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002063 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -04002064 dstProxyView, GrTextureResolveManager(this->drawingManager()),
2065 *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002066}
2067
Brian Salomoneebe7352020-12-09 16:37:04 -05002068bool GrSurfaceDrawContext::setupDstProxyView(const GrOp& op,
2069 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002070 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2071 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2072 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002073 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002074 return false;
2075 }
2076
Greg Danield358cbe2020-09-11 09:33:54 -04002077 if (fDstSampleType == GrDstSampleType::kNone) {
2078 fDstSampleType = this->caps()->getDstSampleTypeForProxy(this->asRenderTargetProxy());
Brian Salomon467921e2017-03-06 16:17:12 -05002079 }
Greg Danield358cbe2020-09-11 09:33:54 -04002080 SkASSERT(fDstSampleType != GrDstSampleType::kNone);
2081
2082 if (GrDstSampleTypeDirectlySamplesDst(fDstSampleType)) {
2083 // The render target is a texture or input attachment, so we can read from it directly in
2084 // the shader. The XP will be responsible to detect this situation and request a texture
2085 // barrier.
2086 dstProxyView->setProxyView(this->readSurfaceView());
2087 dstProxyView->setOffset(0, 0);
2088 dstProxyView->setDstSampleType(fDstSampleType);
2089 return true;
2090 }
2091 SkASSERT(fDstSampleType == GrDstSampleType::kAsTextureCopy);
Brian Salomon467921e2017-03-06 16:17:12 -05002092
Greg Daniel3155f7f2020-01-16 16:54:26 -05002093 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002094 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2095 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002096 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002097 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002098
Michael Ludwig28e5f112020-06-09 10:57:24 -04002099 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002100 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002101 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2102 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2103 // centers during rasterization.
2104 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2105 conservativeDrawBounds.outset(1, 1);
2106 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002107 }
Brian Salomon467921e2017-03-06 16:17:12 -05002108
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002109 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002110 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002111 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002112 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002113 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002114 } else {
Eric Karl74480882017-04-03 14:49:05 -07002115 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002116 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002117 }
Brian Salomonc5243782020-04-02 12:50:34 -04002118 auto copy =
Brian Salomon7e67dca2020-07-21 09:27:25 -04002119 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipmapped::kNo,
Brian Salomonc5243782020-04-02 12:50:34 -04002120 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2121 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002122
Brian Salomonc5243782020-04-02 12:50:34 -04002123 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002124 dstProxyView->setOffset(dstOffset);
Greg Danield358cbe2020-09-11 09:33:54 -04002125 dstProxyView->setDstSampleType(fDstSampleType);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002126 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002127}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002128
Brian Salomoneebe7352020-12-09 16:37:04 -05002129bool GrSurfaceDrawContext::blitTexture(GrSurfaceProxyView view,
2130 const SkIRect& srcRect,
2131 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002132 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002133 SkIRect clippedSrcRect;
2134 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002135 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2136 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002137 return false;
2138 }
2139
2140 GrPaint paint;
2141 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002142
Greg Danield2ccbb52020-02-05 10:45:39 -05002143 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002144 if (!fp) {
2145 return false;
2146 }
John Stiles5933d7d2020-07-21 12:28:35 -04002147 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel46cfbc62019-06-07 11:43:30 -04002148
2149 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002150 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002151 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2152 clippedSrcRect.height()),
2153 SkRect::Make(clippedSrcRect));
2154 return true;
2155}