blob: 4f4c46ff296ee88b8a760314671662a41eb2c778 [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 Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
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
Greg Daniele20fcad2020-01-08 11:52:34 -050091std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
92 GrRecordingContext* context,
93 GrColorType colorType,
94 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -050095 sk_sp<GrSurfaceProxy> proxy,
96 GrSurfaceOrigin origin,
97 const SkSurfaceProps* surfaceProps,
Robert Phillips19006652020-11-19 14:20:57 -050098 bool flushTimeOpsTask) {
Greg Danielba0ff782020-01-07 15:42:57 -050099 if (!proxy) {
100 return nullptr;
101 }
102
103 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400104 GrSwizzle readSwizzle, writeSwizzle;
105 if (colorType != GrColorType::kUnknown) {
106 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
107 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
108 }
Greg Danielba0ff782020-01-07 15:42:57 -0500109
Greg Daniel3912a4b2020-01-14 09:56:04 -0500110 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400111 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500112
113 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400114 std::move(writeView), colorType,
Robert Phillips19006652020-11-19 14:20:57 -0500115 std::move(colorSpace), surfaceProps,
116 flushTimeOpsTask);
Greg Danielba0ff782020-01-07 15:42:57 -0500117}
118
119std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
120 GrRecordingContext* context,
121 GrColorType colorType,
122 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500123 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500124 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500125 const GrBackendFormat& format,
126 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400127 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500128 GrProtected isProtected,
129 GrSurfaceOrigin origin,
130 SkBudgeted budgeted,
131 const SkSurfaceProps* surfaceProps) {
132 // It is probably not necessary to check if the context is abandoned here since uses of the
133 // GrRenderTargetContext which need the context will mostly likely fail later on without an
134 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
135 // an abandoned context correctly. It also lets us early out of some extra work.
Robert Phillips9eb00022020-06-30 15:30:12 -0400136 if (context->abandoned()) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500137 return nullptr;
138 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500139
Greg Daniele20fcad2020-01-08 11:52:34 -0500140 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400141 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500142 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500143 if (!proxy) {
144 return nullptr;
145 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500146
Greg Danielba0ff782020-01-07 15:42:57 -0500147 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
Robert Phillips19006652020-11-19 14:20:57 -0500148 std::move(proxy), origin, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500149 if (!rtc) {
150 return nullptr;
151 }
152 rtc->discard();
153 return rtc;
154}
155
156std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
157 GrRecordingContext* context,
158 GrColorType colorType,
159 sk_sp<SkColorSpace> colorSpace,
160 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500161 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500162 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400163 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500164 GrProtected isProtected,
165 GrSurfaceOrigin origin,
166 SkBudgeted budgeted,
167 const SkSurfaceProps* surfaceProps) {
168 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
169 if (!format.isValid()) {
170 return nullptr;
171 }
172
173 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
174 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
175 surfaceProps);
176}
177
178static inline GrColorType color_type_fallback(GrColorType ct) {
179 switch (ct) {
180 // kRGBA_8888 is our default fallback for many color types that may not have renderable
181 // backend formats.
182 case GrColorType::kAlpha_8:
183 case GrColorType::kBGR_565:
184 case GrColorType::kABGR_4444:
185 case GrColorType::kBGRA_8888:
186 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400187 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500188 case GrColorType::kRGBA_F16:
189 case GrColorType::kRGBA_F16_Clamped:
190 return GrColorType::kRGBA_8888;
191 case GrColorType::kAlpha_F16:
192 return GrColorType::kRGBA_F16;
193 case GrColorType::kGray_8:
194 return GrColorType::kRGB_888x;
195 default:
196 return GrColorType::kUnknown;
197 }
198}
199
Brian Salomond005b692020-04-01 15:47:05 -0400200std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400201 GrImageContext* context, GrColorType colorType, int sampleCnt) {
202 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400203 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400204 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
205 // We continue to the fallback color type if there no default renderable format or we
206 // requested msaa and the format doesn't support msaa.
207 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400208 return {colorType, format};
209 }
210 colorType = color_type_fallback(colorType);
211 } while (colorType != GrColorType::kUnknown);
212 return {GrColorType::kUnknown, {}};
213}
214
Greg Daniele20fcad2020-01-08 11:52:34 -0500215std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
216 GrRecordingContext* context,
217 GrColorType colorType,
218 sk_sp<SkColorSpace> colorSpace,
219 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500220 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500221 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400222 GrMipmapped mipMapped,
Greg Daniele20fcad2020-01-08 11:52:34 -0500223 GrProtected isProtected,
224 GrSurfaceOrigin origin,
225 SkBudgeted budgeted,
226 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400227 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400228 if (ct == GrColorType::kUnknown) {
229 return nullptr;
230 }
231 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
232 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500233}
234
Greg Danielba0ff782020-01-07 15:42:57 -0500235std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
236 GrRecordingContext* context,
237 GrColorType colorType,
238 sk_sp<SkColorSpace> colorSpace,
239 const GrBackendTexture& tex,
240 int sampleCnt,
241 GrSurfaceOrigin origin,
242 const SkSurfaceProps* surfaceProps,
Robert Phillipsa1121332020-06-29 13:05:29 -0400243 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500244 SkASSERT(sampleCnt > 0);
245 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400246 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
247 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500248 if (!proxy) {
249 return nullptr;
250 }
251
252 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
253 origin, surfaceProps);
254}
255
Greg Danielba0ff782020-01-07 15:42:57 -0500256std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
257 GrRecordingContext* context,
258 GrColorType colorType,
259 sk_sp<SkColorSpace> colorSpace,
260 const GrBackendRenderTarget& rt,
261 GrSurfaceOrigin origin,
262 const SkSurfaceProps* surfaceProps,
Brian Salomon8ba54ea2020-10-01 09:48:21 -0400263 sk_sp<GrRefCntedCallback> releaseHelper) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400264 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsa1121332020-06-29 13:05:29 -0400265 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500266 if (!proxy) {
267 return nullptr;
268 }
269
270 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
271 origin, surfaceProps);
272}
273
274std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
275 GrRecordingContext* context,
276 const SkImageInfo& imageInfo,
277 const GrVkDrawableInfo& vkInfo,
278 const SkSurfaceProps* props) {
279 sk_sp<GrSurfaceProxy> proxy(
280 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
281 vkInfo));
282 if (!proxy) {
283 return nullptr;
284 }
285
Greg Danielba0ff782020-01-07 15:42:57 -0500286 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
287 imageInfo.refColorSpace(), std::move(proxy),
288 kTopLeft_GrSurfaceOrigin, props);
289}
290
Greg Danielf41b2bd2019-08-22 16:19:24 -0400291// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
292// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
293// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
294// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500295GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500296 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400297 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400298 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400299 sk_sp<SkColorSpace> colorSpace,
300 const SkSurfaceProps* surfaceProps,
Robert Phillips19006652020-11-19 14:20:57 -0500301 bool flushTimeOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500302 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
303 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400304 , fWriteView(std::move(writeView))
Brian Salomonf3569f02017-10-24 12:52:33 -0400305 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Robert Phillips19006652020-11-19 14:20:57 -0500306 , fFlushTimeOpsTask(flushTimeOpsTask)
Herb Derbyd29207a2020-06-08 13:50:19 -0400307 , fGlyphPainter(*this) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400308 fOpsTask = sk_ref_sp(context->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
Brian Salomon8afde5f2020-04-01 16:22:00 -0400309 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
310 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500311
robertphillips2e1e51f2015-10-15 08:01:48 -0700312 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700313}
314
robertphillips2e1e51f2015-10-15 08:01:48 -0700315#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500316void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400317 if (fOpsTask && !fOpsTask->isClosed()) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400318 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700319 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700320}
321#endif
322
Brian Osman11052242016-10-27 14:47:55 -0400323GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800324 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700325}
326
Chris Dalton7d6748e2019-03-13 00:34:52 -0600327inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600328 if (GrAA::kNo == aa) {
329 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
330 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600331 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600332 return GrAAType::kMSAA;
333 }
334 return GrAAType::kNone;
335 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600336 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600337}
338
Brian Salomon8c82a872020-07-21 12:09:58 -0400339GrMipmapped GrRenderTargetContext::mipmapped() const {
Greg Daniele252f082017-10-23 16:05:23 -0400340 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400341 return proxy->mipmapped();
Greg Daniele252f082017-10-23 16:05:23 -0400342 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400343 return GrMipmapped::kNo;
Greg Daniele252f082017-10-23 16:05:23 -0400344}
345
Greg Danielf41b2bd2019-08-22 16:19:24 -0400346GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800347 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700348 SkDEBUGCODE(this->validate();)
349
Adlai Holler6ecfddb2020-11-04 12:32:30 -0500350 if (!fOpsTask || fOpsTask->isClosed()) {
Robert Phillips19006652020-11-19 14:20:57 -0500351 sk_sp<GrOpsTask> newOpsTask = this->drawingManager()->newOpsTask(this->writeSurfaceView(),
352 fFlushTimeOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600353 if (fOpsTask && fNumStencilSamples > 0) {
354 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600355 fOpsTask->setMustPreserveStencil();
356 // Reload the stencil buffer content at the beginning of newOpsTask.
357 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
358 // values?
359 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
360 }
361 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700362 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400363 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400364 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700365}
366
Herb Derby64688222020-06-08 14:19:57 -0400367static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
368 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
369 if (lcd) {
370 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
371 // can be modified. For now we just regenerate if any run in a textblob has LCD.
372 // TODO figure out where all of these modifications are and see if we can incorporate that
373 // logic at a higher level *OR* use sRGB
374 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
375
376 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
377 // see the note above. We pick a dummy value for LCD text to ensure we always match the
378 // same key
379 return SK_ColorTRANSPARENT;
380 } else {
381 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
382 // gamma corrected masks anyways, nor color
383 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
384 SkColorGetG(canonicalColor),
385 SkColorGetB(canonicalColor));
386 // reduce to our finite number of bits
387 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
388 }
389 return canonicalColor;
390}
391
Michael Ludwig7c12e282020-05-29 09:54:07 -0400392void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Herb Derby411e7aa2020-07-09 16:02:08 -0400393 const SkMatrixProvider& viewMatrix,
Herb Derby64688222020-06-08 14:19:57 -0400394 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800395 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700396 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700397 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400398 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700399
Greg Danielbe7fc462019-01-03 16:40:42 -0500400 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
401 // secondary command buffers because it would require stopping and starting a render pass which
402 // we don't have access to.
403 if (this->wrapsVkSecondaryCB()) {
404 return;
405 }
406
Herb Derbya08bde62020-06-12 15:46:06 -0400407 GrSDFTOptions options = fContext->priv().SDFTOptions();
Herb Derby64688222020-06-08 14:19:57 -0400408 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
409
410 // Get the first paint to use as the key paint.
Herb Derby281583a2020-11-19 11:40:07 -0500411 const SkPaint& drawPaint = glyphRunList.paint();
Herb Derby64688222020-06-08 14:19:57 -0400412
413 SkMaskFilterBase::BlurRec blurRec;
414 // It might be worth caching these things, but its not clear at this time
415 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
Herb Derby281583a2020-11-19 11:40:07 -0500416 const SkMaskFilter* mf = drawPaint.getMaskFilter();
Herb Derby64688222020-06-08 14:19:57 -0400417 bool canCache = glyphRunList.canCache() &&
Herb Derby281583a2020-11-19 11:40:07 -0500418 !(drawPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derby64688222020-06-08 14:19:57 -0400419
420 // If we're doing linear blending, then we can disable the gamma hacks.
421 // Otherwise, leave them on. In either case, we still want the contrast boost:
422 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
423 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
424 ? SkScalerContextFlags::kBoostContrast
425 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
426
427 sk_sp<GrTextBlob> blob;
428 GrTextBlob::Key key;
429 if (canCache) {
430 bool hasLCD = glyphRunList.anyRunsLCD();
431
432 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
433 SkPixelGeometry pixelGeometry =
434 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
435
Herb Derby281583a2020-11-19 11:40:07 -0500436 GrColor canonicalColor = compute_canonical_color(drawPaint, hasLCD);
Herb Derby64688222020-06-08 14:19:57 -0400437
438 key.fPixelGeometry = pixelGeometry;
439 key.fUniqueID = glyphRunList.uniqueID();
Herb Derby281583a2020-11-19 11:40:07 -0500440 key.fStyle = drawPaint.getStyle();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400441 if (key.fStyle != SkPaint::kFill_Style) {
Herb Derby281583a2020-11-19 11:40:07 -0500442 key.fFrameWidth = drawPaint.getStrokeWidth();
443 key.fMiterLimit = drawPaint.getStrokeMiter();
444 key.fJoin = drawPaint.getStrokeJoin();
Herb Derby00b2fdb2020-09-03 11:54:01 -0400445 }
Herb Derby64688222020-06-08 14:19:57 -0400446 key.fHasBlur = SkToBool(mf);
Herb Derby00b2fdb2020-09-03 11:54:01 -0400447 if (key.fHasBlur) {
448 key.fBlurRec = blurRec;
449 }
Herb Derby64688222020-06-08 14:19:57 -0400450 key.fCanonicalColor = canonicalColor;
451 key.fScalerContextFlags = scalerContextFlags;
452 blob = textBlobCache->find(key);
453 }
454
Herb Derby734f8e32020-09-09 17:43:05 -0400455 SkMatrix drawMatrix(viewMatrix.localToDevice());
Herb Derby281583a2020-11-19 11:40:07 -0500456 SkPoint drawOrigin = glyphRunList.origin();
Herb Derby734f8e32020-09-09 17:43:05 -0400457 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derby281583a2020-11-19 11:40:07 -0500458 if (blob == nullptr || !blob->canReuse(drawPaint, drawMatrix)) {
Herb Derby64688222020-06-08 14:19:57 -0400459 if (blob != nullptr) {
460 // We have to remake the blob because changes may invalidate our masks.
461 // TODO we could probably get away with reuse most of the time if the pointer is unique,
462 // but we'd have to clear the SubRun information
463 textBlobCache->remove(blob.get());
464 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400465
466 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
Herb Derby64688222020-06-08 14:19:57 -0400467 if (canCache) {
Herb Derby00b2fdb2020-09-03 11:54:01 -0400468 blob->addKey(key);
469 textBlobCache->add(glyphRunList, blob);
Herb Derby64688222020-06-08 14:19:57 -0400470 }
Herb Derby00b2fdb2020-09-03 11:54:01 -0400471
Herb Derby734f8e32020-09-09 17:43:05 -0400472 // TODO(herb): redo processGlyphRunList to handle shifted draw matrix.
Herb Derby64688222020-06-08 14:19:57 -0400473 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
Herb Derby281583a2020-11-19 11:40:07 -0500474 for (auto& glyphRun : glyphRunList) {
475 fGlyphPainter.processGlyphRun(glyphRun,
476 viewMatrix.localToDevice(),
477 drawOrigin,
478 drawPaint,
Herb Derby734f8e32020-09-09 17:43:05 -0400479 fSurfaceProps,
480 supportsSDFT,
481 options,
482 blob.get());
Herb Derby281583a2020-11-19 11:40:07 -0500483 }
Herb Derby64688222020-06-08 14:19:57 -0400484 }
485
Herb Derby3d00a972020-07-14 11:43:14 -0400486 for (GrSubRun* subRun : blob->subRunList()) {
Herb Derby2604a892020-07-13 12:13:30 -0400487 subRun->draw(clip, viewMatrix, glyphRunList, this);
Herb Derbye5b768b2020-07-07 15:56:07 -0400488 }
robertphillipsea461502015-05-26 11:38:03 -0700489}
490
Brian Osman11052242016-10-27 14:47:55 -0400491void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800492 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700493 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700494 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400495 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700496
Robert Phillips72152832017-01-25 17:31:35 -0500497 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400498
Greg Danielf41b2bd2019-08-22 16:19:24 -0400499 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700500}
501
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500502static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
503 paint->setColor4f(color);
504 if (color.isOpaque()) {
505 // Can just rely on the src-over blend mode to do the right thing
506 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
507 } else {
508 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
509 // were src blended
510 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
511 }
512}
513
Michael Ludwig81d41722020-05-26 16:57:38 -0400514// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
515// premul alpha type. If we ever support different alpha type render targets, this function should
516// transform the color as appropriate.
517void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500518 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400519 bool upgradePartialToFull) {
520 ASSERT_SINGLE_OWNER
521 RETURN_IF_ABANDONED
522 SkDEBUGCODE(this->validate();)
523 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
524
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400525 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
526 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
527 // supports then. Drawing an axis-aligned rect is the fallback path.
528 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
529 if (scissor && !scissorState.set(*scissor)) {
530 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
531 // except clear ops are not draw ops).
532 return;
533 }
534
535 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
536 // the test. We only do this when the clear would be handled by a load op or natively.
537 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
538 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
539 this->caps()->shouldInitializeTextures())) {
540 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
541 // only clear the entire target if we knew it had not been cleared before. As
542 // is this could end up doing a lot of redundant clears.
543 scissorState.setDisabled();
544 } else {
545 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
546 // render target to overflow into any approx-fit padding of the backing store dimensions
547 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400548 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500549 }
550
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400551 if (!scissorState.enabled()) {
552 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
553 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400554 GrOpsTask* opsTask = this->getOpsTask();
555 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500556 !this->caps()->performColorClearsAsDraws()) {
557 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400558 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500559 return;
560 } else {
561 // Will use an op for the clear, reset the load op to discard since the op will
562 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400563 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500564 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400565 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500566
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400567 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
568 // as a load op.
569 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
570 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
571 if (clearAsDraw) {
572 GrPaint paint;
573 clear_to_grpaint(color, &paint);
574 this->addDrawOp(nullptr,
575 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
576 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500577 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400578 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500579 }
580}
581
Michael Ludwig7c12e282020-05-29 09:54:07 -0400582void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500583 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400584 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000585 // Start with the render target, since that is the maximum content we could possibly fill.
586 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500587 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400588 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000589 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
590 // inverse view matrix.
591 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
592 } else {
593 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
594 SkMatrix localMatrix;
595 if (!viewMatrix.invert(&localMatrix)) {
596 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400597 }
Michael Ludwig61328202019-06-19 14:48:58 +0000598 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
599 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700600 }
robertphillipsea461502015-05-26 11:38:03 -0700601}
602
Michael Ludwig61328202019-06-19 14:48:58 +0000603enum class GrRenderTargetContext::QuadOptimization {
604 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
605 kDiscarded,
606 // The rect to draw was converted to some other op and appended to the oplist, so no additional
607 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
608 // a constColor is provided.
609 kSubmitted,
610 // The clip was folded into the device quad, with updated edge flags and local coords, and
611 // caller is responsible for adding an appropriate op.
612 kClipApplied,
613 // No change to clip, but quad updated to better fit clip/render target, and caller is
614 // responsible for adding an appropriate op.
615 kCropped
616};
Michael Ludwig61a16512019-01-15 11:15:13 -0500617
Michael Ludwig61328202019-06-19 14:48:58 +0000618GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400619 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500620 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000621 // Optimization requirements:
622 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500623 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
624 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
625 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
626 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
627 // 5. kCropped in all other scenarios (although a crop may be a no-op)
628
629 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
630 // better to just keep the old flags instead of introducing mixed edge flags.
631 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000632
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400633 // Use the logical size of the render target, which allows for "fullscreen" clears even if
634 // the render target has an approximate backing fit
635 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400636
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500637 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400638 if (!quad->fDevice.isFinite() || drawBounds.isEmpty() ||
639 GrClip::IsOutsideClip(rtRect, drawBounds)) {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000640 return QuadOptimization::kDiscarded;
641 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400642 auto conservativeCrop = [&]() {
643 static constexpr int kLargeDrawLimit = 15000;
644 // Crop the quad to the render target. This doesn't change the visual results of drawing but
645 // is meant to help numerical stability for excessively large draws.
646 if (drawBounds.width() > kLargeDrawLimit || drawBounds.height() > kLargeDrawLimit) {
647 GrQuadUtils::CropToRect(rtRect, *aa, quad, /* compute local */ !constColor);
648 SkASSERT(quad->fEdgeFlags == oldFlags);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000649 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400650 };
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000651
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400652 bool simpleColor = !stencilSettings && constColor;
Michael Ludwig6397e802020-08-05 15:50:01 -0400653 GrClip::PreClipResult result = clip ? clip->preApply(drawBounds, *aa)
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400654 : GrClip::PreClipResult(GrClip::Effect::kUnclipped);
655 switch(result.fEffect) {
656 case GrClip::Effect::kClippedOut:
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000657 return QuadOptimization::kDiscarded;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400658 case GrClip::Effect::kUnclipped:
659 if (!simpleColor) {
660 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000661 return QuadOptimization::kClipApplied;
662 } else {
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400663 // Update result to store the render target bounds in order and then fall
664 // through to attempt the draw->native clear optimization
665 result = GrClip::PreClipResult(SkRRect::MakeRect(rtRect), *aa);
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000666 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400667 break;
668 case GrClip::Effect::kClipped:
669 if (!result.fIsRRect || (stencilSettings && result.fAA != *aa) ||
670 (!result.fRRect.isRect() && !simpleColor)) {
671 // The clip and draw state are too complicated to try and reduce
672 conservativeCrop();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000673 return QuadOptimization::kCropped;
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400674 } // Else fall through to attempt to combine the draw and clip geometry together
675 break;
676 default:
677 SkUNREACHABLE;
678 }
679
680 // If we reached here, we know we're an axis-aligned clip that is either a rect or a round rect,
681 // so we can potentially combine it with the draw geometry so that no clipping is needed.
682 SkASSERT(result.fEffect == GrClip::Effect::kClipped && result.fIsRRect);
683 SkRect clippedBounds = result.fRRect.getBounds();
684 clippedBounds.intersect(rtRect);
685 if (result.fRRect.isRect()) {
686 // No rounded corners, so we might be able to become a native clear or we might be able to
687 // modify geometry and edge flags to represent intersected shape of clip and draw.
688 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
689 /*compute local*/ !constColor)) {
690 if (simpleColor && quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
691 // Clear optimization is possible
692 drawBounds = quad->fDevice.bounds();
693 if (drawBounds.contains(rtRect)) {
694 // Fullscreen clear
695 this->clear(*constColor);
696 return QuadOptimization::kSubmitted;
697 } else if (GrClip::IsPixelAligned(drawBounds) &&
698 drawBounds.width() > 256 && drawBounds.height() > 256) {
699 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
700 SkIRect scissorRect;
701 drawBounds.round(&scissorRect);
702 this->clear(scissorRect, *constColor);
703 return QuadOptimization::kSubmitted;
704 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000705 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400706
707 // else the draw and clip were combined so just update the AA to reflect combination
708 if (*aa == GrAA::kNo && result.fAA == GrAA::kYes &&
709 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
710 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
711 // properly reflect the smooth edge of the clip.
712 *aa = GrAA::kYes;
713 }
714 // We intentionally do not downgrade AA here because we don't know if we need to
715 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
716 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
717 // deviceQuad is exactly the intersection of original quad and clip, so it can be
718 // drawn with no clip (submitted by caller)
719 return QuadOptimization::kClipApplied;
720 }
721 } else {
722 // Rounded corners and constant filled color (limit ourselves to solid colors because
723 // there is no way to use custom local coordinates with drawRRect).
724 SkASSERT(simpleColor);
725 if (GrQuadUtils::CropToRect(clippedBounds, result.fAA, quad,
726 /* compute local */ false) &&
727 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
728 quad->fDevice.bounds().contains(clippedBounds)) {
729 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
730 // we can draw the rrect directly and ignore the edge flags
731 GrPaint paint;
732 clear_to_grpaint(*constColor, &paint);
733 this->drawRRect(nullptr, std::move(paint), result.fAA, SkMatrix::I(), result.fRRect,
734 GrStyle::SimpleFill());
735 return QuadOptimization::kSubmitted;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000736 }
737 }
738
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400739 // The quads have been updated to better fit the clip bounds, but can't get rid of
740 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500741 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000742 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000743}
744
Michael Ludwig7c12e282020-05-29 09:54:07 -0400745void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500746 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500747 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500748 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400749 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000750 ASSERT_SINGLE_OWNER
751 RETURN_IF_ABANDONED
752 SkDEBUGCODE(this->validate();)
753 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500754
Michael Ludwig61328202019-06-19 14:48:58 +0000755 AutoCheckFlush acf(this->drawingManager());
756
757 SkPMColor4f* constColor = nullptr;
758 SkPMColor4f paintColor;
John Stiles41d91b62020-07-21 14:39:40 -0400759 if (!ss && !paint.hasCoverageFragmentProcessor() && paint.isConstantBlendedColor(&paintColor)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000760 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
761 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500762 }
763
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500764 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000765 if (opt >= QuadOptimization::kClipApplied) {
766 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400767 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000768 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
769 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500770 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
771 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700772 }
Michael Ludwig61328202019-06-19 14:48:58 +0000773 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800774}
775
Michael Ludwig7c12e282020-05-29 09:54:07 -0400776void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400777 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500778 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000779 sk_sp<GrColorSpaceXform> textureXform,
780 GrSamplerState::Filter filter,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400781 GrSamplerState::MipmapMode mm,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000782 const SkPMColor4f& color,
783 SkBlendMode blendMode,
784 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500785 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400786 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000787 ASSERT_SINGLE_OWNER
788 RETURN_IF_ABANDONED
789 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400790 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000791 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
792
793 AutoCheckFlush acf(this->drawingManager());
794
795 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
796 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500797 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000798
799 SkASSERT(opt != QuadOptimization::kSubmitted);
800 if (opt != QuadOptimization::kDiscarded) {
801 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400802 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000803 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400804 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400805 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
806 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400807 // Use the provided subset, although hypothetically we could detect that the cropped local
808 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500809 this->addDrawOp(finalClip,
810 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400811 std::move(textureXform), filter, mm, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400812 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000813 }
814}
815
Michael Ludwig7c12e282020-05-29 09:54:07 -0400816void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500817 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500818 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400819 const SkMatrix& viewMatrix,
820 const SkRect& rect,
821 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700822 if (!style) {
823 style = &GrStyle::SimpleFill();
824 }
joshualitt1de610a2016-01-06 08:26:09 -0800825 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700826 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700827 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400828 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700829
bsalomon6663acf2016-05-10 09:14:17 -0700830 // Path effects should've been devolved to a path in SkGpuDevice
831 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700832
Robert Phillips72152832017-01-25 17:31:35 -0500833 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700834
bsalomon6663acf2016-05-10 09:14:17 -0700835 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400836 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000837 // Fills the rect, using rect as its own local coordinates
838 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500839 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400840 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
841 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
842 (rect.width() && rect.height())) {
843 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
844 // GrStyledShape to handle stroke caps and dashing properly.
Chris Dalton7d6748e2019-03-13 00:34:52 -0600845 GrAAType aaType = this->chooseAAType(aa);
Herb Derbyc76d4092020-10-07 16:46:15 -0400846 GrOp::Owner op = GrStrokeRectOp::Make(
847 fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500848 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
849 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500850 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400851 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700852 return;
robertphillips4bc31812016-03-01 12:22:49 -0800853 }
robertphillips4bc31812016-03-01 12:22:49 -0800854 }
Mike Klein16885072018-12-11 09:54:31 -0500855 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000856 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
857 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700858}
859
Michael Ludwig7c12e282020-05-29 09:54:07 -0400860void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500861 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
862 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600863 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500864
865 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
866 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500867}
868
Brian Salomon70fe17e2020-11-30 14:33:58 -0500869int GrRenderTargetContext::maxWindowRectangles() const {
870 return this->asRenderTargetProxy()->maxWindowRectangles(*this->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500871}
872
Robert Phillips24e2f6e2020-06-26 08:30:07 -0400873GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear() const {
Chris Dalton6b982802019-06-27 13:53:46 -0600874#if GR_TEST_UTILS
875 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400876 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600877 }
878#endif
879 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
880 // would normally be overwritten. The one exception is if the render target context is marked as
881 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
882 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
883 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
884 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400885 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600886}
887
Chris Dalton858cf232019-10-14 16:20:00 -0600888void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600889 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600890 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600891 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600892
Chris Daltoneffee202019-07-01 22:28:03 -0600893 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600894 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500895 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600896 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400897 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600898 }
899 SkASSERT(numRequiredSamples > 0);
900
901 if (numRequiredSamples > fNumStencilSamples) {
902 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500903 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600904 }
Chris Dalton6b982802019-06-27 13:53:46 -0600905
Chris Dalton674f77a2019-09-30 20:49:39 -0600906 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600907 if (this->caps()->performStencilClearsAsDraws()) {
908 // There is a driver bug with clearing stencil. We must use an op to manually clear the
909 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400910 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600911 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600912 this->getOpsTask()->setInitialStencilContent(
913 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600914 }
915 }
916}
917
Michael Ludwig81d41722020-05-26 16:57:38 -0400918void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600919 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
920
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400921 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
922 if (scissor && !scissorState.set(*scissor)) {
923 // The requested clear region is off screen, so nothing to do.
924 return;
925 }
926
Michael Ludwig1e632792020-05-21 12:45:31 -0400927 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400928 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400929 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500930 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500931
932 // Configure the paint to have no impact on the color buffer
933 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400934 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400935 this->addDrawOp(nullptr,
936 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
937 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500938 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400939 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500940 }
robertphillips976f5f02016-06-03 10:59:20 -0700941}
942
Brian Salomon70fe17e2020-11-30 14:33:58 -0500943void GrRenderTargetContext::stencilPath(const GrHardClip* clip,
944 GrAA doStencilMSAA,
945 const SkMatrix& viewMatrix,
946 sk_sp<const GrPath> path) {
947 ASSERT_SINGLE_OWNER
948 RETURN_IF_ABANDONED
949 SkDEBUGCODE(this->validate();)
950 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "stencilPath", fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500951
Brian Salomon467921e2017-03-06 16:17:12 -0500952 // TODO: extract portions of checkDraw that are relevant to path stenciling.
953 SkASSERT(path);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500954 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
Brian Salomon467921e2017-03-06 16:17:12 -0500955
956 // FIXME: Use path bounds instead of this WAR once
957 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500958 SkIRect bounds = SkIRect::MakeSize(this->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -0500959
Michael Ludwig7c12e282020-05-29 09:54:07 -0400960 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
961 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
962 // during this clip application.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500963 GrAppliedHardClip appliedClip(this->dimensions(),
964 this->asSurfaceProxy()->backingStoreDimensions());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400965
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400966 if (clip && GrClip::Effect::kClippedOut == clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500967 return;
968 }
Michael Ludwig7c12e282020-05-29 09:54:07 -0400969 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
970 // but as it is, we're just using the full render target so intersecting the two bounds would
971 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -0500972
Brian Salomon70fe17e2020-11-30 14:33:58 -0500973 GrOp::Owner op = GrStencilPathOp::Make(fContext,
Herb Derbyc76d4092020-10-07 16:46:15 -0400974 viewMatrix,
Brian Salomon70fe17e2020-11-30 14:33:58 -0500975 doStencilMSAA == GrAA::kYes,
Herb Derbyc76d4092020-10-07 16:46:15 -0400976 appliedClip.hasStencilClip(),
977 appliedClip.scissorState(),
978 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400979 if (!op) {
980 return;
981 }
Michael Ludwig6397e802020-08-05 15:50:01 -0400982 op->setClippedBounds(SkRect::Make(bounds));
Chris Dalton6b982802019-06-27 13:53:46 -0600983
Brian Salomon70fe17e2020-11-30 14:33:58 -0500984 this->setNeedsStencil(GrAA::kYes == doStencilMSAA);
985 this->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700986}
987
Brian Salomone69b9ef2020-07-22 11:18:06 -0400988void GrRenderTargetContext::drawTextureSet(const GrClip* clip,
989 TextureSetEntry set[],
990 int cnt,
991 int proxyRunCnt,
992 GrSamplerState::Filter filter,
993 GrSamplerState::MipmapMode mm,
994 SkBlendMode mode,
995 GrAA aa,
996 SkCanvas::SrcRectConstraint constraint,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400997 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500998 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400999 ASSERT_SINGLE_OWNER
1000 RETURN_IF_ABANDONED
1001 SkDEBUGCODE(this->validate();)
1002 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001003
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001004 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1005 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1006 AutoCheckFlush acf(this->drawingManager());
1007 GrAAType aaType = this->chooseAAType(aa);
1008 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1009 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1010 : GrTextureOp::Saturate::kNo;
Brian Salomone69b9ef2020-07-22 11:18:06 -04001011 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, mm, saturate,
Michael Ludwig379e4962019-12-06 13:21:26 -05001012 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001013}
1014
Michael Ludwig7c12e282020-05-29 09:54:07 -04001015void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001016 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001017 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001018 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001019 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001020 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001021 ASSERT_SINGLE_OWNER
1022 RETURN_IF_ABANDONED
1023 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001024 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001025
1026 AutoCheckFlush acf(this->drawingManager());
1027
1028 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001029 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001030 GrOp::Owner op =
Brian Osman449b1152020-04-15 16:43:00 -04001031 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001032 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001033 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001034 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001035}
1036
1037///////////////////////////////////////////////////////////////////////////////
1038
Michael Ludwig7c12e282020-05-29 09:54:07 -04001039void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001040 GrPaint&& paint,
1041 const SkMatrix& viewMatrix,
1042 int spriteCount,
1043 const SkRSXform xform[],
1044 const SkRect texRect[],
1045 const SkColor colors[]) {
1046 ASSERT_SINGLE_OWNER
1047 RETURN_IF_ABANDONED
1048 SkDEBUGCODE(this->validate();)
1049 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1050
1051 AutoCheckFlush acf(this->drawingManager());
1052
1053 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001054 GrOp::Owner op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1055 aaType, spriteCount, xform, texRect, colors);
Brian Osman4d92b892019-03-24 00:53:23 +00001056 this->addDrawOp(clip, std::move(op));
1057}
1058
1059///////////////////////////////////////////////////////////////////////////////
1060
Michael Ludwig7c12e282020-05-29 09:54:07 -04001061void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001062 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001063 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001064 const SkMatrix& viewMatrix,
1065 const SkRRect& rrect,
1066 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001067 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001068 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001069 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001070 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001071
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001072 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1073
Robert Phillips85290802018-07-02 13:14:28 -04001074 const SkStrokeRec& stroke = style.strokeRec();
1075 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001076 return;
1077 }
1078
Michael Ludwig7c12e282020-05-29 09:54:07 -04001079 const GrClip* clip = origClip;
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001080 // It is not uncommon to clip to a round rect and then draw that same round rect. Since our
1081 // lower level clip code works from op bounds, which are SkRects, it doesn't detect that the
1082 // clip can be ignored. The following test attempts to mitigate the stencil clip cost but only
1083 // works for axis-aligned round rects. This also only works for filled rrects since the stroke
1084 // width outsets beyond the rrect itself.
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001085 // TODO: skbug.com/10462 - There was mixed performance wins and regressions when this
1086 // optimization was turned on outside of Android Framework. I (michaelludwig) believe this is
1087 // do to the overhead in determining if an SkClipStack is just a rrect. Once that is improved,
1088 // re-enable this and see if we avoid the regressions.
1089#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
bsalomon7f0d9f32016-08-15 14:49:10 -07001090 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001091 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001092 rrect.transform(viewMatrix, &devRRect)) {
Michael Ludwig6397e802020-08-05 15:50:01 -04001093 GrClip::PreClipResult result = clip->preApply(devRRect.getBounds(), aa);
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001094 switch(result.fEffect) {
1095 case GrClip::Effect::kClippedOut:
1096 return;
1097 case GrClip::Effect::kUnclipped:
1098 clip = nullptr;
1099 break;
1100 case GrClip::Effect::kClipped:
1101 // Currently there's no general-purpose rrect-to-rrect contains function, and if we
1102 // got here, we know the devRRect's bounds aren't fully contained by the clip.
1103 // Testing for equality between the two is a reasonable stop-gap for now.
1104 if (result.fIsRRect && result.fRRect == devRRect) {
1105 // NOTE: On the android framework, we allow this optimization even when the clip
1106 // is non-AA and the draw is AA.
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001107 if (result.fAA == aa || (result.fAA == GrAA::kNo && aa == GrAA::kYes)) {
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001108 clip = nullptr;
1109 }
1110 }
1111 break;
1112 default:
1113 SkUNREACHABLE;
1114 }
1115 }
Michael Ludwig1bf2b4f2020-07-24 15:22:25 -04001116#endif
ksakamotoec7f2ac2016-07-05 03:54:53 -07001117
Robert Phillips72152832017-01-25 17:31:35 -05001118 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001119
Chris Dalton7d6748e2019-03-13 00:34:52 -06001120 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001121
Herb Derbyc76d4092020-10-07 16:46:15 -04001122 GrOp::Owner op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001123 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1124 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1125 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1126 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1127 // to avoid perf regressions on some platforms.
1128 assert_alive(paint);
1129 op = GrOvalOpFactory::MakeCircularRRectOp(
1130 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1131 }
1132 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001133 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001134 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001135 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001136 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001137 assert_alive(paint);
1138 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001139 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001140 }
1141 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001142 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001143 return;
robertphillipsea461502015-05-26 11:38:03 -07001144 }
robertphillipsb56f9272016-02-25 11:03:52 -08001145
Mike Klein16885072018-12-11 09:54:31 -05001146 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001147 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001148 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001149}
1150
Jim Van Verthc5903412016-11-17 15:27:09 -05001151///////////////////////////////////////////////////////////////////////////////
1152
Jim Van Verth3af1af92017-05-18 15:06:54 -04001153static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1154 SkPoint3 result;
1155 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1156 result.fZ = pt.fZ;
1157 return result;
1158}
1159
Michael Ludwig7c12e282020-05-29 09:54:07 -04001160bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001161 const SkMatrix& viewMatrix,
1162 const SkPath& path,
1163 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001164 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001165 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001166 return true;
1167 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001168 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001169 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001170
1171 // check z plane
1172 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1173 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1174 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1175 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1176 return false;
1177 }
1178
1179 SkRRect rrect;
1180 SkRect rect;
1181 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001182 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001183 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1184 if (!isRRect &&
1185 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1186 rect.width() > SK_ScalarNearlyZero) {
1187 rrect.setOval(rect);
1188 isRRect = true;
1189 }
1190 if (!isRRect && path.isRect(&rect)) {
1191 rrect.setRect(rect);
1192 isRRect = true;
1193 }
1194
1195 if (!isRRect) {
1196 return false;
1197 }
1198
Jim Van Verthc5903412016-11-17 15:27:09 -05001199 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001200 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001201 }
1202
Robert Phillips72152832017-01-25 17:31:35 -05001203 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001204
Jim Van Verth3af1af92017-05-18 15:06:54 -04001205 // transform light
1206 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1207
1208 // 1/scale
1209 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001210 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001211 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1212 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1213
1214 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001215 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1216
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001217 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001218 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1219 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1220 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001221
1222 // Outset the shadow rrect to the border of the penumbra
1223 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1224 SkRRect ambientRRect;
1225 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1226 // If the rrect was an oval then its outset will also be one.
1227 // We set it explicitly to avoid errors.
1228 if (rrect.isOval()) {
1229 ambientRRect = SkRRect::MakeOval(outsetRect);
1230 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001231 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001232 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1233 }
1234
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001235 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001236 if (transparent) {
1237 // set a large inset to force a fill
1238 devSpaceInsetWidth = ambientRRect.width();
1239 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001240
Herb Derbyc76d4092020-10-07 16:46:15 -04001241 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1242 ambientColor,
1243 viewMatrix,
1244 ambientRRect,
1245 devSpaceAmbientBlur,
1246 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001247 if (op) {
1248 this->addDrawOp(clip, std::move(op));
1249 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001250 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001251
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001252 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001253 SkScalar devSpaceSpotBlur;
1254 SkScalar spotScale;
1255 SkVector spotOffset;
1256 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1257 devLightPos.fZ, rec.fLightRadius,
1258 &devSpaceSpotBlur, &spotScale, &spotOffset);
1259 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001260 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1261
Jim Van Verth3af1af92017-05-18 15:06:54 -04001262 // Adjust translate for the effect of the scale.
1263 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1264 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1265 // This offset is in dev space, need to transform it into source space.
1266 SkMatrix ctmInverse;
1267 if (viewMatrix.invert(&ctmInverse)) {
1268 ctmInverse.mapPoints(&spotOffset, 1);
1269 } else {
1270 // Since the matrix is a similarity, this should never happen, but just in case...
1271 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1272 SkASSERT(false);
1273 }
1274
1275 // Compute the transformed shadow rrect
1276 SkRRect spotShadowRRect;
1277 SkMatrix shadowTransform;
1278 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1279 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001280 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001281
1282 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001283 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001284 SkScalar insetWidth = blurOutset;
1285 if (transparent) {
1286 // If transparent, just do a fill
1287 insetWidth += spotShadowRRect.width();
1288 } else {
1289 // For shadows, instead of using a stroke we specify an inset from the penumbra
1290 // border. We want to extend this inset area so that it meets up with the caster
1291 // geometry. The inset geometry will by default already be inset by the blur width.
1292 //
1293 // We compare the min and max corners inset by the radius between the original
1294 // rrect and the shadow rrect. The distance between the two plus the difference
1295 // between the scaled radius and the original radius gives the distance from the
1296 // transformed shadow shape to the original shape in that corner. The max
1297 // of these gives the maximum distance we need to cover.
1298 //
1299 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1300 // that to get the full insetWidth.
1301 SkScalar maxOffset;
1302 if (rrect.isRect()) {
1303 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001304 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001305 rrect.rect().fLeft),
1306 SkTAbs(spotShadowRRect.rect().fTop -
1307 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001308 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001309 rrect.rect().fRight),
1310 SkTAbs(spotShadowRRect.rect().fBottom -
1311 rrect.rect().fBottom)));
1312 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001313 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001314 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1315 rrect.rect().fLeft + dr,
1316 spotShadowRRect.rect().fTop -
1317 rrect.rect().fTop + dr);
1318 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1319 rrect.rect().fRight - dr,
1320 spotShadowRRect.rect().fBottom -
1321 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001322 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001323 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001324 }
Brian Osman788b9162020-02-07 10:36:46 -05001325 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001326 }
1327
1328 // Outset the shadow rrect to the border of the penumbra
1329 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1330 if (spotShadowRRect.isOval()) {
1331 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1332 } else {
1333 SkScalar outsetRad = spotRadius + blurOutset;
1334 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1335 }
1336
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001337 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001338
Herb Derbyc76d4092020-10-07 16:46:15 -04001339 GrOp::Owner op = GrShadowRRectOp::Make(fContext,
1340 spotColor,
1341 viewMatrix,
1342 spotShadowRRect,
1343 2.0f * devSpaceSpotBlur,
1344 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001345 if (op) {
1346 this->addDrawOp(clip, std::move(op));
1347 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001348 }
1349
1350 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001351}
1352
1353///////////////////////////////////////////////////////////////////////////////
1354
Michael Ludwig7c12e282020-05-29 09:54:07 -04001355bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001356 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001357 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001358 const SkMatrix& viewMatrix,
1359 const SkRRect& origOuter,
1360 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001361 SkASSERT(!origInner.isEmpty());
1362 SkASSERT(!origOuter.isEmpty());
1363
Brian Salomon65749212017-12-01 16:01:47 -05001364 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1365
Chris Dalton7d6748e2019-03-13 00:34:52 -06001366 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001367
1368 if (GrAAType::kMSAA == aaType) {
1369 return false;
1370 }
1371
Greg Daniel2655ede2019-04-10 00:49:28 +00001372 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1373 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001374 auto outerR = outer->width() / 2.f;
1375 auto innerR = inner->width() / 2.f;
1376 auto cx = outer->getBounds().fLeft + outerR;
1377 auto cy = outer->getBounds().fTop + outerR;
1378 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1379 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1380 auto avgR = (innerR + outerR) / 2.f;
1381 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1382 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1383 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001384 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001385 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001386 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001387 if (op) {
1388 this->addDrawOp(clip, std::move(op));
1389 return true;
1390 }
Mike Klein16885072018-12-11 09:54:31 -05001391 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001392 }
1393 }
1394
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001395 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001396 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001397 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1398 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001399 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001400 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1401 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001402 }
robertphillips00095892016-02-29 13:50:40 -08001403
robertphillips00095892016-02-29 13:50:40 -08001404 SkMatrix inverseVM;
1405 if (!viewMatrix.isIdentity()) {
1406 if (!origInner.transform(viewMatrix, inner.writable())) {
1407 return false;
1408 }
1409 if (!origOuter.transform(viewMatrix, outer.writable())) {
1410 return false;
1411 }
1412 if (!viewMatrix.invert(&inverseVM)) {
1413 return false;
1414 }
1415 } else {
1416 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001417 }
robertphillips00095892016-02-29 13:50:40 -08001418
Ethan Nicholaseace9352018-10-15 20:09:54 +00001419 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001420 // TODO these need to be a geometry processors
John Stiles851b90e2020-06-17 13:53:37 -04001421 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, innerEdgeType, *inner, caps);
1422 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001423 return false;
1424 }
1425
John Stiles851b90e2020-06-17 13:53:37 -04001426 std::tie(success, fp) = GrRRectEffect::Make(std::move(fp), outerEdgeType, *outer, caps);
1427 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001428 return false;
1429 }
1430
John Stiles41d91b62020-07-21 14:39:40 -04001431 paint.setCoverageFragmentProcessor(std::move(fp));
robertphillips00095892016-02-29 13:50:40 -08001432
1433 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001434 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001435 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1436 }
halcanary9d524f22016-03-29 09:03:52 -07001437
Brian Salomon82f44312017-01-11 13:42:54 -05001438 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1439 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001440 return true;
1441}
1442
Michael Ludwig7c12e282020-05-29 09:54:07 -04001443void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001444 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001445 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001446 const SkMatrix& viewMatrix,
1447 const SkRRect& outer,
1448 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001449 ASSERT_SINGLE_OWNER
1450 RETURN_IF_ABANDONED
1451 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001452 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001453
1454 SkASSERT(!outer.isEmpty());
1455 SkASSERT(!inner.isEmpty());
1456
Robert Phillips72152832017-01-25 17:31:35 -05001457 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001458
Brian Salomon82f44312017-01-11 13:42:54 -05001459 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001460 return;
1461 }
Mike Klein16885072018-12-11 09:54:31 -05001462 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001463
1464 SkPath path;
1465 path.setIsVolatile(true);
1466 path.addRRect(inner);
1467 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001468 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001469 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001470}
1471
robertphillipsea461502015-05-26 11:38:03 -07001472///////////////////////////////////////////////////////////////////////////////
1473
Michael Ludwig7c12e282020-05-29 09:54:07 -04001474void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001475 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001476 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001477 const SkMatrix& viewMatrix,
1478 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001479 const GrStyle& style,
1480 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001481 ASSERT_SINGLE_OWNER
1482 RETURN_IF_ABANDONED
1483 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001484 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001485
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001486 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001487 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001488 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001489 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001490 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1491 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001492 aa = GrAA::kNo;
1493 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001494 }
msarettcc319b92016-08-25 18:07:18 -07001495 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001496 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001497 SkPath path;
1498 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001499 path.setIsVolatile(true);
1500
Brian Salomon82f44312017-01-11 13:42:54 -05001501 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001502 }
1503
Chris Dalton7d6748e2019-03-13 00:34:52 -06001504 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Herb Derbyc76d4092020-10-07 16:46:15 -04001505 GrOp::Owner op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1506 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001507 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001508}
1509
Michael Ludwig7c12e282020-05-29 09:54:07 -04001510void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001511 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001512 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001513 const SkMatrix& viewMatrix,
1514 const SkRect& oval,
1515 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001516 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001517 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001518 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001519 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001520
Robert Phillips7484d202018-07-03 09:09:08 -04001521 const SkStrokeRec& stroke = style.strokeRec();
1522
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001523 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001524 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1525 return;
1526 }
1527
1528 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001529 return;
robertphillipsea461502015-05-26 11:38:03 -07001530 }
1531
Robert Phillips72152832017-01-25 17:31:35 -05001532 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001533
Chris Dalton7d6748e2019-03-13 00:34:52 -06001534 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001535
Herb Derbyc76d4092020-10-07 16:46:15 -04001536 GrOp::Owner op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001537 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1538 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001539 // We don't draw true circles as round rects in coverage mode, because it can
1540 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1541 assert_alive(paint);
1542 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1543 this->caps()->shaderCaps());
1544 }
1545 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001546 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1547 // the arc equation. This same special geometry and fragment branch also turn out to be a
1548 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1549 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1550 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001551 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001552 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1553 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001554 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001555 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001556 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001557 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1558 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001559 }
1560 if (op) {
1561 this->addDrawOp(clip, std::move(op));
1562 return;
robertphillipsea461502015-05-26 11:38:03 -07001563 }
robertphillipsb56f9272016-02-25 11:03:52 -08001564
Mike Klein16885072018-12-11 09:54:31 -05001565 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001566 this->drawShapeUsingPathRenderer(
1567 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001568 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001569}
1570
Michael Ludwig7c12e282020-05-29 09:54:07 -04001571void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001572 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001573 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001574 const SkMatrix& viewMatrix,
1575 const SkRect& oval,
1576 SkScalar startAngle,
1577 SkScalar sweepAngle,
1578 bool useCenter,
1579 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001580 ASSERT_SINGLE_OWNER
1581 RETURN_IF_ABANDONED
1582 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001583 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001584
1585 AutoCheckFlush acf(this->drawingManager());
1586
Chris Dalton7d6748e2019-03-13 00:34:52 -06001587 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001588 if (GrAAType::kCoverage == aaType) {
1589 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Herb Derbyc76d4092020-10-07 16:46:15 -04001590 GrOp::Owner op = GrOvalOpFactory::MakeArcOp(fContext,
1591 std::move(paint),
1592 viewMatrix,
1593 oval,
1594 startAngle,
1595 sweepAngle,
1596 useCenter,
1597 style,
1598 shaderCaps);
Greg Daniel2655ede2019-04-10 00:49:28 +00001599 if (op) {
1600 this->addDrawOp(clip, std::move(op));
1601 return;
1602 }
1603 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001604 }
Brian Salomone4949402018-04-26 15:22:04 -04001605 this->drawShapeUsingPathRenderer(
1606 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001607 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001608}
1609
Michael Ludwig7c12e282020-05-29 09:54:07 -04001610void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001611 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001612 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001613 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001614 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001615 sk_sp<GrColorSpaceXform> csxf,
1616 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001617 std::unique_ptr<SkLatticeIter> iter,
1618 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001619 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001620 RETURN_IF_ABANDONED
1621 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001622 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001623
Robert Phillips72152832017-01-25 17:31:35 -05001624 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001625
Herb Derbyc76d4092020-10-07 16:46:15 -04001626 GrOp::Owner op =
Greg Danieled96bca2019-12-05 15:05:54 -05001627 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001628 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001629 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001630}
1631
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001632void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1633 const SkRect& bounds) {
Herb Derbyc76d4092020-10-07 16:46:15 -04001634 GrOp::Owner op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001635 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001636 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001637}
1638
Greg Danielc64ee462017-06-15 16:59:49 -04001639bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniel414418d2020-07-08 11:44:25 -04001640 const GrBackendSemaphore waitSemaphores[],
1641 bool deleteSemaphoresAfterWait) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001642 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001643 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001644 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001645 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001646
1647 AutoCheckFlush acf(this->drawingManager());
1648
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001649 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001650 return false;
1651 }
1652
Robert Phillipsf8f45d92020-07-01 11:11:18 -04001653 auto direct = fContext->asDirectContext();
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001654 if (!direct) {
1655 return false;
1656 }
1657
1658 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001659
Greg Daniel414418d2020-07-08 11:44:25 -04001660 GrWrapOwnership ownership =
1661 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
1662
Greg Daniel301015c2019-11-18 14:06:46 -05001663 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1664 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001665 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001666 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -04001667 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001668 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001669 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1670 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001671 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001672}
joshualitt33a5fce2015-11-18 13:28:51 -08001673
Michael Ludwig7c12e282020-05-29 09:54:07 -04001674void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001675 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001676 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001677 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001678 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001679 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001680 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001681 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001682 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001683 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1684
Michael Ludwig2686d692020-04-17 20:21:37 +00001685 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001686
Robert Phillips27927a52018-08-20 13:18:12 -04001687 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001688}
1689
Michael Ludwig7c12e282020-05-29 09:54:07 -04001690void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04001691 GrPaint&& paint,
1692 GrAA aa,
1693 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001694 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04001695 ASSERT_SINGLE_OWNER
1696 RETURN_IF_ABANDONED
1697 SkDEBUGCODE(this->validate();)
1698 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1699
Brian Salomon2fad74a2017-12-20 13:28:55 -05001700 if (shape.isEmpty()) {
1701 if (shape.inverseFilled()) {
1702 this->drawPaint(clip, std::move(paint), viewMatrix);
1703 }
1704 return;
robertphillipsea461502015-05-26 11:38:03 -07001705 }
1706
Robert Phillips72152832017-01-25 17:31:35 -05001707 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001708
Brian Salomon2fad74a2017-12-20 13:28:55 -05001709 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06001710 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001711 SkRRect rrect;
1712 // We can ignore the starting point and direction since there is no path effect.
1713 bool inverted;
1714 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1715 if (rrect.isRect()) {
1716 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1717 &shape.style());
1718 return;
1719 } else if (rrect.isOval()) {
1720 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001721 return;
1722 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001723 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1724 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001725 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1726 viewMatrix.rectStaysRect()) {
1727 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1728 // the matrix to all the points individually rather than just to the rect
1729 SkRect rects[2];
1730 if (shape.asNestedRects(rects)) {
1731 // Concave AA paths are expensive - try to avoid them for special cases
Herb Derbyc76d4092020-10-07 16:46:15 -04001732 GrOp::Owner op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04001733 fContext, std::move(paint), viewMatrix, rects);
1734 if (op) {
1735 this->addDrawOp(clip, std::move(op));
1736 }
1737 // Returning here indicates that there is nothing to draw in this case.
1738 return;
1739 }
robertphillipsea461502015-05-26 11:38:03 -07001740 }
1741 }
robertphillips4bc31812016-03-01 12:22:49 -08001742
Michael Ludwig6a6de652020-04-30 20:16:36 -04001743 // If we get here in drawShape(), we definitely need to use path rendering
1744 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
1745 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07001746}
1747
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001748static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
1749 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
1750}
1751
Brian Salomon70fe17e2020-11-30 14:33:58 -05001752bool GrRenderTargetContext::drawAndStencilPath(const GrHardClip* clip,
1753 const GrUserStencilSettings* ss,
1754 SkRegion::Op op,
1755 bool invert,
1756 GrAA aa,
1757 const SkMatrix& viewMatrix,
1758 const SkPath& path) {
1759 ASSERT_SINGLE_OWNER
1760 RETURN_FALSE_IF_ABANDONED
1761 SkDEBUGCODE(this->validate();)
1762 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAndStencilPath", fContext);
robertphillips391395d2016-03-02 09:26:36 -08001763
1764 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001765 GrPaint paint;
1766 paint.setCoverageSetOpXPFactory(op, invert);
1767 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon70fe17e2020-11-30 14:33:58 -05001768 SkRect::Make(this->dimensions()));
robertphillips391395d2016-03-02 09:26:36 -08001769 return true;
1770 }
1771
Brian Salomon70fe17e2020-11-30 14:33:58 -05001772 AutoCheckFlush acf(this->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001773
1774 // An Assumption here is that path renderer would use some form of tweaking
1775 // the src color (either the input alpha or in the frag shader) to implement
1776 // aa. If we have some future driver-mojo path AA that can do the right
1777 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon70fe17e2020-11-30 14:33:58 -05001778 GrAAType aaType = this->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07001779 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001780
Brian Salomon70fe17e2020-11-30 14:33:58 -05001781 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001782
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001783 GrPaint paint;
1784 paint.setCoverageSetOpXPFactory(op, invert);
1785
Michael Ludwig2686d692020-04-17 20:21:37 +00001786 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001787 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001788 canDrawArgs.fCaps = this->caps();
1789 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08001790 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001791 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001792 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001793 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001794 canDrawArgs.fAAType = aaType;
Brian Salomon70fe17e2020-11-30 14:33:58 -05001795 SkASSERT(!this->wrapsVkSecondaryCB());
Greg Danielbe7fc462019-01-03 16:40:42 -05001796 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001797 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001798
1799 // Don't allow the SW renderer
Brian Salomon70fe17e2020-11-30 14:33:58 -05001800 GrPathRenderer* pr = this->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001801 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001802 if (!pr) {
1803 return false;
1804 }
1805
Brian Salomon70fe17e2020-11-30 14:33:58 -05001806 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomonf3569f02017-10-24 12:52:33 -04001807 std::move(paint),
1808 ss,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001809 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001810 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04001811 &clipConservativeBounds,
1812 &viewMatrix,
1813 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001814 aaType,
Brian Salomon70fe17e2020-11-30 14:33:58 -05001815 this->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001816 pr->drawPath(args);
1817 return true;
1818}
1819
Brian Salomon70fe17e2020-11-30 14:33:58 -05001820SkBudgeted GrRenderTargetContext::isBudgeted() const {
1821 ASSERT_SINGLE_OWNER
robertphillips714712b2016-08-04 06:20:45 -07001822
Brian Salomon70fe17e2020-11-30 14:33:58 -05001823 if (fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001824 return SkBudgeted::kNo;
1825 }
1826
Brian Salomon70fe17e2020-11-30 14:33:58 -05001827 SkDEBUGCODE(this->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001828
Brian Salomon70fe17e2020-11-30 14:33:58 -05001829 return this->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001830}
1831
Michael Ludwig7c12e282020-05-29 09:54:07 -04001832void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001833 GrPaint&& paint,
1834 GrAA aa,
1835 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04001836 const GrStyledShape& originalShape,
1837 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08001838 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001839 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001840 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1841
Jim Van Verthf86073a2018-10-02 13:05:38 -04001842 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1843 return;
1844 }
1845
Michael Ludwig6a6de652020-04-30 20:16:36 -04001846 if (attemptShapeFallback && originalShape.simplified()) {
1847 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
1848 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
1849 // we ought to try again instead of going right to path rendering.
1850 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
1851 return;
1852 }
1853
Michael Ludwig7c12e282020-05-29 09:54:07 -04001854 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06001855
Michael Ludwig2686d692020-04-17 20:21:37 +00001856 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06001857 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06001858
robertphillips68737822015-10-29 12:12:21 -07001859 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001860 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05001861 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07001862 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001863 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04001864 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001865 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001866 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001867 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001868
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001869 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001870 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001871 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001872 return;
1873 }
1874
Chris Dalton6ce447a2019-06-23 18:07:38 -06001875 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001876
1877 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1878 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1879 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1880
Brian Salomon2fad74a2017-12-20 13:28:55 -05001881 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001882 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001883 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1884 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001885 return;
1886 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001887 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001888 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001889 }
1890 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001891 if (canDrawArgs.fShape->style().applies()) {
1892 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1893 styleScale);
1894 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001895 return;
1896 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001897 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001898 // This time, allow SW renderer
1899 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1900 } else {
1901 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04001902#if GR_PATH_RENDERER_SPEW
1903 SkDebugf("falling back to: %s\n", pr->name());
1904#endif
bsalomon6663acf2016-05-10 09:14:17 -07001905 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001906 }
robertphillipsea461502015-05-26 11:38:03 -07001907
bsalomon8acedde2016-06-24 10:42:16 -07001908 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001909#ifdef SK_DEBUG
1910 SkDebugf("Unable to find path renderer compatible with path.\n");
1911#endif
1912 return;
1913 }
1914
Robert Phillips256c37b2017-03-01 14:32:46 -05001915 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001916 std::move(paint),
1917 &GrUserStencilSettings::kUnused,
1918 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001919 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001920 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001921 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001922 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06001923 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001924 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001925 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001926}
1927
Brian Salomon467921e2017-03-06 16:17:12 -05001928static void op_bounds(SkRect* bounds, const GrOp* op) {
1929 *bounds = op->bounds();
1930 if (op->hasZeroArea()) {
1931 if (op->hasAABloat()) {
1932 bounds->outset(0.5f, 0.5f);
1933 } else {
1934 // We don't know which way the particular GPU will snap lines or points at integer
1935 // coords. So we ensure that the bounds is large enough for either snap.
1936 SkRect before = *bounds;
1937 bounds->roundOut(bounds);
1938 if (bounds->fLeft == before.fLeft) {
1939 bounds->fLeft -= 1;
1940 }
1941 if (bounds->fTop == before.fTop) {
1942 bounds->fTop -= 1;
1943 }
1944 if (bounds->fRight == before.fRight) {
1945 bounds->fRight += 1;
1946 }
1947 if (bounds->fBottom == before.fBottom) {
1948 bounds->fBottom += 1;
1949 }
1950 }
1951 }
1952}
1953
Herb Derbyc76d4092020-10-07 16:46:15 -04001954void GrRenderTargetContext::addOp(GrOp::Owner op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04001955 GrDrawingManager* drawingMgr = this->drawingManager();
1956 this->getOpsTask()->addOp(drawingMgr,
1957 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06001958}
1959
Herb Derbyc76d4092020-10-07 16:46:15 -04001960void GrRenderTargetContext::addDrawOp(const GrClip* clip, GrOp::Owner op,
Brian Salomon348a0372018-10-31 10:42:18 -04001961 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001962 ASSERT_SINGLE_OWNER
Robert Phillips9eb00022020-06-30 15:30:12 -04001963 if (fContext->abandoned()) {
Brian Salomon348a0372018-10-31 10:42:18 -04001964 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001965 }
Herb Derbyc76d4092020-10-07 16:46:15 -04001966 GrDrawOp* drawOp = (GrDrawOp*)op.get();
robertphillips2e1e51f2015-10-15 08:01:48 -07001967 SkDEBUGCODE(this->validate();)
Herb Derbyc76d4092020-10-07 16:46:15 -04001968 SkDEBUGCODE(drawOp->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001969 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001970
Brian Salomon467921e2017-03-06 16:17:12 -05001971 // Setup clip
1972 SkRect bounds;
1973 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001974 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Herb Derbyc76d4092020-10-07 16:46:15 -04001975 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = drawOp->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06001976 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07001977 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06001978
Chris Dalton9acf6822019-11-12 11:52:40 -07001979 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06001980 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06001981 }
1982
Michael Ludwig7c12e282020-05-29 09:54:07 -04001983 bool skipDraw = false;
1984 if (clip) {
1985 // Have a complex clip, so defer to its early clip culling
Michael Ludwig6397e802020-08-05 15:50:01 -04001986 GrAAType aaType = usesHWAA ? GrAAType::kMSAA :
1987 (op->hasAABloat() ? GrAAType::kCoverage :
1988 GrAAType::kNone);
1989 skipDraw = clip->apply(fContext, this, aaType, usesUserStencilBits,
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001990 &appliedClip, &bounds) == GrClip::Effect::kClippedOut;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001991 } else {
1992 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwig4e3cab72020-06-30 11:12:46 -04001993 skipDraw = !bounds.intersect(this->asSurfaceProxy()->getBoundsRect());
Michael Ludwig7c12e282020-05-29 09:54:07 -04001994 }
1995
1996 if (skipDraw) {
Brian Salomon348a0372018-10-31 10:42:18 -04001997 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001998 }
1999
Chris Dalton9acf6822019-11-12 11:52:40 -07002000 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2001 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2002
2003 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2004 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2005 // that uses a stencil test when the stencil buffer is multisampled.)
2006 bool hasMixedSampledCoverage = (
2007 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002008 SkASSERT(!hasMixedSampledCoverage ||
2009 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002010
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002011 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Herb Derbyc76d4092020-10-07 16:46:15 -04002012 GrProcessorSet::Analysis analysis = drawOp->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002013 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2014
Michael Ludwig28e5f112020-06-09 10:57:24 -04002015 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2016 op->setClippedBounds(bounds);
2017
Greg Daniel524e28b2019-11-01 11:48:53 -04002018 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002019 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002020 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Brian Salomon348a0372018-10-31 10:42:18 -04002021 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002022 }
2023 }
2024
Greg Danielf41b2bd2019-08-22 16:19:24 -04002025 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002026 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002027 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002028 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002029 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -04002030 dstProxyView, GrTextureResolveManager(this->drawingManager()),
2031 *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002032}
2033
Michael Ludwig28e5f112020-06-09 10:57:24 -04002034bool GrRenderTargetContext::setupDstProxyView(const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002035 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002036 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2037 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2038 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002039 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002040 return false;
2041 }
2042
Greg Danield358cbe2020-09-11 09:33:54 -04002043 if (fDstSampleType == GrDstSampleType::kNone) {
2044 fDstSampleType = this->caps()->getDstSampleTypeForProxy(this->asRenderTargetProxy());
Brian Salomon467921e2017-03-06 16:17:12 -05002045 }
Greg Danield358cbe2020-09-11 09:33:54 -04002046 SkASSERT(fDstSampleType != GrDstSampleType::kNone);
2047
2048 if (GrDstSampleTypeDirectlySamplesDst(fDstSampleType)) {
2049 // The render target is a texture or input attachment, so we can read from it directly in
2050 // the shader. The XP will be responsible to detect this situation and request a texture
2051 // barrier.
2052 dstProxyView->setProxyView(this->readSurfaceView());
2053 dstProxyView->setOffset(0, 0);
2054 dstProxyView->setDstSampleType(fDstSampleType);
2055 return true;
2056 }
2057 SkASSERT(fDstSampleType == GrDstSampleType::kAsTextureCopy);
Brian Salomon467921e2017-03-06 16:17:12 -05002058
Greg Daniel3155f7f2020-01-16 16:54:26 -05002059 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002060 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2061 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002062 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002063 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002064
Michael Ludwig28e5f112020-06-09 10:57:24 -04002065 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002066 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002067 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2068 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2069 // centers during rasterization.
2070 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2071 conservativeDrawBounds.outset(1, 1);
2072 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002073 }
Brian Salomon467921e2017-03-06 16:17:12 -05002074
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002075 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002076 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002077 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002078 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002079 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002080 } else {
Eric Karl74480882017-04-03 14:49:05 -07002081 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002082 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002083 }
Brian Salomonc5243782020-04-02 12:50:34 -04002084 auto copy =
Brian Salomon7e67dca2020-07-21 09:27:25 -04002085 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipmapped::kNo,
Brian Salomonc5243782020-04-02 12:50:34 -04002086 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2087 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002088
Brian Salomonc5243782020-04-02 12:50:34 -04002089 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002090 dstProxyView->setOffset(dstOffset);
Greg Danield358cbe2020-09-11 09:33:54 -04002091 dstProxyView->setDstSampleType(fDstSampleType);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002092 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002093}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002094
Greg Daniel573312e2020-02-07 17:22:35 -05002095bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002096 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002097 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002098 SkIRect clippedSrcRect;
2099 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002100 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2101 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002102 return false;
2103 }
2104
2105 GrPaint paint;
2106 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002107
Greg Danield2ccbb52020-02-05 10:45:39 -05002108 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002109 if (!fp) {
2110 return false;
2111 }
John Stiles5933d7d2020-07-21 12:28:35 -04002112 paint.setColorFragmentProcessor(std::move(fp));
Greg Daniel46cfbc62019-06-07 11:43:30 -04002113
2114 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002115 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002116 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2117 clippedSrcRect.height()),
2118 SkRect::Make(clippedSrcRect));
2119 return true;
2120}