blob: fe3ef5d74f2534f544a0218e06caa16a2915438c [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"
11#include "include/gpu/GrBackendSemaphore.h"
Brian Salomonb16f30b2020-03-31 09:17:56 -040012#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrRecordingContext.h"
14#include "include/private/SkShadowFlags.h"
15#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040016#include "src/core/SkAutoPixmapStorage.h"
17#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkDrawShadowInfo.h"
19#include "src/core/SkGlyphRunPainter.h"
20#include "src/core/SkLatticeIter.h"
21#include "src/core/SkMatrixPriv.h"
22#include "src/core/SkRRectPriv.h"
23#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040024#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040026#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrBlurUtils.h"
28#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040029#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040030#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040032#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrDrawingManager.h"
34#include "src/gpu/GrFixedClip.h"
35#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonb16f30b2020-03-31 09:17:56 -040036#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040037#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrMemoryPool.h"
39#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040041#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrRenderTargetContextPriv.h"
43#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/GrStencilAttachment.h"
45#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"
Brian Salomon024bd002019-06-11 11:38:16 -040050#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040051#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000052#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040053#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/gpu/ops/GrAtlasTextOp.h"
55#include "src/gpu/ops/GrClearOp.h"
56#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "src/gpu/ops/GrDrawAtlasOp.h"
58#include "src/gpu/ops/GrDrawOp.h"
59#include "src/gpu/ops/GrDrawVerticesOp.h"
60#include "src/gpu/ops/GrDrawableOp.h"
61#include "src/gpu/ops/GrFillRRectOp.h"
62#include "src/gpu/ops/GrFillRectOp.h"
63#include "src/gpu/ops/GrLatticeOp.h"
64#include "src/gpu/ops/GrOp.h"
65#include "src/gpu/ops/GrOvalOpFactory.h"
66#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050067#include "src/gpu/ops/GrShadowRRectOp.h"
68#include "src/gpu/ops/GrStencilPathOp.h"
69#include "src/gpu/ops/GrStrokeRectOp.h"
70#include "src/gpu/ops/GrTextureOp.h"
71#include "src/gpu/text/GrTextContext.h"
72#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040073
Herb Derbyc1b482c2018-08-09 15:02:27 -040074class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040075public:
76 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040077 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040078 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040079 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040081
Robert Phillips7c525e62018-06-12 10:11:12 -040082 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040083 fRenderTargetContext->addDrawOp(clip, std::move(op));
84 }
85
Robert Phillips46a13382018-08-23 13:53:01 -040086 void drawShape(const GrClip& clip, const SkPaint& paint,
87 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040088 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
89 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040090 }
91
92 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040093 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050094 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040095 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040096 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040097 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040098 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040099 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400100 }
101 }
102
Robert Phillips69893702019-02-22 11:16:30 -0500103 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400104 return fRenderTargetContext->fContext;
105 }
106
Herb Derby65956872018-08-21 16:55:04 -0400107 SkGlyphRunListPainter* glyphPainter() override {
108 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400109 }
110
Brian Salomonf18b1d82017-10-27 11:30:49 -0400111private:
112 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400113 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400114
Brian Salomonf18b1d82017-10-27 11:30:49 -0400115};
joshualittbc907352016-01-13 06:45:40 -0800116
Robert Phillips72152832017-01-25 17:31:35 -0500117#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800118#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400119 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800120#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400121 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500122#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
123#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
124#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
125#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
126#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700127
Brian Salomone225b562017-06-14 13:00:03 -0400128//////////////////////////////////////////////////////////////////////////////
129
robertphillipsea461502015-05-26 11:38:03 -0700130class AutoCheckFlush {
131public:
halcanary9d524f22016-03-29 09:03:52 -0700132 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700133 SkASSERT(fDrawingManager);
134 }
bsalomonb77a9072016-09-07 10:02:04 -0700135 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700136
137private:
robertphillips77a2e522015-10-17 07:43:27 -0700138 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700139};
140
Greg Daniele20fcad2020-01-08 11:52:34 -0500141std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
142 GrRecordingContext* context,
143 GrColorType colorType,
144 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500145 sk_sp<GrSurfaceProxy> proxy,
146 GrSurfaceOrigin origin,
147 const SkSurfaceProps* surfaceProps,
148 bool managedOps) {
149 if (!proxy) {
150 return nullptr;
151 }
152
153 const GrBackendFormat& format = proxy->backendFormat();
154 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
155 GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(format, colorType);
156
Greg Daniel3912a4b2020-01-14 09:56:04 -0500157 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
158 GrSurfaceProxyView outputView(std::move(proxy), origin, outSwizzle);
159
160 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
161 std::move(outputView), colorType,
162 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500163}
164
165std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
166 GrRecordingContext* context,
167 GrColorType colorType,
168 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500169 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500170 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500171 const GrBackendFormat& format,
172 int sampleCnt,
173 GrMipMapped mipMapped,
174 GrProtected isProtected,
175 GrSurfaceOrigin origin,
176 SkBudgeted budgeted,
177 const SkSurfaceProps* surfaceProps) {
178 // It is probably not necessary to check if the context is abandoned here since uses of the
179 // GrRenderTargetContext which need the context will mostly likely fail later on without an
180 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
181 // an abandoned context correctly. It also lets us early out of some extra work.
182 if (context->priv().abandoned()) {
183 return nullptr;
184 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500185
Greg Daniele20fcad2020-01-08 11:52:34 -0500186 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400187 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500188 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500189 if (!proxy) {
190 return nullptr;
191 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500192
Greg Danielba0ff782020-01-07 15:42:57 -0500193 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
194 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500195 if (!rtc) {
196 return nullptr;
197 }
198 rtc->discard();
199 return rtc;
200}
201
202std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
203 GrRecordingContext* context,
204 GrColorType colorType,
205 sk_sp<SkColorSpace> colorSpace,
206 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500207 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500208 int sampleCnt,
209 GrMipMapped mipMapped,
210 GrProtected isProtected,
211 GrSurfaceOrigin origin,
212 SkBudgeted budgeted,
213 const SkSurfaceProps* surfaceProps) {
214 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
215 if (!format.isValid()) {
216 return nullptr;
217 }
218
219 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
220 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
221 surfaceProps);
222}
223
224static inline GrColorType color_type_fallback(GrColorType ct) {
225 switch (ct) {
226 // kRGBA_8888 is our default fallback for many color types that may not have renderable
227 // backend formats.
228 case GrColorType::kAlpha_8:
229 case GrColorType::kBGR_565:
230 case GrColorType::kABGR_4444:
231 case GrColorType::kBGRA_8888:
232 case GrColorType::kRGBA_1010102:
233 case GrColorType::kRGBA_F16:
234 case GrColorType::kRGBA_F16_Clamped:
235 return GrColorType::kRGBA_8888;
236 case GrColorType::kAlpha_F16:
237 return GrColorType::kRGBA_F16;
238 case GrColorType::kGray_8:
239 return GrColorType::kRGB_888x;
240 default:
241 return GrColorType::kUnknown;
242 }
243}
244
Brian Salomonb16f30b2020-03-31 09:17:56 -0400245std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
246 GrImageContext* context, GrColorType colorType) {
247 do {
248 auto format =
249 context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
250 if (format.isValid()) {
251 return {colorType, format};
252 }
253 colorType = color_type_fallback(colorType);
254 } while (colorType != GrColorType::kUnknown);
255 return {GrColorType::kUnknown, {}};
256}
257
Greg Daniele20fcad2020-01-08 11:52:34 -0500258std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
259 GrRecordingContext* context,
260 GrColorType colorType,
261 sk_sp<SkColorSpace> colorSpace,
262 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500263 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500264 int sampleCnt,
265 GrMipMapped mipMapped,
266 GrProtected isProtected,
267 GrSurfaceOrigin origin,
268 SkBudgeted budgeted,
269 const SkSurfaceProps* surfaceProps) {
Brian Salomonb16f30b2020-03-31 09:17:56 -0400270 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType);
271 if (ct == GrColorType::kUnknown) {
272 return nullptr;
273 }
274 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
275 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500276}
277
Greg Danielba0ff782020-01-07 15:42:57 -0500278std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
279 GrRecordingContext* context,
280 GrColorType colorType,
281 sk_sp<SkColorSpace> colorSpace,
282 const GrBackendTexture& tex,
283 int sampleCnt,
284 GrSurfaceOrigin origin,
285 const SkSurfaceProps* surfaceProps,
286 ReleaseProc releaseProc,
287 ReleaseContext releaseCtx) {
288 SkASSERT(sampleCnt > 0);
289 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400290 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500291 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500292 if (!proxy) {
293 return nullptr;
294 }
295
296 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
297 origin, surfaceProps);
298}
299
300std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
301 GrRecordingContext* context,
302 GrColorType colorType,
303 sk_sp<SkColorSpace> colorSpace,
304 const GrBackendTexture& tex,
305 int sampleCnt,
306 GrSurfaceOrigin origin,
307 const SkSurfaceProps* surfaceProps) {
308 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400309 sk_sp<GrSurfaceProxy> proxy(
310 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500311 if (!proxy) {
312 return nullptr;
313 }
314
315 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
316 origin, surfaceProps);
317}
318
319std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
320 GrRecordingContext* context,
321 GrColorType colorType,
322 sk_sp<SkColorSpace> colorSpace,
323 const GrBackendRenderTarget& rt,
324 GrSurfaceOrigin origin,
325 const SkSurfaceProps* surfaceProps,
326 ReleaseProc releaseProc,
327 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400328 sk_sp<GrSurfaceProxy> proxy(
329 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500330 if (!proxy) {
331 return nullptr;
332 }
333
334 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
335 origin, surfaceProps);
336}
337
338std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
339 GrRecordingContext* context,
340 const SkImageInfo& imageInfo,
341 const GrVkDrawableInfo& vkInfo,
342 const SkSurfaceProps* props) {
343 sk_sp<GrSurfaceProxy> proxy(
344 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
345 vkInfo));
346 if (!proxy) {
347 return nullptr;
348 }
349
Greg Danielba0ff782020-01-07 15:42:57 -0500350 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
351 imageInfo.refColorSpace(), std::move(proxy),
352 kTopLeft_GrSurfaceOrigin, props);
353}
354
Greg Danielf41b2bd2019-08-22 16:19:24 -0400355// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
356// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
357// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
358// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500359GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500360 GrSurfaceProxyView readView,
361 GrSurfaceProxyView outputView,
Brian Salomond6287472019-06-24 15:50:07 -0400362 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400363 sk_sp<SkColorSpace> colorSpace,
364 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400365 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500366 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
367 std::move(colorSpace))
368 , fOutputView(std::move(outputView))
369 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400370 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400371 , fManagedOpsTask(managedOpsTask) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500372 SkASSERT(this->asSurfaceProxy() == fOutputView.proxy());
373 SkASSERT(this->origin() == fOutputView.origin());
374
Brian Salomonf18b1d82017-10-27 11:30:49 -0400375 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700376 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700377}
378
robertphillips2e1e51f2015-10-15 08:01:48 -0700379#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500380void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400381 if (fOpsTask && !fOpsTask->isClosed()) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500382 SkASSERT(fOutputView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700383 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700384}
385#endif
386
Brian Osman11052242016-10-27 14:47:55 -0400387GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800388 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700389}
390
Chris Dalton7d6748e2019-03-13 00:34:52 -0600391inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600392 if (GrAA::kNo == aa) {
393 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
394 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600395 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600396 return GrAAType::kMSAA;
397 }
398 return GrAAType::kNone;
399 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600400 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600401}
402
Greg Daniele252f082017-10-23 16:05:23 -0400403GrMipMapped GrRenderTargetContext::mipMapped() const {
404 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
405 return proxy->mipMapped();
406 }
407 return GrMipMapped::kNo;
408}
409
Greg Danielf41b2bd2019-08-22 16:19:24 -0400410GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800411 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700412 SkDEBUGCODE(this->validate();)
413
Greg Danielf41b2bd2019-08-22 16:19:24 -0400414 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600415 sk_sp<GrOpsTask> newOpsTask =
Greg Daniel16f5c652019-10-29 11:26:01 -0400416 this->drawingManager()->newOpsTask(this->outputSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600417 if (fOpsTask && fNumStencilSamples > 0) {
418 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600419 fOpsTask->setMustPreserveStencil();
420 // Reload the stencil buffer content at the beginning of newOpsTask.
421 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
422 // values?
423 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
424 }
425 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700426 }
427
Greg Danielf41b2bd2019-08-22 16:19:24 -0400428 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700429}
430
Herb Derbycddab252018-07-16 11:19:04 -0400431void GrRenderTargetContext::drawGlyphRunList(
432 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400433 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800434 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700435 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700436 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400437 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700438
Greg Danielbe7fc462019-01-03 16:40:42 -0500439 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
440 // secondary command buffers because it would require stopping and starting a render pass which
441 // we don't have access to.
442 if (this->wrapsVkSecondaryCB()) {
443 return;
444 }
445
Herb Derby26cbe512018-05-24 14:39:01 -0400446 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400447 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400448 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700449}
450
Brian Osman11052242016-10-27 14:47:55 -0400451void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800452 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700453 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700454 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700456
Robert Phillips72152832017-01-25 17:31:35 -0500457 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400458
Greg Danielf41b2bd2019-08-22 16:19:24 -0400459 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700460}
461
Brian Osman11052242016-10-27 14:47:55 -0400462void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500463 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700464 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800465 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700466 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700467 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400468 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700469
Robert Phillips72152832017-01-25 17:31:35 -0500470 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700471 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
472 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700473}
robertphillips9199a9f2016-07-13 07:48:43 -0700474
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500475void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
476 const SkPMColor4f& color,
477 CanClearFullscreen canClearFullscreen) {
478 ASSERT_SINGLE_OWNER_PRIV
479 RETURN_IF_ABANDONED_PRIV
480 SkDEBUGCODE(fRenderTargetContext->validate();)
481 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
482 fRenderTargetContext->fContext);
483
484 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
485 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
486}
487
488static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
489 paint->setColor4f(color);
490 if (color.isOpaque()) {
491 // Can just rely on the src-over blend mode to do the right thing
492 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
493 } else {
494 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
495 // were src blended
496 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
497 }
498}
499
500void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
501 const SkPMColor4f& color,
502 CanClearFullscreen canClearFullscreen) {
503 bool isFull = false;
504 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400505 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
506 // only clear the entire target if we knew it had not been cleared before. As
507 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500508 isFull = !clip.scissorEnabled() ||
509 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400510 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500511 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
512 }
513
514 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400515 GrOpsTask* opsTask = this->getOpsTask();
516 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500517 !this->caps()->performColorClearsAsDraws()) {
518 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400519 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500520 return;
521 } else {
522 // Will use an op for the clear, reset the load op to discard since the op will
523 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400524 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500525 }
526
527 // Must add an op to the list (either because we couldn't use a load op, or because the
528 // clear load op isn't supported)
529 if (this->caps()->performColorClearsAsDraws()) {
530 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
531 GrPaint paint;
532 clear_to_grpaint(color, &paint);
533 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400534 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
535 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600537 this->addOp(GrClearOp::Make(
538 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500539 }
540 } else {
541 if (this->caps()->performPartialClearsAsDraws()) {
542 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500543 GrPaint paint;
544 clear_to_grpaint(color, &paint);
545
Michael Ludwig64b28a72019-05-28 12:02:00 -0400546 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400547 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
548 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500549 } else {
550 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
551 this->asSurfaceProxy()));
552 // This version of the clear op factory can return null if the clip doesn't intersect
553 // with the surface proxy's boundary
554 if (!op) {
555 return;
556 }
Chris Dalton08755122019-08-05 16:13:47 -0600557 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500558 }
559 }
560}
561
Brian Osman11052242016-10-27 14:47:55 -0400562void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500563 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400564 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000565 // Start with the render target, since that is the maximum content we could possibly fill.
566 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500567 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400568 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000569 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
570 // inverse view matrix.
571 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
572 } else {
573 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
574 SkMatrix localMatrix;
575 if (!viewMatrix.invert(&localMatrix)) {
576 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400577 }
Michael Ludwig61328202019-06-19 14:48:58 +0000578 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
579 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700580 }
robertphillipsea461502015-05-26 11:38:03 -0700581}
582
Michael Ludwig61328202019-06-19 14:48:58 +0000583enum class GrRenderTargetContext::QuadOptimization {
584 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
585 kDiscarded,
586 // The rect to draw was converted to some other op and appended to the oplist, so no additional
587 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
588 // a constColor is provided.
589 kSubmitted,
590 // The clip was folded into the device quad, with updated edge flags and local coords, and
591 // caller is responsible for adding an appropriate op.
592 kClipApplied,
593 // No change to clip, but quad updated to better fit clip/render target, and caller is
594 // responsible for adding an appropriate op.
595 kCropped
596};
Michael Ludwig61a16512019-01-15 11:15:13 -0500597
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400598static bool make_vertex_finite(float* value) {
599 if (SkScalarIsNaN(*value)) {
600 return false;
601 }
602
603 if (!SkScalarIsFinite(*value)) {
604 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
605 // left when calculating crops.
606 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
607 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
608 }
609
610 return true;
611}
612
Michael Ludwig61328202019-06-19 14:48:58 +0000613GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400614 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500615 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000616 // Optimization requirements:
617 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500618 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
619 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
620 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
621 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
622 // 5. kCropped in all other scenarios (although a crop may be a no-op)
623
624 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
625 // better to just keep the old flags instead of introducing mixed edge flags.
626 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000627
Michael Ludwige08b4432019-06-19 18:00:48 -0400628 SkRect rtRect;
629 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400630 // Must use size at which the rendertarget will ultimately be allocated so that stencil
631 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500632 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400633 } else {
634 // Use the logical size of the render target, which allows for "fullscreen" clears even if
635 // the render target has an approximate backing fit
636 rtRect = SkRect::MakeWH(this->width(), this->height());
637 }
638
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500639 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000640 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400641 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
642 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500643 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400644 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500645 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
646 !make_vertex_finite(quad->fDevice.ys() + i) ||
647 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400648 // Discard if we see a nan
649 return QuadOptimization::kDiscarded;
650 }
651 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500652 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400653 }
654 } else {
655 // CropToRect requires the quads to be finite. If they are not finite and we have local
656 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500657 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400658 return QuadOptimization::kDiscarded;
659 }
Brian Salomon7694b902019-06-18 21:00:21 +0000660 }
661
Michael Ludwig61328202019-06-19 14:48:58 +0000662 // If the quad is entirely off screen, it doesn't matter what the clip does
663 if (!rtRect.intersects(drawBounds)) {
664 return QuadOptimization::kDiscarded;
665 }
Brian Salomon7694b902019-06-18 21:00:21 +0000666
Michael Ludwig61328202019-06-19 14:48:58 +0000667 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
668 // the render target).
669 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400670 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
671 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
672 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
673 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000674 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000675 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000676 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
677 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000678 }
Brian Salomon7694b902019-06-18 21:00:21 +0000679 }
680
Michael Ludwig61328202019-06-19 14:48:58 +0000681 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
682 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400683 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000684 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
685 // of the render target and the clip rect)
686 SkRect clipBounds = rtRect;
687 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
688 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000689 }
690
Michael Ludwig61328202019-06-19 14:48:58 +0000691 if (clipRRect.isRect()) {
692 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500693 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700694 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500695 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000696 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500697 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000698 if (drawBounds.contains(rtRect)) {
699 // Fullscreen clear
700 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
701 return QuadOptimization::kSubmitted;
702 } else if (GrClip::IsPixelAligned(drawBounds) &&
703 drawBounds.width() > 256 && drawBounds.height() > 256) {
704 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
705 SkIRect scissorRect;
706 drawBounds.round(&scissorRect);
707 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
708 return QuadOptimization::kSubmitted;
709 }
710 }
711
712 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000713 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500714 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000715 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
716 // properly reflect the smooth edge of the clip.
717 *aa = GrAA::kYes;
718 }
719 // We intentionally do not downgrade AA here because we don't know if we need to
720 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
721 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
722
723 // deviceQuad is exactly the intersection of original quad and clip, so it can be
724 // drawn with no clip (submitted by caller)
725 return QuadOptimization::kClipApplied;
726 } else {
727 // The quads have been updated to better fit the clip bounds, but can't get rid of
728 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500729 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000730 return QuadOptimization::kCropped;
731 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700732 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000733 // Rounded corners and constant filled color (limit ourselves to solid colors because
734 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500735 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
736 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
737 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000738 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
739 // we can draw the rrect directly and ignore the edge flags
740 GrPaint paint;
741 clear_to_grpaint(*constColor, &paint);
742 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
743 clipRRect, GrStyle::SimpleFill());
744 return QuadOptimization::kSubmitted;
745 } else {
746 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500747 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000748 return QuadOptimization::kCropped;
749 }
Brian Salomon7694b902019-06-18 21:00:21 +0000750 }
Brian Salomon7694b902019-06-18 21:00:21 +0000751 }
752
Michael Ludwig61328202019-06-19 14:48:58 +0000753 // Crop the quad to the conservative bounds of the clip.
754 SkIRect clipDevBounds;
755 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
756 SkRect clipBounds = SkRect::Make(clipDevBounds);
757
758 // One final check for discarding, since we may have gone here directly due to a complex clip
759 if (!clipBounds.intersects(drawBounds)) {
760 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000761 }
762
Michael Ludwig61328202019-06-19 14:48:58 +0000763 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
764 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500765 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
766 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000767
768 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000769}
770
Michael Ludwig61328202019-06-19 14:48:58 +0000771void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500772 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500773 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500774 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400775 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000776 ASSERT_SINGLE_OWNER
777 RETURN_IF_ABANDONED
778 SkDEBUGCODE(this->validate();)
779 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500780
Michael Ludwig61328202019-06-19 14:48:58 +0000781 AutoCheckFlush acf(this->drawingManager());
782
783 SkPMColor4f* constColor = nullptr;
784 SkPMColor4f paintColor;
785 if (!ss && !paint.numCoverageFragmentProcessors() &&
786 paint.isConstantBlendedColor(&paintColor)) {
787 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
788 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500789 }
790
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500791 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000792 if (opt >= QuadOptimization::kClipApplied) {
793 // These optimizations require caller to add an op themselves
794 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
795 : clip;
796 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
797 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500798 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
799 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700800 }
Michael Ludwig61328202019-06-19 14:48:58 +0000801 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800802}
803
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000804void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400805 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500806 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000807 sk_sp<GrColorSpaceXform> textureXform,
808 GrSamplerState::Filter filter,
809 const SkPMColor4f& color,
810 SkBlendMode blendMode,
811 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500812 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000813 const SkRect* domain) {
814 ASSERT_SINGLE_OWNER
815 RETURN_IF_ABANDONED
816 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400817 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000818 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
819
820 AutoCheckFlush acf(this->drawingManager());
821
822 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
823 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500824 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000825
826 SkASSERT(opt != QuadOptimization::kSubmitted);
827 if (opt != QuadOptimization::kDiscarded) {
828 // And the texture op if not discarded
829 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
830 : clip;
831 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400832 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400833 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
834 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000835 // Use the provided domain, although hypothetically we could detect that the cropped local
836 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500837 this->addDrawOp(finalClip,
838 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
839 std::move(textureXform), filter, color, saturate,
840 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000841 }
842}
843
Brian Osman11052242016-10-27 14:47:55 -0400844void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500845 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500846 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400847 const SkMatrix& viewMatrix,
848 const SkRect& rect,
849 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700850 if (!style) {
851 style = &GrStyle::SimpleFill();
852 }
joshualitt1de610a2016-01-06 08:26:09 -0800853 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700854 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700855 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400856 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700857
bsalomon6663acf2016-05-10 09:14:17 -0700858 // Path effects should've been devolved to a path in SkGpuDevice
859 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700860
Robert Phillips72152832017-01-25 17:31:35 -0500861 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700862
bsalomon6663acf2016-05-10 09:14:17 -0700863 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400864 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000865 // Fills the rect, using rect as its own local coordinates
866 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500867 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700868 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
869 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
870 if ((!rect.width() || !rect.height()) &&
871 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
872 SkScalar r = stroke.getWidth() / 2;
873 // TODO: Move these stroke->fill fallbacks to GrShape?
874 switch (stroke.getJoin()) {
875 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500876 this->drawRect(
877 clip, std::move(paint), aa, viewMatrix,
878 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
879 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700880 return;
881 case SkPaint::kRound_Join:
882 // Raster draws nothing when both dimensions are empty.
883 if (rect.width() || rect.height()){
884 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500885 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
886 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700887 return;
888 }
889 case SkPaint::kBevel_Join:
890 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500891 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700892 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
893 &GrStyle::SimpleFill());
894 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500895 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700896 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
897 &GrStyle::SimpleFill());
898 }
899 return;
900 }
901 }
robertphillips44302392016-07-08 14:43:03 -0700902
Brian Salomonbaaf4392017-06-15 09:59:23 -0400903 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700904
Chris Dalton7d6748e2019-03-13 00:34:52 -0600905 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500906 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
907 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
908 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500909 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400910 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700911 return;
robertphillips4bc31812016-03-01 12:22:49 -0800912 }
robertphillips4bc31812016-03-01 12:22:49 -0800913 }
Mike Klein16885072018-12-11 09:54:31 -0500914 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500915 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700916}
917
Michael Ludwig69858532018-11-28 15:34:34 -0500918void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
919 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
920 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600921 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500922
923 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
924 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500925}
926
Robert Phillipsec2249f2016-11-09 08:54:35 -0500927int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500928 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400929 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500930}
931
Greg Danielf41b2bd2019-08-22 16:19:24 -0400932GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600933 ) const {
934#if GR_TEST_UTILS
935 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400936 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600937 }
938#endif
939 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
940 // would normally be overwritten. The one exception is if the render target context is marked as
941 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
942 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
943 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
944 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400945 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600946}
947
Chris Dalton858cf232019-10-14 16:20:00 -0600948void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600949 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600950 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600951 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600952
Chris Daltoneffee202019-07-01 22:28:03 -0600953 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600954 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500955 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600956 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400957 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600958 }
959 SkASSERT(numRequiredSamples > 0);
960
961 if (numRequiredSamples > fNumStencilSamples) {
962 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500963 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600964 }
Chris Dalton6b982802019-06-27 13:53:46 -0600965
Chris Dalton674f77a2019-09-30 20:49:39 -0600966 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600967 if (this->caps()->performStencilClearsAsDraws()) {
968 // There is a driver bug with clearing stencil. We must use an op to manually clear the
969 // stencil buffer before the op that required 'setNeedsStencil'.
970 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
971 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600972 this->getOpsTask()->setInitialStencilContent(
973 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600974 }
975 }
976}
977
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000978void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700979 ASSERT_SINGLE_OWNER_PRIV
980 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400981 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400982 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
983 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700984
Robert Phillips72152832017-01-25 17:31:35 -0500985 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400986
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500987 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
988}
989
990void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600991 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
992
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500993 if (this->caps()->performStencilClearsAsDraws()) {
994 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
995 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
996
997 // Configure the paint to have no impact on the color buffer
998 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400999 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001000 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1001 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001002 } else {
1003 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001004 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001005 if (!op) {
1006 return;
1007 }
Chris Dalton08755122019-08-05 16:13:47 -06001008 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001009 }
robertphillips976f5f02016-06-03 10:59:20 -07001010}
1011
Chris Daltonbbfd5162017-11-07 13:35:22 -07001012void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001013 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001014 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001015 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001016 ASSERT_SINGLE_OWNER_PRIV
1017 RETURN_IF_ABANDONED_PRIV
1018 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001019 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1020 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001021
Brian Salomon467921e2017-03-06 16:17:12 -05001022 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1023 SkASSERT(path);
1024 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1025
1026 // FIXME: Use path bounds instead of this WAR once
1027 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1028 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1029
1030 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001031 GrAppliedHardClip appliedClip;
1032 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1033 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001034 return;
1035 }
1036
Robert Phillips7c525e62018-06-12 10:11:12 -04001037 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1038 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001039 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001040 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001041 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001042 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001043 if (!op) {
1044 return;
1045 }
Brian Salomon97180af2017-03-14 13:42:58 -04001046 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001047
Chris Daltoneffee202019-07-01 22:28:03 -06001048 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001049 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001050}
1051
Michael Ludwig379e4962019-12-06 13:21:26 -05001052void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1053 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001054 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001055 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1056 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001057 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001058 ASSERT_SINGLE_OWNER
1059 RETURN_IF_ABANDONED
1060 SkDEBUGCODE(this->validate();)
1061 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001062
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001063 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1064 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1065 AutoCheckFlush acf(this->drawingManager());
1066 GrAAType aaType = this->chooseAAType(aa);
1067 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1068 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1069 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001070 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1071 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001072}
1073
Brian Osman11052242016-10-27 14:47:55 -04001074void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001075 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001076 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001077 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001078 GrPrimitiveType* overridePrimType,
1079 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001080 ASSERT_SINGLE_OWNER
1081 RETURN_IF_ABANDONED
1082 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001083 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001084
1085 AutoCheckFlush acf(this->drawingManager());
1086
1087 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001088 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001089 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001090 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001091 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001092 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001093}
1094
1095///////////////////////////////////////////////////////////////////////////////
1096
Brian Osman4d92b892019-03-24 00:53:23 +00001097void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1098 GrPaint&& paint,
1099 const SkMatrix& viewMatrix,
1100 int spriteCount,
1101 const SkRSXform xform[],
1102 const SkRect texRect[],
1103 const SkColor colors[]) {
1104 ASSERT_SINGLE_OWNER
1105 RETURN_IF_ABANDONED
1106 SkDEBUGCODE(this->validate();)
1107 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1108
1109 AutoCheckFlush acf(this->drawingManager());
1110
1111 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1112 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1113 aaType, spriteCount, xform, texRect, colors);
1114 this->addDrawOp(clip, std::move(op));
1115}
1116
1117///////////////////////////////////////////////////////////////////////////////
1118
Brian Osman11052242016-10-27 14:47:55 -04001119void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001120 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001121 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001122 const SkMatrix& viewMatrix,
1123 const SkRRect& rrect,
1124 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001125 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001126 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001127 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001128 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001129
1130 const SkStrokeRec& stroke = style.strokeRec();
1131 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001132 return;
1133 }
1134
bsalomon7f0d9f32016-08-15 14:49:10 -07001135 GrNoClip noclip;
1136 const GrClip* clip = &origClip;
1137#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1138 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001139 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001140 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1141 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001142 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1143 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001144 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001145 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1146 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001147 clip = &noclip;
1148 }
1149#endif
bsalomon6663acf2016-05-10 09:14:17 -07001150 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001151
Robert Phillips72152832017-01-25 17:31:35 -05001152 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001153
Chris Dalton7d6748e2019-03-13 00:34:52 -06001154 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001155
Chris Dalton0dffbab2019-03-27 13:08:50 -06001156 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001157 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1158 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1159 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1160 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1161 // to avoid perf regressions on some platforms.
1162 assert_alive(paint);
1163 op = GrOvalOpFactory::MakeCircularRRectOp(
1164 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1165 }
1166 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001167 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001168 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001169 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001170 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001171 assert_alive(paint);
1172 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001173 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001174 }
1175 if (op) {
1176 this->addDrawOp(*clip, std::move(op));
1177 return;
robertphillipsea461502015-05-26 11:38:03 -07001178 }
robertphillipsb56f9272016-02-25 11:03:52 -08001179
Mike Klein16885072018-12-11 09:54:31 -05001180 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001181 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1182 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001183}
1184
Jim Van Verthc5903412016-11-17 15:27:09 -05001185///////////////////////////////////////////////////////////////////////////////
1186
Jim Van Verth3af1af92017-05-18 15:06:54 -04001187static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1188 SkPoint3 result;
1189 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1190 result.fZ = pt.fZ;
1191 return result;
1192}
1193
1194bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001195 const SkMatrix& viewMatrix,
1196 const SkPath& path,
1197 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001198 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001199 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001200 return true;
1201 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001202 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001203 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001204
1205 // check z plane
1206 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1207 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1208 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1209 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1210 return false;
1211 }
1212
1213 SkRRect rrect;
1214 SkRect rect;
1215 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001216 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001217 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1218 if (!isRRect &&
1219 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1220 rect.width() > SK_ScalarNearlyZero) {
1221 rrect.setOval(rect);
1222 isRRect = true;
1223 }
1224 if (!isRRect && path.isRect(&rect)) {
1225 rrect.setRect(rect);
1226 isRRect = true;
1227 }
1228
1229 if (!isRRect) {
1230 return false;
1231 }
1232
Jim Van Verthc5903412016-11-17 15:27:09 -05001233 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001234 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001235 }
1236
Robert Phillips72152832017-01-25 17:31:35 -05001237 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001238
Jim Van Verth3af1af92017-05-18 15:06:54 -04001239 // transform light
1240 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1241
1242 // 1/scale
1243 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001244 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001245 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1246 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1247
1248 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001249 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1250
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001251 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001252 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1253 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1254 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255
1256 // Outset the shadow rrect to the border of the penumbra
1257 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1258 SkRRect ambientRRect;
1259 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1260 // If the rrect was an oval then its outset will also be one.
1261 // We set it explicitly to avoid errors.
1262 if (rrect.isOval()) {
1263 ambientRRect = SkRRect::MakeOval(outsetRect);
1264 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001265 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001266 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1267 }
1268
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001269 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001270 if (transparent) {
1271 // set a large inset to force a fill
1272 devSpaceInsetWidth = ambientRRect.width();
1273 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001274
Robert Phillips7c525e62018-06-12 10:11:12 -04001275 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1276 ambientColor,
1277 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001278 ambientRRect,
1279 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001280 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001281 if (op) {
1282 this->addDrawOp(clip, std::move(op));
1283 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001284 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001285
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001286 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001287 SkScalar devSpaceSpotBlur;
1288 SkScalar spotScale;
1289 SkVector spotOffset;
1290 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1291 devLightPos.fZ, rec.fLightRadius,
1292 &devSpaceSpotBlur, &spotScale, &spotOffset);
1293 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001294 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1295
Jim Van Verth3af1af92017-05-18 15:06:54 -04001296 // Adjust translate for the effect of the scale.
1297 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1298 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1299 // This offset is in dev space, need to transform it into source space.
1300 SkMatrix ctmInverse;
1301 if (viewMatrix.invert(&ctmInverse)) {
1302 ctmInverse.mapPoints(&spotOffset, 1);
1303 } else {
1304 // Since the matrix is a similarity, this should never happen, but just in case...
1305 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1306 SkASSERT(false);
1307 }
1308
1309 // Compute the transformed shadow rrect
1310 SkRRect spotShadowRRect;
1311 SkMatrix shadowTransform;
1312 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1313 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001314 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001315
1316 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001317 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001318 SkScalar insetWidth = blurOutset;
1319 if (transparent) {
1320 // If transparent, just do a fill
1321 insetWidth += spotShadowRRect.width();
1322 } else {
1323 // For shadows, instead of using a stroke we specify an inset from the penumbra
1324 // border. We want to extend this inset area so that it meets up with the caster
1325 // geometry. The inset geometry will by default already be inset by the blur width.
1326 //
1327 // We compare the min and max corners inset by the radius between the original
1328 // rrect and the shadow rrect. The distance between the two plus the difference
1329 // between the scaled radius and the original radius gives the distance from the
1330 // transformed shadow shape to the original shape in that corner. The max
1331 // of these gives the maximum distance we need to cover.
1332 //
1333 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1334 // that to get the full insetWidth.
1335 SkScalar maxOffset;
1336 if (rrect.isRect()) {
1337 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001338 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001339 rrect.rect().fLeft),
1340 SkTAbs(spotShadowRRect.rect().fTop -
1341 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001342 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001343 rrect.rect().fRight),
1344 SkTAbs(spotShadowRRect.rect().fBottom -
1345 rrect.rect().fBottom)));
1346 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001347 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001348 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1349 rrect.rect().fLeft + dr,
1350 spotShadowRRect.rect().fTop -
1351 rrect.rect().fTop + dr);
1352 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1353 rrect.rect().fRight - dr,
1354 spotShadowRRect.rect().fBottom -
1355 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001356 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001357 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001358 }
Brian Osman788b9162020-02-07 10:36:46 -05001359 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001360 }
1361
1362 // Outset the shadow rrect to the border of the penumbra
1363 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1364 if (spotShadowRRect.isOval()) {
1365 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1366 } else {
1367 SkScalar outsetRad = spotRadius + blurOutset;
1368 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1369 }
1370
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001371 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001372
Robert Phillips7c525e62018-06-12 10:11:12 -04001373 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1374 spotColor,
1375 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001376 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001377 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001378 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001379 if (op) {
1380 this->addDrawOp(clip, std::move(op));
1381 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001382 }
1383
1384 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001385}
1386
1387///////////////////////////////////////////////////////////////////////////////
1388
Brian Osman11052242016-10-27 14:47:55 -04001389bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001390 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001391 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001392 const SkMatrix& viewMatrix,
1393 const SkRRect& origOuter,
1394 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001395 SkASSERT(!origInner.isEmpty());
1396 SkASSERT(!origOuter.isEmpty());
1397
Brian Salomon65749212017-12-01 16:01:47 -05001398 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1399
Chris Dalton7d6748e2019-03-13 00:34:52 -06001400 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001401
1402 if (GrAAType::kMSAA == aaType) {
1403 return false;
1404 }
1405
Greg Daniel2655ede2019-04-10 00:49:28 +00001406 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1407 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001408 auto outerR = outer->width() / 2.f;
1409 auto innerR = inner->width() / 2.f;
1410 auto cx = outer->getBounds().fLeft + outerR;
1411 auto cy = outer->getBounds().fTop + outerR;
1412 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1413 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1414 auto avgR = (innerR + outerR) / 2.f;
1415 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1416 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1417 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001418 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001419 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001420 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001421 if (op) {
1422 this->addDrawOp(clip, std::move(op));
1423 return true;
1424 }
Mike Klein16885072018-12-11 09:54:31 -05001425 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001426 }
1427 }
1428
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001429 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001430 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001431 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1432 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001433 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001434 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1435 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001436 }
robertphillips00095892016-02-29 13:50:40 -08001437
robertphillips00095892016-02-29 13:50:40 -08001438 SkMatrix inverseVM;
1439 if (!viewMatrix.isIdentity()) {
1440 if (!origInner.transform(viewMatrix, inner.writable())) {
1441 return false;
1442 }
1443 if (!origOuter.transform(viewMatrix, outer.writable())) {
1444 return false;
1445 }
1446 if (!viewMatrix.invert(&inverseVM)) {
1447 return false;
1448 }
1449 } else {
1450 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001451 }
robertphillips00095892016-02-29 13:50:40 -08001452
Ethan Nicholaseace9352018-10-15 20:09:54 +00001453 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001454 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001455 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001456 if (!innerEffect) {
1457 return false;
1458 }
1459
Ethan Nicholaseace9352018-10-15 20:09:54 +00001460 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001461 if (!outerEffect) {
1462 return false;
1463 }
1464
Brian Salomon82f44312017-01-11 13:42:54 -05001465 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1466 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001467
1468 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001469 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001470 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1471 }
halcanary9d524f22016-03-29 09:03:52 -07001472
Brian Salomon82f44312017-01-11 13:42:54 -05001473 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1474 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001475 return true;
1476}
1477
Brian Osman11052242016-10-27 14:47:55 -04001478void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001479 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001480 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001481 const SkMatrix& viewMatrix,
1482 const SkRRect& outer,
1483 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001484 ASSERT_SINGLE_OWNER
1485 RETURN_IF_ABANDONED
1486 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001487 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001488
1489 SkASSERT(!outer.isEmpty());
1490 SkASSERT(!inner.isEmpty());
1491
Robert Phillips72152832017-01-25 17:31:35 -05001492 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001493
Brian Salomon82f44312017-01-11 13:42:54 -05001494 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001495 return;
1496 }
Mike Klein16885072018-12-11 09:54:31 -05001497 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001498
1499 SkPath path;
1500 path.setIsVolatile(true);
1501 path.addRRect(inner);
1502 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001503 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001504 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001505}
1506
robertphillipsea461502015-05-26 11:38:03 -07001507///////////////////////////////////////////////////////////////////////////////
1508
Brian Osman11052242016-10-27 14:47:55 -04001509void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001510 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001511 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001512 const SkMatrix& viewMatrix,
1513 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001514 const GrStyle& style,
1515 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001516 ASSERT_SINGLE_OWNER
1517 RETURN_IF_ABANDONED
1518 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001519 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001520
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001521 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001522 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001523 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001524 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001525 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1526 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 aa = GrAA::kNo;
1528 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001529 }
msarettcc319b92016-08-25 18:07:18 -07001530 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001531 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001532 SkPath path;
1533 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001534 path.setIsVolatile(true);
1535
Brian Salomon82f44312017-01-11 13:42:54 -05001536 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001537 }
1538
Chris Dalton7d6748e2019-03-13 00:34:52 -06001539 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001540 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1541 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001542 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001543}
1544
Brian Osman11052242016-10-27 14:47:55 -04001545void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001546 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001547 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001548 const SkMatrix& viewMatrix,
1549 const SkRect& oval,
1550 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001551 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001552 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001553 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001554 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001555
Robert Phillips7484d202018-07-03 09:09:08 -04001556 const SkStrokeRec& stroke = style.strokeRec();
1557
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001558 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001559 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1560 return;
1561 }
1562
1563 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001564 return;
robertphillipsea461502015-05-26 11:38:03 -07001565 }
1566
Robert Phillips72152832017-01-25 17:31:35 -05001567 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001568
Chris Dalton7d6748e2019-03-13 00:34:52 -06001569 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001570
1571 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001572 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1573 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001574 // We don't draw true circles as round rects in coverage mode, because it can
1575 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1576 assert_alive(paint);
1577 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1578 this->caps()->shaderCaps());
1579 }
1580 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001581 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1582 // the arc equation. This same special geometry and fragment branch also turn out to be a
1583 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1584 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1585 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001586 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001587 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1588 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001589 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001590 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001591 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001592 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1593 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001594 }
1595 if (op) {
1596 this->addDrawOp(clip, std::move(op));
1597 return;
robertphillipsea461502015-05-26 11:38:03 -07001598 }
robertphillipsb56f9272016-02-25 11:03:52 -08001599
Mike Klein16885072018-12-11 09:54:31 -05001600 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001601 this->drawShapeUsingPathRenderer(
1602 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001603 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001604}
1605
Brian Osman11052242016-10-27 14:47:55 -04001606void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001607 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001608 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001609 const SkMatrix& viewMatrix,
1610 const SkRect& oval,
1611 SkScalar startAngle,
1612 SkScalar sweepAngle,
1613 bool useCenter,
1614 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001615 ASSERT_SINGLE_OWNER
1616 RETURN_IF_ABANDONED
1617 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001618 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001619
1620 AutoCheckFlush acf(this->drawingManager());
1621
Chris Dalton7d6748e2019-03-13 00:34:52 -06001622 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001623 if (GrAAType::kCoverage == aaType) {
1624 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1625 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1626 std::move(paint),
1627 viewMatrix,
1628 oval,
1629 startAngle,
1630 sweepAngle,
1631 useCenter,
1632 style,
1633 shaderCaps);
1634 if (op) {
1635 this->addDrawOp(clip, std::move(op));
1636 return;
1637 }
1638 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001639 }
Brian Salomone4949402018-04-26 15:22:04 -04001640 this->drawShapeUsingPathRenderer(
1641 clip, std::move(paint), aa, viewMatrix,
1642 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001643}
1644
Brian Osman11052242016-10-27 14:47:55 -04001645void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001646 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001647 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001648 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001649 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001650 sk_sp<GrColorSpaceXform> csxf,
1651 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001652 std::unique_ptr<SkLatticeIter> iter,
1653 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001654 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001655 RETURN_IF_ABANDONED
1656 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001657 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001658
Robert Phillips72152832017-01-25 17:31:35 -05001659 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001660
Brian Salomon2a943df2018-05-04 13:43:19 -04001661 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001662 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001663 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001664 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001665}
1666
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001667void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1668 const SkRect& bounds) {
1669 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1670 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001671 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001672}
1673
Brian Salomon031b0ba2019-05-23 11:05:26 -04001674void GrRenderTargetContext::asyncRescaleAndReadPixels(
1675 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1676 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1677 auto direct = fContext->priv().asDirectContext();
1678 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001679 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001680 return;
1681 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001682 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001683 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001684 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001685 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001686 if (this->asRenderTargetProxy()->framebufferOnly()) {
1687 callback(context, nullptr);
1688 return;
1689 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001690 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001691 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001692 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001693 return;
1694 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001695 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001696 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001697 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001698 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001699 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001700 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1701 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001702 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001703 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001704 backendFormatOfFinalContext, dstCT);
1705 // Fail if we can't read from the source surface's color type.
1706 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001707 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 return;
1709 }
Brian Salomon624f9062019-07-02 14:23:00 -04001710 // Fail if read color type does not have all of dstCT's color channels and those missing color
1711 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001712 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1713 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1714 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1715 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001716 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001717 return;
1718 }
1719
Brian Salomonbf6b9792019-08-21 09:38:10 -04001720 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001721 int x = srcRect.fLeft;
1722 int y = srcRect.fTop;
1723 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001724 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1725 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001726 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001727 return;
1728 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001729 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001730 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001731 x = y = 0;
1732 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001733 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1734 this->colorInfo().alphaType(),
1735 info.colorSpace(),
1736 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001737 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1738 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001739 // We flip or color convert by drawing and we don't currently support drawing to
1740 // kPremul.
1741 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001742 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001743 return;
1744 }
Greg Daniel40903af2020-01-30 14:55:05 -05001745 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 SkRect srcRectToDraw = SkRect::Make(srcRect);
1747 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001748 if (!texProxyView.asTextureProxy()) {
1749 texProxyView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
1750 this->origin(), this->colorInfo().colorType(),
1751 GrMipMapped::kNo, srcRect,
1752 SkBackingFit::kApprox, SkBudgeted::kNo);
1753 if (!texProxyView.asTextureProxy()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001754 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001755 return;
1756 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001757 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1758 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001759 tempRTC = GrRenderTargetContext::Make(
1760 direct, this->colorInfo().colorType(), info.refColorSpace(),
1761 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001762 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001763 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001764 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001765 return;
1766 }
Greg Daniel40903af2020-01-30 14:55:05 -05001767 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1768 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1769 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001770 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1771 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1772 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001773 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001774 }
1775 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001776 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001777 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1778 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001779}
1780
Brian Salomon9241a6d2019-10-03 13:26:54 -04001781class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1782public:
1783 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1784 ~AsyncReadResult() override {
1785 for (int i = 0; i < fPlanes.count(); ++i) {
1786 if (!fPlanes[i].fMappedBuffer) {
1787 delete[] static_cast<const char*>(fPlanes[i].fData);
1788 } else {
1789 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1790 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1791 }
1792 }
1793 }
1794
1795 int count() const override { return fPlanes.count(); }
1796 const void* data(int i) const override { return fPlanes[i].fData; }
1797 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1798
1799 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001800 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001801 size_t rowBytes,
1802 GrClientMappedBufferManager* manager) {
1803 SkASSERT(!result.fTransferBuffer->isMapped());
1804 const void* mappedData = result.fTransferBuffer->map();
1805 if (!mappedData) {
1806 return false;
1807 }
1808 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001809 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001810 result.fPixelConverter(convertedData.get(), mappedData);
1811 this->addCpuPlane(std::move(convertedData), rowBytes);
1812 result.fTransferBuffer->unmap();
1813 } else {
1814 manager->insert(result.fTransferBuffer);
1815 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1816 }
1817 return true;
1818 }
1819
1820 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1821 SkASSERT(data);
1822 SkASSERT(rowBytes > 0);
1823 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1824 }
1825
1826private:
1827 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1828 SkASSERT(data);
1829 SkASSERT(rowBytes > 0);
1830 SkASSERT(mappedBuffer);
1831 SkASSERT(mappedBuffer->isMapped());
1832 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1833 }
1834
1835 struct Plane {
1836 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1837 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1838 const void* fData;
1839 size_t fRowBytes;
1840 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1841 sk_sp<GrGpuBuffer> fMappedBuffer;
1842 };
1843 SkSTArray<3, Plane> fPlanes;
1844 uint32_t fInboxID;
1845};
1846
Brian Salomon024bd002019-06-11 11:38:16 -04001847void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1848 ReadPixelsCallback callback,
1849 ReadPixelsContext context) {
1850 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1851 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1852
Greg Danielca9dbe22020-01-02 13:44:30 -05001853 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001854 callback(context, nullptr);
1855 return;
1856 }
1857
Brian Salomon9241a6d2019-10-03 13:26:54 -04001858 auto directContext = fContext->priv().asDirectContext();
1859 SkASSERT(directContext);
1860 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1861
Brian Salomon024bd002019-06-11 11:38:16 -04001862 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1863
1864 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001865 auto ii = SkImageInfo::Make(rect.size(), colorType,
1866 this->colorInfo().alphaType(),
1867 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001868 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001869 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1870 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1871 result->addCpuPlane(std::move(data), pm.rowBytes());
1872
Brian Salomon1d435302019-07-01 13:05:28 -04001873 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001874 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001875 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001876 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001877 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001878 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001879 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001880
Brian Salomonab32f652019-05-10 14:24:50 -04001881 struct FinishContext {
1882 ReadPixelsCallback* fClientCallback;
1883 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001884 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001885 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001886 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001887 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001888 };
1889 // Assumption is that the caller would like to flush. We could take a parameter or require an
1890 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1891 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001892 auto* finishContext = new FinishContext{callback,
1893 context,
1894 rect.size(),
1895 colorType,
1896 mappedBufferManager,
1897 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001898 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001899 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001900 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001901 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1902 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1903 context->fMappedBufferManager)) {
1904 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001905 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001906 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001907 delete context;
1908 };
1909 GrFlushInfo flushInfo;
1910 flushInfo.fFinishedContext = finishContext;
1911 flushInfo.fFinishedProc = finishCallback;
1912 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1913}
1914
Brian Salomon9241a6d2019-10-03 13:26:54 -04001915void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1916 sk_sp<SkColorSpace> dstColorSpace,
1917 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001918 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 RescaleGamma rescaleGamma,
1920 SkFilterQuality rescaleQuality,
1921 ReadPixelsCallback callback,
1922 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001923 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1924 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001925 SkASSERT(!dstSize.isZero());
1926 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1927
Brian Salomon024bd002019-06-11 11:38:16 -04001928 auto direct = fContext->priv().asDirectContext();
1929 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001930 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001931 return;
1932 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001933 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001934 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001935 return;
1936 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001937 if (this->asRenderTargetProxy()->framebufferOnly()) {
1938 callback(context, nullptr);
1939 return;
1940 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001941 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001942 callback(context, nullptr);
1943 return;
1944 }
Brian Salomon024bd002019-06-11 11:38:16 -04001945 int x = srcRect.fLeft;
1946 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001947 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001948 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001949 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001950 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001951 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001952 dstColorSpace);
1953 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001954 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001955 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001956 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001957 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001958 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001959 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001960 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001961 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001962 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001963 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001964 srcView = this->readSurfaceView();
1965 if (!srcView.asTextureProxy()) {
1966 srcView = GrSurfaceProxy::Copy(fContext, fReadView.proxy(), this->origin(),
1967 this->colorInfo().colorType(), GrMipMapped::kNo,
1968 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1969 if (!srcView.asTextureProxy()) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001970 // If we can't get a texture copy of the contents then give up.
1971 callback(context, nullptr);
1972 return;
1973 }
1974 x = y = 0;
1975 }
Brian Salomon4d036892019-07-02 15:10:58 -04001976 // We assume the caller wants kPremul. There is no way to indicate a preference.
1977 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001978 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1979 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001980 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001981 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1982 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001983 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1984 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001985 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001986 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001987 return;
1988 }
Greg Daniel40903af2020-01-30 14:55:05 -05001989 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1990 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1991 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1992 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1993 SkMatrix::I(), std::move(xform));
1994 srcView = tempRTC->readSurfaceView();
1995 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001996 x = y = 0;
1997 }
1998 }
Greg Danielc594e622019-10-15 14:01:49 -04001999
Greg Daniele20fcad2020-01-08 11:52:34 -05002000 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2001 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2002 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002003 int halfW = dstSize.width()/2;
2004 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002005 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2006 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2007 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2008 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2009 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2010 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002011 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002012 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002013 return;
2014 }
2015
Brian Osman28ba5282019-10-30 14:18:07 -04002016 float baseM[20];
2017 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2018
Brian Salomon024bd002019-06-11 11:38:16 -04002019 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2020
2021 auto texMatrix = SkMatrix::MakeTrans(x, y);
2022
Brian Salomon9241a6d2019-10-03 13:26:54 -04002023 SkRect dstRectY = SkRect::Make(dstSize);
2024 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002025
Brian Salomon6aa65052020-01-28 12:16:53 -05002026 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2027 PixelTransferResult yTransfer, uTransfer, vTransfer;
2028
Brian Salomon024bd002019-06-11 11:38:16 -04002029 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2030 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002031 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002032 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002033 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002034 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002035 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002036 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2037 yPaint.addColorFragmentProcessor(std::move(yFP));
2038 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002039 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2040 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002041 if (!doSynchronousRead) {
2042 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2043 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2044 if (!yTransfer.fTransferBuffer) {
2045 callback(context, nullptr);
2046 return;
2047 }
Brian Salomon024bd002019-06-11 11:38:16 -04002048 }
2049
2050 texMatrix.preScale(2.f, 2.f);
2051 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2052 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002053 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002054 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002055 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002056 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002057 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002058 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2059 uPaint.addColorFragmentProcessor(std::move(uFP));
2060 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002061 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2062 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002063 if (!doSynchronousRead) {
2064 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2065 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2066 if (!uTransfer.fTransferBuffer) {
2067 callback(context, nullptr);
2068 return;
2069 }
Brian Salomon024bd002019-06-11 11:38:16 -04002070 }
2071
2072 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2073 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002074 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002075 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002076 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002077 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2078 this->colorInfo().alphaType(), texMatrix,
2079 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002080 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2081 vPaint.addColorFragmentProcessor(std::move(vFP));
2082 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002083 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2084 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002085 if (!doSynchronousRead) {
2086 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2087 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2088 if (!vTransfer.fTransferBuffer) {
2089 callback(context, nullptr);
2090 return;
2091 }
2092 }
2093
2094 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002095 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2096 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2097 size_t yRB = yInfo.minRowBytes();
2098 size_t uvRB = uvInfo.minRowBytes();
2099 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2100 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2101 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2102 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2103 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2104 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002105 callback(context, nullptr);
2106 return;
2107 }
2108 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002109 result->addCpuPlane(std::move(y), yRB );
2110 result->addCpuPlane(std::move(u), uvRB);
2111 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002112 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002113 return;
2114 }
2115
2116 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002117 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002118 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002119 GrClientMappedBufferManager* fMappedBufferManager;
2120 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002121 PixelTransferResult fYTransfer;
2122 PixelTransferResult fUTransfer;
2123 PixelTransferResult fVTransfer;
2124 };
2125 // Assumption is that the caller would like to flush. We could take a parameter or require an
2126 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2127 // callback to GrGpu until after the next flush that flushes our op list, though.
2128 auto* finishContext = new FinishContext{callback,
2129 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002130 direct->priv().clientMappedBufferManager(),
2131 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002132 std::move(yTransfer),
2133 std::move(uTransfer),
2134 std::move(vTransfer)};
2135 auto finishCallback = [](GrGpuFinishedContext c) {
2136 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002137 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002138 auto manager = context->fMappedBufferManager;
2139 size_t rowBytes = SkToSizeT(context->fSize.width());
2140 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2141 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002142 delete context;
2143 return;
2144 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002145 rowBytes /= 2;
2146 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2147 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2148 (*context->fClientCallback)(context->fClientContext, nullptr);
2149 delete context;
2150 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002151 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002152 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2153 (*context->fClientCallback)(context->fClientContext, nullptr);
2154 delete context;
2155 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002156 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002157 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002158 delete context;
2159 };
2160 GrFlushInfo flushInfo;
2161 flushInfo.fFinishedContext = finishContext;
2162 flushInfo.fFinishedProc = finishCallback;
2163 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002164}
2165
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002166GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2167 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002168 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002169 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002170 return GrSemaphoresSubmitted::kNo;
2171 }
robertphillips8c523e02016-07-26 07:41:00 -07002172 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002173 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002174
Greg Daniel55f040b2020-02-13 15:38:32 +00002175 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002176}
2177
Greg Danielc64ee462017-06-15 16:59:49 -04002178bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002179 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002180 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002181 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002182 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002183 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002184
2185 AutoCheckFlush acf(this->drawingManager());
2186
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002187 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002188 return false;
2189 }
2190
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002191 auto direct = fContext->priv().asDirectContext();
2192 if (!direct) {
2193 return false;
2194 }
2195
2196 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002197
Greg Daniel301015c2019-11-18 14:06:46 -05002198 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2199 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002200 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002201 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002202 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2203 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002204 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002205 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2206 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002207 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002208}
joshualitt33a5fce2015-11-18 13:28:51 -08002209
Brian Osman11052242016-10-27 14:47:55 -04002210void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002211 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002212 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002213 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002214 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002215 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002216 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002217 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002218 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002219 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2220
Brian Salomon40b77a62017-12-22 11:25:52 -05002221 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002222
Robert Phillips27927a52018-08-20 13:18:12 -04002223 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002224}
2225
2226void GrRenderTargetContext::drawShape(const GrClip& clip,
2227 GrPaint&& paint,
2228 GrAA aa,
2229 const SkMatrix& viewMatrix,
2230 const GrShape& shape) {
2231 ASSERT_SINGLE_OWNER
2232 RETURN_IF_ABANDONED
2233 SkDEBUGCODE(this->validate();)
2234 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2235
Brian Salomon2fad74a2017-12-20 13:28:55 -05002236 if (shape.isEmpty()) {
2237 if (shape.inverseFilled()) {
2238 this->drawPaint(clip, std::move(paint), viewMatrix);
2239 }
2240 return;
robertphillipsea461502015-05-26 11:38:03 -07002241 }
2242
Robert Phillips72152832017-01-25 17:31:35 -05002243 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002244
Brian Salomon2fad74a2017-12-20 13:28:55 -05002245 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002246 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002247 SkRRect rrect;
2248 // We can ignore the starting point and direction since there is no path effect.
2249 bool inverted;
2250 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2251 if (rrect.isRect()) {
2252 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2253 &shape.style());
2254 return;
2255 } else if (rrect.isOval()) {
2256 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002257 return;
2258 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002259 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2260 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002261 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2262 viewMatrix.rectStaysRect()) {
2263 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2264 // the matrix to all the points individually rather than just to the rect
2265 SkRect rects[2];
2266 if (shape.asNestedRects(rects)) {
2267 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002268 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002269 fContext, std::move(paint), viewMatrix, rects);
2270 if (op) {
2271 this->addDrawOp(clip, std::move(op));
2272 }
2273 // Returning here indicates that there is nothing to draw in this case.
2274 return;
2275 }
robertphillipsea461502015-05-26 11:38:03 -07002276 }
2277 }
robertphillips4bc31812016-03-01 12:22:49 -08002278
Brian Salomon2fad74a2017-12-20 13:28:55 -05002279 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002280}
2281
Chris Daltonbbfd5162017-11-07 13:35:22 -07002282bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002283 const GrUserStencilSettings* ss,
2284 SkRegion::Op op,
2285 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002286 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002287 const SkMatrix& viewMatrix,
2288 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002289 ASSERT_SINGLE_OWNER_PRIV
2290 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002291 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002292 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2293 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002294
2295 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002296 GrPaint paint;
2297 paint.setCoverageSetOpXPFactory(op, invert);
2298 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2299 SkRect::MakeIWH(fRenderTargetContext->width(),
2300 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002301 return true;
2302 }
2303
Robert Phillips72152832017-01-25 17:31:35 -05002304 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002305
2306 // An Assumption here is that path renderer would use some form of tweaking
2307 // the src color (either the input alpha or in the frag shader) to implement
2308 // aa. If we have some future driver-mojo path AA that can do the right
2309 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002310 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002311 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002312
Chris Daltondb91c6e2017-09-08 16:25:08 -06002313 SkIRect clipConservativeBounds;
2314 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2315 &clipConservativeBounds, nullptr);
2316
bsalomon8acedde2016-06-24 10:42:16 -07002317 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002318 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002319 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002320 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002321 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002322 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002323 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002324 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002325 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2326 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002327 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002328
2329 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002330 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002331 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002332 if (!pr) {
2333 return false;
2334 }
2335
2336 GrPaint paint;
2337 paint.setCoverageSetOpXPFactory(op, invert);
2338
Brian Salomonf3569f02017-10-24 12:52:33 -04002339 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2340 std::move(paint),
2341 ss,
2342 fRenderTargetContext,
2343 &clip,
2344 &clipConservativeBounds,
2345 &viewMatrix,
2346 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002347 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002348 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002349 pr->drawPath(args);
2350 return true;
2351}
2352
Brian Osman11052242016-10-27 14:47:55 -04002353SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002354 ASSERT_SINGLE_OWNER_PRIV
2355
Robert Phillips6a6de562019-02-15 15:19:15 -05002356 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002357 return SkBudgeted::kNo;
2358 }
2359
Brian Osman11052242016-10-27 14:47:55 -04002360 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002361
Greg Daniel3912a4b2020-01-14 09:56:04 -05002362 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002363}
2364
Brian Salomon2fad74a2017-12-20 13:28:55 -05002365void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2366 GrPaint&& paint,
2367 GrAA aa,
2368 const SkMatrix& viewMatrix,
2369 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002370 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002371 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002372 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2373
Jim Van Verthf86073a2018-10-02 13:05:38 -04002374 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2375 return;
2376 }
2377
Chris Daltondb91c6e2017-09-08 16:25:08 -06002378 SkIRect clipConservativeBounds;
2379 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2380
Brian Salomon2fad74a2017-12-20 13:28:55 -05002381 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002382 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002383
robertphillips68737822015-10-29 12:12:21 -07002384 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002385 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002386 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002387 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002388 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002389 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002390 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002391 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002392
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002393 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002394 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002395 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002396 return;
2397 }
2398
Chris Dalton6ce447a2019-06-23 18:07:38 -06002399 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002400
2401 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2402 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2403 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2404
Brian Salomon2fad74a2017-12-20 13:28:55 -05002405 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002406 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002407 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2408 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002409 return;
2410 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002411 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002412 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002413 }
2414 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002415 if (canDrawArgs.fShape->style().applies()) {
2416 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2417 styleScale);
2418 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002419 return;
2420 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002421 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002422 // This time, allow SW renderer
2423 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2424 } else {
2425 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002426 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002427 }
robertphillipsea461502015-05-26 11:38:03 -07002428
bsalomon8acedde2016-06-24 10:42:16 -07002429 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002430#ifdef SK_DEBUG
2431 SkDebugf("Unable to find path renderer compatible with path.\n");
2432#endif
2433 return;
2434 }
2435
Robert Phillips256c37b2017-03-01 14:32:46 -05002436 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002437 std::move(paint),
2438 &GrUserStencilSettings::kUnused,
2439 this,
2440 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002441 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002442 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002443 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002444 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002445 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002446 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002447}
2448
Brian Salomon467921e2017-03-06 16:17:12 -05002449static void op_bounds(SkRect* bounds, const GrOp* op) {
2450 *bounds = op->bounds();
2451 if (op->hasZeroArea()) {
2452 if (op->hasAABloat()) {
2453 bounds->outset(0.5f, 0.5f);
2454 } else {
2455 // We don't know which way the particular GPU will snap lines or points at integer
2456 // coords. So we ensure that the bounds is large enough for either snap.
2457 SkRect before = *bounds;
2458 bounds->roundOut(bounds);
2459 if (bounds->fLeft == before.fLeft) {
2460 bounds->fLeft -= 1;
2461 }
2462 if (bounds->fTop == before.fTop) {
2463 bounds->fTop -= 1;
2464 }
2465 if (bounds->fRight == before.fRight) {
2466 bounds->fRight += 1;
2467 }
2468 if (bounds->fBottom == before.fBottom) {
2469 bounds->fBottom += 1;
2470 }
2471 }
2472 }
2473}
2474
Chris Dalton08755122019-08-05 16:13:47 -06002475void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002476 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002477 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2478}
2479
Brian Salomon348a0372018-10-31 10:42:18 -04002480void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2481 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002482 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002483 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002484 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002485 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002486 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002487 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002488 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002489 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002490
Brian Salomon467921e2017-03-06 16:17:12 -05002491 // Setup clip
2492 SkRect bounds;
2493 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002494 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002495 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002496 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002497 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002498
Chris Dalton9acf6822019-11-12 11:52:40 -07002499 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002500 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002501 }
2502
Chris Dalton9acf6822019-11-12 11:52:40 -07002503 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002504 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002505 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002506 }
2507
Chris Dalton9acf6822019-11-12 11:52:40 -07002508 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2509 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2510
2511 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2512 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2513 // that uses a stencil test when the stencil buffer is multisampled.)
2514 bool hasMixedSampledCoverage = (
2515 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002516 SkASSERT(!hasMixedSampledCoverage ||
2517 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002518
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002519 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002520 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002521 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2522
Greg Daniel524e28b2019-11-01 11:48:53 -04002523 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002524 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002525 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002526 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002527 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002528 }
2529 }
2530
2531 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002532 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002533 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002534 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002535 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002536 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002537 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002538}
2539
Greg Daniel524e28b2019-11-01 11:48:53 -04002540bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2541 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002542 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2543 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2544 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002545 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002546 return false;
2547 }
2548
Greg Daniel3912a4b2020-01-14 09:56:04 -05002549 if (this->caps()->textureBarrierSupport() &&
2550 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002551 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002552 // The render target is a texture, so we can read from it directly in the shader. The XP
2553 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002554 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002555 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002556 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002557 }
2558 }
2559
Greg Daniel3912a4b2020-01-14 09:56:04 -05002560 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002561
Eric Karl74480882017-04-03 14:49:05 -07002562 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002563 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002564 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002565 SkRect opBounds = op.bounds();
2566 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2567 // 0.5 pixels.
2568 if (op.hasAABloat() || op.hasZeroArea()) {
2569 opBounds.outset(0.5f, 0.5f);
2570 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2571 // performance we may ignore the clip when the draw is entirely inside the clip is float
2572 // space but will hit pixels just outside the clip when actually rasterizing.
2573 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002574 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002575 }
2576 SkIRect opIBounds;
2577 opBounds.roundOut(&opIBounds);
2578 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002579#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002580 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002581#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002582 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002583 }
2584
Greg Daniel3155f7f2020-01-16 16:54:26 -05002585 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002586 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2587 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002588 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002589 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002590
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002591 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002592 copyRect = clippedRect;
2593 }
Brian Salomon467921e2017-03-06 16:17:12 -05002594
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002595 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002596 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002597 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002598 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002600 } else {
Eric Karl74480882017-04-03 14:49:05 -07002601 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002602 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002603 }
Greg Daniel40903af2020-01-30 14:55:05 -05002604 GrSurfaceProxyView newProxyView =
2605 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), colorType,
2606 GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2607 restrictions.fRectsMustMatch);
2608 SkASSERT(newProxyView.proxy());
Robert Phillipsbf25d432017-04-07 10:08:53 -04002609
Greg Daniel40903af2020-01-30 14:55:05 -05002610 dstProxyView->setProxyView(std::move(newProxyView));
Greg Daniel524e28b2019-11-01 11:48:53 -04002611 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002612 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002613}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002614
Greg Daniel573312e2020-02-07 17:22:35 -05002615bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002616 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002617 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002618 SkIRect clippedSrcRect;
2619 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002620 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2621 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002622 return false;
2623 }
2624
2625 GrPaint paint;
2626 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002627
Greg Danield2ccbb52020-02-05 10:45:39 -05002628 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002629 if (!fp) {
2630 return false;
2631 }
2632 paint.addColorFragmentProcessor(std::move(fp));
2633
2634 this->fillRectToRect(
2635 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2636 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2637 clippedSrcRect.height()),
2638 SkRect::Make(clippedSrcRect));
2639 return true;
2640}