blob: 2eadfccac40bca27736e7d555859075080b10b36 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
Brian Osmanf5ecf512020-04-01 09:29:13 -040011#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrBackendSemaphore.h"
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 Salomonf2ebdd92019-09-30 12:15:30 -040036#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrMemoryPool.h"
38#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040040#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrRenderTargetContextPriv.h"
42#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/GrStencilAttachment.h"
44#include "src/gpu/GrStyle.h"
45#include "src/gpu/GrTracing.h"
46#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040047#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040049#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040050#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000051#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040052#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "src/gpu/ops/GrAtlasTextOp.h"
54#include "src/gpu/ops/GrClearOp.h"
55#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrDrawAtlasOp.h"
57#include "src/gpu/ops/GrDrawOp.h"
58#include "src/gpu/ops/GrDrawVerticesOp.h"
59#include "src/gpu/ops/GrDrawableOp.h"
60#include "src/gpu/ops/GrFillRRectOp.h"
61#include "src/gpu/ops/GrFillRectOp.h"
62#include "src/gpu/ops/GrLatticeOp.h"
63#include "src/gpu/ops/GrOp.h"
64#include "src/gpu/ops/GrOvalOpFactory.h"
65#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066#include "src/gpu/ops/GrShadowRRectOp.h"
67#include "src/gpu/ops/GrStencilPathOp.h"
68#include "src/gpu/ops/GrStrokeRectOp.h"
69#include "src/gpu/ops/GrTextureOp.h"
70#include "src/gpu/text/GrTextContext.h"
71#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040072
Herb Derbyc1b482c2018-08-09 15:02:27 -040073class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040074public:
75 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040076 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040077 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040078 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040079 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040080
Robert Phillips7c525e62018-06-12 10:11:12 -040081 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040082 fRenderTargetContext->addDrawOp(clip, std::move(op));
83 }
84
Robert Phillips46a13382018-08-23 13:53:01 -040085 void drawShape(const GrClip& clip, const SkPaint& paint,
86 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040087 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
88 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040089 }
90
91 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040092 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050093 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040094 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040095 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040096 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040097 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040098 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040099 }
100 }
101
Robert Phillips69893702019-02-22 11:16:30 -0500102 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400103 return fRenderTargetContext->fContext;
104 }
105
Herb Derby65956872018-08-21 16:55:04 -0400106 SkGlyphRunListPainter* glyphPainter() override {
107 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400108 }
109
Brian Salomonf18b1d82017-10-27 11:30:49 -0400110private:
111 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400112 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400113
Brian Salomonf18b1d82017-10-27 11:30:49 -0400114};
joshualittbc907352016-01-13 06:45:40 -0800115
Robert Phillips72152832017-01-25 17:31:35 -0500116#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800117#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400118 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800119#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400120 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500121#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
122#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
123#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
124#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
125#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700126
Brian Salomone225b562017-06-14 13:00:03 -0400127//////////////////////////////////////////////////////////////////////////////
128
robertphillipsea461502015-05-26 11:38:03 -0700129class AutoCheckFlush {
130public:
halcanary9d524f22016-03-29 09:03:52 -0700131 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700132 SkASSERT(fDrawingManager);
133 }
bsalomonb77a9072016-09-07 10:02:04 -0700134 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700135
136private:
robertphillips77a2e522015-10-17 07:43:27 -0700137 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700138};
139
Greg Daniele20fcad2020-01-08 11:52:34 -0500140std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
141 GrRecordingContext* context,
142 GrColorType colorType,
143 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500144 sk_sp<GrSurfaceProxy> proxy,
145 GrSurfaceOrigin origin,
146 const SkSurfaceProps* surfaceProps,
147 bool managedOps) {
148 if (!proxy) {
149 return nullptr;
150 }
151
152 const GrBackendFormat& format = proxy->backendFormat();
153 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400154 GrSwizzle writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
Greg Danielba0ff782020-01-07 15:42:57 -0500155
Greg Daniel3912a4b2020-01-14 09:56:04 -0500156 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400157 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500158
159 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400160 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500161 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500162}
163
164std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
165 GrRecordingContext* context,
166 GrColorType colorType,
167 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500168 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500169 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500170 const GrBackendFormat& format,
171 int sampleCnt,
172 GrMipMapped mipMapped,
173 GrProtected isProtected,
174 GrSurfaceOrigin origin,
175 SkBudgeted budgeted,
176 const SkSurfaceProps* surfaceProps) {
177 // It is probably not necessary to check if the context is abandoned here since uses of the
178 // GrRenderTargetContext which need the context will mostly likely fail later on without an
179 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
180 // an abandoned context correctly. It also lets us early out of some extra work.
181 if (context->priv().abandoned()) {
182 return nullptr;
183 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500184
Greg Daniele20fcad2020-01-08 11:52:34 -0500185 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400186 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500187 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500188 if (!proxy) {
189 return nullptr;
190 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500191
Greg Danielba0ff782020-01-07 15:42:57 -0500192 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
193 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500194 if (!rtc) {
195 return nullptr;
196 }
197 rtc->discard();
198 return rtc;
199}
200
201std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
202 GrRecordingContext* context,
203 GrColorType colorType,
204 sk_sp<SkColorSpace> colorSpace,
205 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500206 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500207 int sampleCnt,
208 GrMipMapped mipMapped,
209 GrProtected isProtected,
210 GrSurfaceOrigin origin,
211 SkBudgeted budgeted,
212 const SkSurfaceProps* surfaceProps) {
213 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
214 if (!format.isValid()) {
215 return nullptr;
216 }
217
218 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
219 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
220 surfaceProps);
221}
222
223static inline GrColorType color_type_fallback(GrColorType ct) {
224 switch (ct) {
225 // kRGBA_8888 is our default fallback for many color types that may not have renderable
226 // backend formats.
227 case GrColorType::kAlpha_8:
228 case GrColorType::kBGR_565:
229 case GrColorType::kABGR_4444:
230 case GrColorType::kBGRA_8888:
231 case GrColorType::kRGBA_1010102:
232 case GrColorType::kRGBA_F16:
233 case GrColorType::kRGBA_F16_Clamped:
234 return GrColorType::kRGBA_8888;
235 case GrColorType::kAlpha_F16:
236 return GrColorType::kRGBA_F16;
237 case GrColorType::kGray_8:
238 return GrColorType::kRGB_888x;
239 default:
240 return GrColorType::kUnknown;
241 }
242}
243
244std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
245 GrRecordingContext* context,
246 GrColorType colorType,
247 sk_sp<SkColorSpace> colorSpace,
248 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500249 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500250 int sampleCnt,
251 GrMipMapped mipMapped,
252 GrProtected isProtected,
253 GrSurfaceOrigin origin,
254 SkBudgeted budgeted,
255 const SkSurfaceProps* surfaceProps) {
Brian Salomone8d20802020-04-01 09:10:38 -0400256 std::unique_ptr<GrRenderTargetContext> rtc;
257 do {
258 rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
259 sampleCnt, mipMapped, isProtected, origin, budgeted,
260 surfaceProps);
261 colorType = color_type_fallback(colorType);
262 } while (!rtc && colorType != GrColorType::kUnknown);
263 return rtc;
Greg Daniele20fcad2020-01-08 11:52:34 -0500264}
265
Greg Danielba0ff782020-01-07 15:42:57 -0500266std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
267 GrRecordingContext* context,
268 GrColorType colorType,
269 sk_sp<SkColorSpace> colorSpace,
270 const GrBackendTexture& tex,
271 int sampleCnt,
272 GrSurfaceOrigin origin,
273 const SkSurfaceProps* surfaceProps,
274 ReleaseProc releaseProc,
275 ReleaseContext releaseCtx) {
276 SkASSERT(sampleCnt > 0);
277 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400278 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500279 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500280 if (!proxy) {
281 return nullptr;
282 }
283
284 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
285 origin, surfaceProps);
286}
287
288std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
289 GrRecordingContext* context,
290 GrColorType colorType,
291 sk_sp<SkColorSpace> colorSpace,
292 const GrBackendTexture& tex,
293 int sampleCnt,
294 GrSurfaceOrigin origin,
295 const SkSurfaceProps* surfaceProps) {
296 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400297 sk_sp<GrSurfaceProxy> proxy(
298 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500299 if (!proxy) {
300 return nullptr;
301 }
302
303 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
304 origin, surfaceProps);
305}
306
307std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
308 GrRecordingContext* context,
309 GrColorType colorType,
310 sk_sp<SkColorSpace> colorSpace,
311 const GrBackendRenderTarget& rt,
312 GrSurfaceOrigin origin,
313 const SkSurfaceProps* surfaceProps,
314 ReleaseProc releaseProc,
315 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400316 sk_sp<GrSurfaceProxy> proxy(
317 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500318 if (!proxy) {
319 return nullptr;
320 }
321
322 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
323 origin, surfaceProps);
324}
325
326std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
327 GrRecordingContext* context,
328 const SkImageInfo& imageInfo,
329 const GrVkDrawableInfo& vkInfo,
330 const SkSurfaceProps* props) {
331 sk_sp<GrSurfaceProxy> proxy(
332 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
333 vkInfo));
334 if (!proxy) {
335 return nullptr;
336 }
337
Greg Danielba0ff782020-01-07 15:42:57 -0500338 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
339 imageInfo.refColorSpace(), std::move(proxy),
340 kTopLeft_GrSurfaceOrigin, props);
341}
342
Greg Danielf41b2bd2019-08-22 16:19:24 -0400343// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
344// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
345// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
346// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500347GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500348 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400349 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400350 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400351 sk_sp<SkColorSpace> colorSpace,
352 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400353 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500354 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
355 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400356 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500357 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400358 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400359 , fManagedOpsTask(managedOpsTask) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400360 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
361 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500362
Brian Salomonf18b1d82017-10-27 11:30:49 -0400363 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700364 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700365}
366
robertphillips2e1e51f2015-10-15 08:01:48 -0700367#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500368void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400369 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400370 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700371 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700372}
373#endif
374
Brian Osman11052242016-10-27 14:47:55 -0400375GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800376 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700377}
378
Chris Dalton7d6748e2019-03-13 00:34:52 -0600379inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600380 if (GrAA::kNo == aa) {
381 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
382 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600383 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600384 return GrAAType::kMSAA;
385 }
386 return GrAAType::kNone;
387 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600388 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600389}
390
Greg Daniele252f082017-10-23 16:05:23 -0400391GrMipMapped GrRenderTargetContext::mipMapped() const {
392 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
393 return proxy->mipMapped();
394 }
395 return GrMipMapped::kNo;
396}
397
Greg Danielf41b2bd2019-08-22 16:19:24 -0400398GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800399 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700400 SkDEBUGCODE(this->validate();)
401
Greg Danielf41b2bd2019-08-22 16:19:24 -0400402 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600403 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400404 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600405 if (fOpsTask && fNumStencilSamples > 0) {
406 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600407 fOpsTask->setMustPreserveStencil();
408 // Reload the stencil buffer content at the beginning of newOpsTask.
409 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
410 // values?
411 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
412 }
413 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700414 }
415
Greg Danielf41b2bd2019-08-22 16:19:24 -0400416 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700417}
418
Herb Derbycddab252018-07-16 11:19:04 -0400419void GrRenderTargetContext::drawGlyphRunList(
420 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400421 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800422 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700423 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700424 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400425 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700426
Greg Danielbe7fc462019-01-03 16:40:42 -0500427 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
428 // secondary command buffers because it would require stopping and starting a render pass which
429 // we don't have access to.
430 if (this->wrapsVkSecondaryCB()) {
431 return;
432 }
433
Herb Derby26cbe512018-05-24 14:39:01 -0400434 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400435 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400436 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700437}
438
Brian Osman11052242016-10-27 14:47:55 -0400439void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800440 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700441 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700442 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400443 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700444
Robert Phillips72152832017-01-25 17:31:35 -0500445 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400446
Greg Danielf41b2bd2019-08-22 16:19:24 -0400447 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700448}
449
Brian Osman11052242016-10-27 14:47:55 -0400450void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500451 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700452 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800453 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700454 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700455 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400456 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700457
Robert Phillips72152832017-01-25 17:31:35 -0500458 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700459 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
460 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700461}
robertphillips9199a9f2016-07-13 07:48:43 -0700462
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500463void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
464 const SkPMColor4f& color,
465 CanClearFullscreen canClearFullscreen) {
466 ASSERT_SINGLE_OWNER_PRIV
467 RETURN_IF_ABANDONED_PRIV
468 SkDEBUGCODE(fRenderTargetContext->validate();)
469 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
470 fRenderTargetContext->fContext);
471
472 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
473 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
474}
475
476static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
477 paint->setColor4f(color);
478 if (color.isOpaque()) {
479 // Can just rely on the src-over blend mode to do the right thing
480 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
481 } else {
482 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
483 // were src blended
484 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
485 }
486}
487
488void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
489 const SkPMColor4f& color,
490 CanClearFullscreen canClearFullscreen) {
491 bool isFull = false;
492 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400493 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
494 // only clear the entire target if we knew it had not been cleared before. As
495 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500496 isFull = !clip.scissorEnabled() ||
497 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400498 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500499 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
500 }
501
502 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400503 GrOpsTask* opsTask = this->getOpsTask();
504 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500505 !this->caps()->performColorClearsAsDraws()) {
506 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400507 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500508 return;
509 } else {
510 // Will use an op for the clear, reset the load op to discard since the op will
511 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400512 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500513 }
514
515 // Must add an op to the list (either because we couldn't use a load op, or because the
516 // clear load op isn't supported)
517 if (this->caps()->performColorClearsAsDraws()) {
518 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
519 GrPaint paint;
520 clear_to_grpaint(color, &paint);
521 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400522 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
523 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500524 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600525 this->addOp(GrClearOp::Make(
526 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500527 }
528 } else {
529 if (this->caps()->performPartialClearsAsDraws()) {
530 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500531 GrPaint paint;
532 clear_to_grpaint(color, &paint);
533
Michael Ludwig64b28a72019-05-28 12:02:00 -0400534 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400535 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
536 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500537 } else {
538 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
539 this->asSurfaceProxy()));
540 // This version of the clear op factory can return null if the clip doesn't intersect
541 // with the surface proxy's boundary
542 if (!op) {
543 return;
544 }
Chris Dalton08755122019-08-05 16:13:47 -0600545 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500546 }
547 }
548}
549
Brian Osman11052242016-10-27 14:47:55 -0400550void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500551 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400552 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000553 // Start with the render target, since that is the maximum content we could possibly fill.
554 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500555 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400556 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000557 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
558 // inverse view matrix.
559 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
560 } else {
561 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
562 SkMatrix localMatrix;
563 if (!viewMatrix.invert(&localMatrix)) {
564 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400565 }
Michael Ludwig61328202019-06-19 14:48:58 +0000566 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
567 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700568 }
robertphillipsea461502015-05-26 11:38:03 -0700569}
570
Michael Ludwig61328202019-06-19 14:48:58 +0000571enum class GrRenderTargetContext::QuadOptimization {
572 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
573 kDiscarded,
574 // The rect to draw was converted to some other op and appended to the oplist, so no additional
575 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
576 // a constColor is provided.
577 kSubmitted,
578 // The clip was folded into the device quad, with updated edge flags and local coords, and
579 // caller is responsible for adding an appropriate op.
580 kClipApplied,
581 // No change to clip, but quad updated to better fit clip/render target, and caller is
582 // responsible for adding an appropriate op.
583 kCropped
584};
Michael Ludwig61a16512019-01-15 11:15:13 -0500585
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400586static bool make_vertex_finite(float* value) {
587 if (SkScalarIsNaN(*value)) {
588 return false;
589 }
590
591 if (!SkScalarIsFinite(*value)) {
592 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
593 // left when calculating crops.
594 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
595 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
596 }
597
598 return true;
599}
600
Michael Ludwig61328202019-06-19 14:48:58 +0000601GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400602 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500603 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000604 // Optimization requirements:
605 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500606 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
607 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
608 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
609 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
610 // 5. kCropped in all other scenarios (although a crop may be a no-op)
611
612 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
613 // better to just keep the old flags instead of introducing mixed edge flags.
614 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000615
Michael Ludwige08b4432019-06-19 18:00:48 -0400616 SkRect rtRect;
617 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400618 // Must use size at which the rendertarget will ultimately be allocated so that stencil
619 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500620 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400621 } else {
622 // Use the logical size of the render target, which allows for "fullscreen" clears even if
623 // the render target has an approximate backing fit
624 rtRect = SkRect::MakeWH(this->width(), this->height());
625 }
626
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500627 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000628 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400629 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
630 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500631 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400632 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500633 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
634 !make_vertex_finite(quad->fDevice.ys() + i) ||
635 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400636 // Discard if we see a nan
637 return QuadOptimization::kDiscarded;
638 }
639 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500640 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400641 }
642 } else {
643 // CropToRect requires the quads to be finite. If they are not finite and we have local
644 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500645 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400646 return QuadOptimization::kDiscarded;
647 }
Brian Salomon7694b902019-06-18 21:00:21 +0000648 }
649
Michael Ludwig61328202019-06-19 14:48:58 +0000650 // If the quad is entirely off screen, it doesn't matter what the clip does
651 if (!rtRect.intersects(drawBounds)) {
652 return QuadOptimization::kDiscarded;
653 }
Brian Salomon7694b902019-06-18 21:00:21 +0000654
Michael Ludwig61328202019-06-19 14:48:58 +0000655 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
656 // the render target).
657 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400658 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
659 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
660 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
661 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000662 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000663 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000664 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
665 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000666 }
Brian Salomon7694b902019-06-18 21:00:21 +0000667 }
668
Michael Ludwig61328202019-06-19 14:48:58 +0000669 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
670 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400671 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000672 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
673 // of the render target and the clip rect)
674 SkRect clipBounds = rtRect;
675 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
676 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000677 }
678
Michael Ludwig61328202019-06-19 14:48:58 +0000679 if (clipRRect.isRect()) {
680 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500681 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700682 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500683 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000684 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500685 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000686 if (drawBounds.contains(rtRect)) {
687 // Fullscreen clear
688 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
689 return QuadOptimization::kSubmitted;
690 } else if (GrClip::IsPixelAligned(drawBounds) &&
691 drawBounds.width() > 256 && drawBounds.height() > 256) {
692 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
693 SkIRect scissorRect;
694 drawBounds.round(&scissorRect);
695 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
696 return QuadOptimization::kSubmitted;
697 }
698 }
699
700 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000701 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500702 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000703 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
704 // properly reflect the smooth edge of the clip.
705 *aa = GrAA::kYes;
706 }
707 // We intentionally do not downgrade AA here because we don't know if we need to
708 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
709 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
710
711 // deviceQuad is exactly the intersection of original quad and clip, so it can be
712 // drawn with no clip (submitted by caller)
713 return QuadOptimization::kClipApplied;
714 } else {
715 // The quads have been updated to better fit the clip bounds, but can't get rid of
716 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500717 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000718 return QuadOptimization::kCropped;
719 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700720 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000721 // Rounded corners and constant filled color (limit ourselves to solid colors because
722 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500723 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
724 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
725 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000726 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
727 // we can draw the rrect directly and ignore the edge flags
728 GrPaint paint;
729 clear_to_grpaint(*constColor, &paint);
730 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
731 clipRRect, GrStyle::SimpleFill());
732 return QuadOptimization::kSubmitted;
733 } else {
734 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500735 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000736 return QuadOptimization::kCropped;
737 }
Brian Salomon7694b902019-06-18 21:00:21 +0000738 }
Brian Salomon7694b902019-06-18 21:00:21 +0000739 }
740
Michael Ludwig61328202019-06-19 14:48:58 +0000741 // Crop the quad to the conservative bounds of the clip.
742 SkIRect clipDevBounds;
743 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
744 SkRect clipBounds = SkRect::Make(clipDevBounds);
745
746 // One final check for discarding, since we may have gone here directly due to a complex clip
747 if (!clipBounds.intersects(drawBounds)) {
748 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000749 }
750
Michael Ludwig61328202019-06-19 14:48:58 +0000751 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
752 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500753 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
754 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000755
756 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000757}
758
Michael Ludwig61328202019-06-19 14:48:58 +0000759void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500760 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500761 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500762 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400763 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000764 ASSERT_SINGLE_OWNER
765 RETURN_IF_ABANDONED
766 SkDEBUGCODE(this->validate();)
767 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500768
Michael Ludwig61328202019-06-19 14:48:58 +0000769 AutoCheckFlush acf(this->drawingManager());
770
771 SkPMColor4f* constColor = nullptr;
772 SkPMColor4f paintColor;
773 if (!ss && !paint.numCoverageFragmentProcessors() &&
774 paint.isConstantBlendedColor(&paintColor)) {
775 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
776 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500777 }
778
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500779 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000780 if (opt >= QuadOptimization::kClipApplied) {
781 // These optimizations require caller to add an op themselves
782 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
783 : clip;
784 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
785 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500786 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
787 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700788 }
Michael Ludwig61328202019-06-19 14:48:58 +0000789 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800790}
791
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000792void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400793 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500794 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000795 sk_sp<GrColorSpaceXform> textureXform,
796 GrSamplerState::Filter filter,
797 const SkPMColor4f& color,
798 SkBlendMode blendMode,
799 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500800 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000801 const SkRect* domain) {
802 ASSERT_SINGLE_OWNER
803 RETURN_IF_ABANDONED
804 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400805 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000806 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
807
808 AutoCheckFlush acf(this->drawingManager());
809
810 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
811 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500812 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000813
814 SkASSERT(opt != QuadOptimization::kSubmitted);
815 if (opt != QuadOptimization::kDiscarded) {
816 // And the texture op if not discarded
817 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
818 : clip;
819 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400820 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400821 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
822 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000823 // Use the provided domain, although hypothetically we could detect that the cropped local
824 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500825 this->addDrawOp(finalClip,
826 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
827 std::move(textureXform), filter, color, saturate,
828 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000829 }
830}
831
Brian Osman11052242016-10-27 14:47:55 -0400832void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500833 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500834 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400835 const SkMatrix& viewMatrix,
836 const SkRect& rect,
837 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700838 if (!style) {
839 style = &GrStyle::SimpleFill();
840 }
joshualitt1de610a2016-01-06 08:26:09 -0800841 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700842 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700843 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400844 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700845
bsalomon6663acf2016-05-10 09:14:17 -0700846 // Path effects should've been devolved to a path in SkGpuDevice
847 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700848
Robert Phillips72152832017-01-25 17:31:35 -0500849 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700850
bsalomon6663acf2016-05-10 09:14:17 -0700851 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400852 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000853 // Fills the rect, using rect as its own local coordinates
854 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500855 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700856 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
857 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
858 if ((!rect.width() || !rect.height()) &&
859 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
860 SkScalar r = stroke.getWidth() / 2;
861 // TODO: Move these stroke->fill fallbacks to GrShape?
862 switch (stroke.getJoin()) {
863 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500864 this->drawRect(
865 clip, std::move(paint), aa, viewMatrix,
866 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
867 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700868 return;
869 case SkPaint::kRound_Join:
870 // Raster draws nothing when both dimensions are empty.
871 if (rect.width() || rect.height()){
872 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500873 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
874 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700875 return;
876 }
877 case SkPaint::kBevel_Join:
878 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500879 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700880 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
881 &GrStyle::SimpleFill());
882 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500883 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700884 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
885 &GrStyle::SimpleFill());
886 }
887 return;
888 }
889 }
robertphillips44302392016-07-08 14:43:03 -0700890
Brian Salomonbaaf4392017-06-15 09:59:23 -0400891 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700892
Chris Dalton7d6748e2019-03-13 00:34:52 -0600893 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500894 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
895 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
896 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500897 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400898 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700899 return;
robertphillips4bc31812016-03-01 12:22:49 -0800900 }
robertphillips4bc31812016-03-01 12:22:49 -0800901 }
Mike Klein16885072018-12-11 09:54:31 -0500902 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500903 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700904}
905
Michael Ludwig69858532018-11-28 15:34:34 -0500906void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
907 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
908 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600909 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500910
911 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
912 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500913}
914
Robert Phillipsec2249f2016-11-09 08:54:35 -0500915int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500916 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400917 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500918}
919
Greg Danielf41b2bd2019-08-22 16:19:24 -0400920GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600921 ) const {
922#if GR_TEST_UTILS
923 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400924 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600925 }
926#endif
927 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
928 // would normally be overwritten. The one exception is if the render target context is marked as
929 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
930 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
931 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
932 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400933 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600934}
935
Chris Dalton858cf232019-10-14 16:20:00 -0600936void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600937 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600938 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600939 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600940
Chris Daltoneffee202019-07-01 22:28:03 -0600941 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600942 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500943 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600944 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400945 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600946 }
947 SkASSERT(numRequiredSamples > 0);
948
949 if (numRequiredSamples > fNumStencilSamples) {
950 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500951 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600952 }
Chris Dalton6b982802019-06-27 13:53:46 -0600953
Chris Dalton674f77a2019-09-30 20:49:39 -0600954 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600955 if (this->caps()->performStencilClearsAsDraws()) {
956 // There is a driver bug with clearing stencil. We must use an op to manually clear the
957 // stencil buffer before the op that required 'setNeedsStencil'.
958 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
959 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600960 this->getOpsTask()->setInitialStencilContent(
961 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600962 }
963 }
964}
965
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000966void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700967 ASSERT_SINGLE_OWNER_PRIV
968 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400969 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400970 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
971 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700972
Robert Phillips72152832017-01-25 17:31:35 -0500973 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400974
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500975 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
976}
977
978void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600979 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
980
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500981 if (this->caps()->performStencilClearsAsDraws()) {
982 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
983 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
984
985 // Configure the paint to have no impact on the color buffer
986 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400987 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400988 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
989 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500990 } else {
991 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -0500992 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500993 if (!op) {
994 return;
995 }
Chris Dalton08755122019-08-05 16:13:47 -0600996 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500997 }
robertphillips976f5f02016-06-03 10:59:20 -0700998}
999
Chris Daltonbbfd5162017-11-07 13:35:22 -07001000void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001001 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001002 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001003 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001004 ASSERT_SINGLE_OWNER_PRIV
1005 RETURN_IF_ABANDONED_PRIV
1006 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001007 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1008 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001009
Brian Salomon467921e2017-03-06 16:17:12 -05001010 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1011 SkASSERT(path);
1012 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1013
1014 // FIXME: Use path bounds instead of this WAR once
1015 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1016 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1017
1018 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001019 GrAppliedHardClip appliedClip;
1020 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1021 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001022 return;
1023 }
1024
Robert Phillips7c525e62018-06-12 10:11:12 -04001025 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1026 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001027 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001028 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001029 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001030 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001031 if (!op) {
1032 return;
1033 }
Brian Salomon97180af2017-03-14 13:42:58 -04001034 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001035
Chris Daltoneffee202019-07-01 22:28:03 -06001036 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001037 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001038}
1039
Michael Ludwig379e4962019-12-06 13:21:26 -05001040void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1041 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001042 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001043 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1044 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001045 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001046 ASSERT_SINGLE_OWNER
1047 RETURN_IF_ABANDONED
1048 SkDEBUGCODE(this->validate();)
1049 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001050
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001051 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1052 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1053 AutoCheckFlush acf(this->drawingManager());
1054 GrAAType aaType = this->chooseAAType(aa);
1055 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1056 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1057 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001058 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1059 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001060}
1061
Brian Osman11052242016-10-27 14:47:55 -04001062void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001063 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001064 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001065 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001066 GrPrimitiveType* overridePrimType,
1067 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001068 ASSERT_SINGLE_OWNER
1069 RETURN_IF_ABANDONED
1070 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001071 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001072
1073 AutoCheckFlush acf(this->drawingManager());
1074
1075 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001076 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001077 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001078 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001079 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001080 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001081}
1082
1083///////////////////////////////////////////////////////////////////////////////
1084
Brian Osman4d92b892019-03-24 00:53:23 +00001085void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1086 GrPaint&& paint,
1087 const SkMatrix& viewMatrix,
1088 int spriteCount,
1089 const SkRSXform xform[],
1090 const SkRect texRect[],
1091 const SkColor colors[]) {
1092 ASSERT_SINGLE_OWNER
1093 RETURN_IF_ABANDONED
1094 SkDEBUGCODE(this->validate();)
1095 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1096
1097 AutoCheckFlush acf(this->drawingManager());
1098
1099 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1100 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1101 aaType, spriteCount, xform, texRect, colors);
1102 this->addDrawOp(clip, std::move(op));
1103}
1104
1105///////////////////////////////////////////////////////////////////////////////
1106
Brian Osman11052242016-10-27 14:47:55 -04001107void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001108 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001109 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001110 const SkMatrix& viewMatrix,
1111 const SkRRect& rrect,
1112 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001113 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001114 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001115 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001116 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001117
1118 const SkStrokeRec& stroke = style.strokeRec();
1119 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001120 return;
1121 }
1122
bsalomon7f0d9f32016-08-15 14:49:10 -07001123 GrNoClip noclip;
1124 const GrClip* clip = &origClip;
1125#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1126 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001127 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001128 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1129 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001130 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1131 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001132 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001133 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1134 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001135 clip = &noclip;
1136 }
1137#endif
bsalomon6663acf2016-05-10 09:14:17 -07001138 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001139
Robert Phillips72152832017-01-25 17:31:35 -05001140 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001141
Chris Dalton7d6748e2019-03-13 00:34:52 -06001142 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001143
Chris Dalton0dffbab2019-03-27 13:08:50 -06001144 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001145 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1146 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1147 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1148 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1149 // to avoid perf regressions on some platforms.
1150 assert_alive(paint);
1151 op = GrOvalOpFactory::MakeCircularRRectOp(
1152 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1153 }
1154 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001155 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001156 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001157 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001158 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001159 assert_alive(paint);
1160 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001161 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001162 }
1163 if (op) {
1164 this->addDrawOp(*clip, std::move(op));
1165 return;
robertphillipsea461502015-05-26 11:38:03 -07001166 }
robertphillipsb56f9272016-02-25 11:03:52 -08001167
Mike Klein16885072018-12-11 09:54:31 -05001168 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001169 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1170 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001171}
1172
Jim Van Verthc5903412016-11-17 15:27:09 -05001173///////////////////////////////////////////////////////////////////////////////
1174
Jim Van Verth3af1af92017-05-18 15:06:54 -04001175static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1176 SkPoint3 result;
1177 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1178 result.fZ = pt.fZ;
1179 return result;
1180}
1181
1182bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001183 const SkMatrix& viewMatrix,
1184 const SkPath& path,
1185 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001186 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001187 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001188 return true;
1189 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001190 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001191 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001192
1193 // check z plane
1194 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1195 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1196 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1197 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1198 return false;
1199 }
1200
1201 SkRRect rrect;
1202 SkRect rect;
1203 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001204 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001205 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1206 if (!isRRect &&
1207 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1208 rect.width() > SK_ScalarNearlyZero) {
1209 rrect.setOval(rect);
1210 isRRect = true;
1211 }
1212 if (!isRRect && path.isRect(&rect)) {
1213 rrect.setRect(rect);
1214 isRRect = true;
1215 }
1216
1217 if (!isRRect) {
1218 return false;
1219 }
1220
Jim Van Verthc5903412016-11-17 15:27:09 -05001221 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001222 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001223 }
1224
Robert Phillips72152832017-01-25 17:31:35 -05001225 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001226
Jim Van Verth3af1af92017-05-18 15:06:54 -04001227 // transform light
1228 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1229
1230 // 1/scale
1231 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001232 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001233 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1234 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1235
1236 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001237 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1238
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001239 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001240 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1241 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1242 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001243
1244 // Outset the shadow rrect to the border of the penumbra
1245 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1246 SkRRect ambientRRect;
1247 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1248 // If the rrect was an oval then its outset will also be one.
1249 // We set it explicitly to avoid errors.
1250 if (rrect.isOval()) {
1251 ambientRRect = SkRRect::MakeOval(outsetRect);
1252 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001253 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1255 }
1256
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001257 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001258 if (transparent) {
1259 // set a large inset to force a fill
1260 devSpaceInsetWidth = ambientRRect.width();
1261 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001262
Robert Phillips7c525e62018-06-12 10:11:12 -04001263 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1264 ambientColor,
1265 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001266 ambientRRect,
1267 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001268 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001269 if (op) {
1270 this->addDrawOp(clip, std::move(op));
1271 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001272 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001273
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001274 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001275 SkScalar devSpaceSpotBlur;
1276 SkScalar spotScale;
1277 SkVector spotOffset;
1278 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1279 devLightPos.fZ, rec.fLightRadius,
1280 &devSpaceSpotBlur, &spotScale, &spotOffset);
1281 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001282 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1283
Jim Van Verth3af1af92017-05-18 15:06:54 -04001284 // Adjust translate for the effect of the scale.
1285 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1286 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1287 // This offset is in dev space, need to transform it into source space.
1288 SkMatrix ctmInverse;
1289 if (viewMatrix.invert(&ctmInverse)) {
1290 ctmInverse.mapPoints(&spotOffset, 1);
1291 } else {
1292 // Since the matrix is a similarity, this should never happen, but just in case...
1293 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1294 SkASSERT(false);
1295 }
1296
1297 // Compute the transformed shadow rrect
1298 SkRRect spotShadowRRect;
1299 SkMatrix shadowTransform;
1300 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1301 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001302 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001303
1304 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001305 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001306 SkScalar insetWidth = blurOutset;
1307 if (transparent) {
1308 // If transparent, just do a fill
1309 insetWidth += spotShadowRRect.width();
1310 } else {
1311 // For shadows, instead of using a stroke we specify an inset from the penumbra
1312 // border. We want to extend this inset area so that it meets up with the caster
1313 // geometry. The inset geometry will by default already be inset by the blur width.
1314 //
1315 // We compare the min and max corners inset by the radius between the original
1316 // rrect and the shadow rrect. The distance between the two plus the difference
1317 // between the scaled radius and the original radius gives the distance from the
1318 // transformed shadow shape to the original shape in that corner. The max
1319 // of these gives the maximum distance we need to cover.
1320 //
1321 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1322 // that to get the full insetWidth.
1323 SkScalar maxOffset;
1324 if (rrect.isRect()) {
1325 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001326 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001327 rrect.rect().fLeft),
1328 SkTAbs(spotShadowRRect.rect().fTop -
1329 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001330 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001331 rrect.rect().fRight),
1332 SkTAbs(spotShadowRRect.rect().fBottom -
1333 rrect.rect().fBottom)));
1334 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001335 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001336 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1337 rrect.rect().fLeft + dr,
1338 spotShadowRRect.rect().fTop -
1339 rrect.rect().fTop + dr);
1340 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1341 rrect.rect().fRight - dr,
1342 spotShadowRRect.rect().fBottom -
1343 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001344 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001345 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001346 }
Brian Osman788b9162020-02-07 10:36:46 -05001347 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001348 }
1349
1350 // Outset the shadow rrect to the border of the penumbra
1351 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1352 if (spotShadowRRect.isOval()) {
1353 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1354 } else {
1355 SkScalar outsetRad = spotRadius + blurOutset;
1356 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1357 }
1358
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001359 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001360
Robert Phillips7c525e62018-06-12 10:11:12 -04001361 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1362 spotColor,
1363 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001364 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001365 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001366 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001367 if (op) {
1368 this->addDrawOp(clip, std::move(op));
1369 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001370 }
1371
1372 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001373}
1374
1375///////////////////////////////////////////////////////////////////////////////
1376
Brian Osman11052242016-10-27 14:47:55 -04001377bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001378 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001379 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001380 const SkMatrix& viewMatrix,
1381 const SkRRect& origOuter,
1382 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001383 SkASSERT(!origInner.isEmpty());
1384 SkASSERT(!origOuter.isEmpty());
1385
Brian Salomon65749212017-12-01 16:01:47 -05001386 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1387
Chris Dalton7d6748e2019-03-13 00:34:52 -06001388 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001389
1390 if (GrAAType::kMSAA == aaType) {
1391 return false;
1392 }
1393
Greg Daniel2655ede2019-04-10 00:49:28 +00001394 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1395 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001396 auto outerR = outer->width() / 2.f;
1397 auto innerR = inner->width() / 2.f;
1398 auto cx = outer->getBounds().fLeft + outerR;
1399 auto cy = outer->getBounds().fTop + outerR;
1400 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1401 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1402 auto avgR = (innerR + outerR) / 2.f;
1403 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1404 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1405 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001406 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001407 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001408 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001409 if (op) {
1410 this->addDrawOp(clip, std::move(op));
1411 return true;
1412 }
Mike Klein16885072018-12-11 09:54:31 -05001413 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001414 }
1415 }
1416
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001417 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001418 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001419 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1420 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001421 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001422 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1423 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001424 }
robertphillips00095892016-02-29 13:50:40 -08001425
robertphillips00095892016-02-29 13:50:40 -08001426 SkMatrix inverseVM;
1427 if (!viewMatrix.isIdentity()) {
1428 if (!origInner.transform(viewMatrix, inner.writable())) {
1429 return false;
1430 }
1431 if (!origOuter.transform(viewMatrix, outer.writable())) {
1432 return false;
1433 }
1434 if (!viewMatrix.invert(&inverseVM)) {
1435 return false;
1436 }
1437 } else {
1438 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001439 }
robertphillips00095892016-02-29 13:50:40 -08001440
Ethan Nicholaseace9352018-10-15 20:09:54 +00001441 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001442 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001443 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001444 if (!innerEffect) {
1445 return false;
1446 }
1447
Ethan Nicholaseace9352018-10-15 20:09:54 +00001448 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001449 if (!outerEffect) {
1450 return false;
1451 }
1452
Brian Salomon82f44312017-01-11 13:42:54 -05001453 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1454 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001455
1456 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001457 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001458 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1459 }
halcanary9d524f22016-03-29 09:03:52 -07001460
Brian Salomon82f44312017-01-11 13:42:54 -05001461 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1462 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001463 return true;
1464}
1465
Brian Osman11052242016-10-27 14:47:55 -04001466void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001467 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001468 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001469 const SkMatrix& viewMatrix,
1470 const SkRRect& outer,
1471 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001472 ASSERT_SINGLE_OWNER
1473 RETURN_IF_ABANDONED
1474 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001475 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001476
1477 SkASSERT(!outer.isEmpty());
1478 SkASSERT(!inner.isEmpty());
1479
Robert Phillips72152832017-01-25 17:31:35 -05001480 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001481
Brian Salomon82f44312017-01-11 13:42:54 -05001482 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001483 return;
1484 }
Mike Klein16885072018-12-11 09:54:31 -05001485 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001486
1487 SkPath path;
1488 path.setIsVolatile(true);
1489 path.addRRect(inner);
1490 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001491 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001492 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001493}
1494
robertphillipsea461502015-05-26 11:38:03 -07001495///////////////////////////////////////////////////////////////////////////////
1496
Brian Osman11052242016-10-27 14:47:55 -04001497void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001498 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001499 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001500 const SkMatrix& viewMatrix,
1501 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001502 const GrStyle& style,
1503 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001504 ASSERT_SINGLE_OWNER
1505 RETURN_IF_ABANDONED
1506 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001507 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001508
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001509 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001510 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001511 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001512 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001513 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1514 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001515 aa = GrAA::kNo;
1516 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001517 }
msarettcc319b92016-08-25 18:07:18 -07001518 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001519 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001520 SkPath path;
1521 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001522 path.setIsVolatile(true);
1523
Brian Salomon82f44312017-01-11 13:42:54 -05001524 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001525 }
1526
Chris Dalton7d6748e2019-03-13 00:34:52 -06001527 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001528 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1529 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001530 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001531}
1532
Brian Osman11052242016-10-27 14:47:55 -04001533void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001534 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001535 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001536 const SkMatrix& viewMatrix,
1537 const SkRect& oval,
1538 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001539 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001540 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001541 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001542 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001543
Robert Phillips7484d202018-07-03 09:09:08 -04001544 const SkStrokeRec& stroke = style.strokeRec();
1545
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001546 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001547 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1548 return;
1549 }
1550
1551 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001552 return;
robertphillipsea461502015-05-26 11:38:03 -07001553 }
1554
Robert Phillips72152832017-01-25 17:31:35 -05001555 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001556
Chris Dalton7d6748e2019-03-13 00:34:52 -06001557 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001558
1559 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001560 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1561 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001562 // We don't draw true circles as round rects in coverage mode, because it can
1563 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1564 assert_alive(paint);
1565 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1566 this->caps()->shaderCaps());
1567 }
1568 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001569 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1570 // the arc equation. This same special geometry and fragment branch also turn out to be a
1571 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1572 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1573 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001574 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001575 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1576 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001577 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001578 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001579 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001580 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1581 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001582 }
1583 if (op) {
1584 this->addDrawOp(clip, std::move(op));
1585 return;
robertphillipsea461502015-05-26 11:38:03 -07001586 }
robertphillipsb56f9272016-02-25 11:03:52 -08001587
Mike Klein16885072018-12-11 09:54:31 -05001588 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001589 this->drawShapeUsingPathRenderer(
1590 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001591 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001592}
1593
Brian Osman11052242016-10-27 14:47:55 -04001594void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001595 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001596 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001597 const SkMatrix& viewMatrix,
1598 const SkRect& oval,
1599 SkScalar startAngle,
1600 SkScalar sweepAngle,
1601 bool useCenter,
1602 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001603 ASSERT_SINGLE_OWNER
1604 RETURN_IF_ABANDONED
1605 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001606 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001607
1608 AutoCheckFlush acf(this->drawingManager());
1609
Chris Dalton7d6748e2019-03-13 00:34:52 -06001610 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001611 if (GrAAType::kCoverage == aaType) {
1612 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1613 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1614 std::move(paint),
1615 viewMatrix,
1616 oval,
1617 startAngle,
1618 sweepAngle,
1619 useCenter,
1620 style,
1621 shaderCaps);
1622 if (op) {
1623 this->addDrawOp(clip, std::move(op));
1624 return;
1625 }
1626 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001627 }
Brian Salomone4949402018-04-26 15:22:04 -04001628 this->drawShapeUsingPathRenderer(
1629 clip, std::move(paint), aa, viewMatrix,
1630 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001631}
1632
Brian Osman11052242016-10-27 14:47:55 -04001633void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001634 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001635 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001636 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001637 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001638 sk_sp<GrColorSpaceXform> csxf,
1639 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001640 std::unique_ptr<SkLatticeIter> iter,
1641 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001642 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001643 RETURN_IF_ABANDONED
1644 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001645 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001646
Robert Phillips72152832017-01-25 17:31:35 -05001647 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001648
Brian Salomon2a943df2018-05-04 13:43:19 -04001649 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001650 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001651 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001652 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001653}
1654
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001655void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1656 const SkRect& bounds) {
1657 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1658 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001659 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001660}
1661
Brian Salomon031b0ba2019-05-23 11:05:26 -04001662void GrRenderTargetContext::asyncRescaleAndReadPixels(
1663 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1664 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1665 auto direct = fContext->priv().asDirectContext();
1666 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001667 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001668 return;
1669 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001670 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001671 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001672 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001673 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001674 if (this->asRenderTargetProxy()->framebufferOnly()) {
1675 callback(context, nullptr);
1676 return;
1677 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001678 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001679 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001680 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001681 return;
1682 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001683 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001684 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001685 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001686 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001687 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001688 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1689 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001690 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001691 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001692 backendFormatOfFinalContext, dstCT);
1693 // Fail if we can't read from the source surface's color type.
1694 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001695 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001696 return;
1697 }
Brian Salomon624f9062019-07-02 14:23:00 -04001698 // Fail if read color type does not have all of dstCT's color channels and those missing color
1699 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001700 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1701 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1702 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1703 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001704 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001705 return;
1706 }
1707
Brian Salomonbf6b9792019-08-21 09:38:10 -04001708 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001709 int x = srcRect.fLeft;
1710 int y = srcRect.fTop;
1711 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001712 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1713 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001714 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001715 return;
1716 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001717 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001718 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001719 x = y = 0;
1720 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001721 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1722 this->colorInfo().alphaType(),
1723 info.colorSpace(),
1724 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001725 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1726 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001727 // We flip or color convert by drawing and we don't currently support drawing to
1728 // kPremul.
1729 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001730 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001731 return;
1732 }
Greg Daniel40903af2020-01-30 14:55:05 -05001733 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001734 SkRect srcRectToDraw = SkRect::Make(srcRect);
1735 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001736 if (!texProxyView.asTextureProxy()) {
1737 texProxyView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
1738 this->origin(), this->colorInfo().colorType(),
1739 GrMipMapped::kNo, srcRect,
1740 SkBackingFit::kApprox, SkBudgeted::kNo);
1741 if (!texProxyView.asTextureProxy()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001742 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001743 return;
1744 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001745 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1746 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001747 tempRTC = GrRenderTargetContext::Make(
1748 direct, this->colorInfo().colorType(), info.refColorSpace(),
1749 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001750 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001751 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001752 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001753 return;
1754 }
Greg Daniel40903af2020-01-30 14:55:05 -05001755 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1756 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1757 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001758 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1759 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1760 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001761 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001762 }
1763 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001764 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001765 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1766 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001767}
1768
Brian Salomon9241a6d2019-10-03 13:26:54 -04001769class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1770public:
1771 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1772 ~AsyncReadResult() override {
1773 for (int i = 0; i < fPlanes.count(); ++i) {
1774 if (!fPlanes[i].fMappedBuffer) {
1775 delete[] static_cast<const char*>(fPlanes[i].fData);
1776 } else {
1777 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1778 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1779 }
1780 }
1781 }
1782
1783 int count() const override { return fPlanes.count(); }
1784 const void* data(int i) const override { return fPlanes[i].fData; }
1785 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1786
1787 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001788 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001789 size_t rowBytes,
1790 GrClientMappedBufferManager* manager) {
1791 SkASSERT(!result.fTransferBuffer->isMapped());
1792 const void* mappedData = result.fTransferBuffer->map();
1793 if (!mappedData) {
1794 return false;
1795 }
1796 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001797 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001798 result.fPixelConverter(convertedData.get(), mappedData);
1799 this->addCpuPlane(std::move(convertedData), rowBytes);
1800 result.fTransferBuffer->unmap();
1801 } else {
1802 manager->insert(result.fTransferBuffer);
1803 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1804 }
1805 return true;
1806 }
1807
1808 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1809 SkASSERT(data);
1810 SkASSERT(rowBytes > 0);
1811 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1812 }
1813
1814private:
1815 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1816 SkASSERT(data);
1817 SkASSERT(rowBytes > 0);
1818 SkASSERT(mappedBuffer);
1819 SkASSERT(mappedBuffer->isMapped());
1820 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1821 }
1822
1823 struct Plane {
1824 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1825 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1826 const void* fData;
1827 size_t fRowBytes;
1828 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1829 sk_sp<GrGpuBuffer> fMappedBuffer;
1830 };
1831 SkSTArray<3, Plane> fPlanes;
1832 uint32_t fInboxID;
1833};
1834
Brian Salomon024bd002019-06-11 11:38:16 -04001835void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1836 ReadPixelsCallback callback,
1837 ReadPixelsContext context) {
1838 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1839 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1840
Greg Danielca9dbe22020-01-02 13:44:30 -05001841 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001842 callback(context, nullptr);
1843 return;
1844 }
1845
Brian Salomon9241a6d2019-10-03 13:26:54 -04001846 auto directContext = fContext->priv().asDirectContext();
1847 SkASSERT(directContext);
1848 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1849
Brian Salomon024bd002019-06-11 11:38:16 -04001850 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1851
1852 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001853 auto ii = SkImageInfo::Make(rect.size(), colorType,
1854 this->colorInfo().alphaType(),
1855 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001856 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001857 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1858 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1859 result->addCpuPlane(std::move(data), pm.rowBytes());
1860
Brian Salomon1d435302019-07-01 13:05:28 -04001861 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001862 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001863 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001864 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001865 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001866 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001867 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001868
Brian Salomonab32f652019-05-10 14:24:50 -04001869 struct FinishContext {
1870 ReadPixelsCallback* fClientCallback;
1871 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001872 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001873 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001874 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001875 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001876 };
1877 // Assumption is that the caller would like to flush. We could take a parameter or require an
1878 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1879 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001880 auto* finishContext = new FinishContext{callback,
1881 context,
1882 rect.size(),
1883 colorType,
1884 mappedBufferManager,
1885 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001886 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001887 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001888 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001889 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1890 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1891 context->fMappedBufferManager)) {
1892 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001893 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001894 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001895 delete context;
1896 };
1897 GrFlushInfo flushInfo;
1898 flushInfo.fFinishedContext = finishContext;
1899 flushInfo.fFinishedProc = finishCallback;
1900 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1901}
1902
Brian Salomon9241a6d2019-10-03 13:26:54 -04001903void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1904 sk_sp<SkColorSpace> dstColorSpace,
1905 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001906 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001907 RescaleGamma rescaleGamma,
1908 SkFilterQuality rescaleQuality,
1909 ReadPixelsCallback callback,
1910 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001911 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1912 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001913 SkASSERT(!dstSize.isZero());
1914 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1915
Brian Salomon024bd002019-06-11 11:38:16 -04001916 auto direct = fContext->priv().asDirectContext();
1917 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001918 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001919 return;
1920 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001921 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001922 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001923 return;
1924 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001925 if (this->asRenderTargetProxy()->framebufferOnly()) {
1926 callback(context, nullptr);
1927 return;
1928 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001929 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001930 callback(context, nullptr);
1931 return;
1932 }
Brian Salomon024bd002019-06-11 11:38:16 -04001933 int x = srcRect.fLeft;
1934 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001935 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001936 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001937 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001938 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001939 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001940 dstColorSpace);
1941 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001942 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001943 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001944 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001945 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001946 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001947 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001948 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001949 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001950 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001951 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001952 srcView = this->readSurfaceView();
1953 if (!srcView.asTextureProxy()) {
1954 srcView = GrSurfaceProxy::Copy(fContext, fReadView.proxy(), this->origin(),
1955 this->colorInfo().colorType(), GrMipMapped::kNo,
1956 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1957 if (!srcView.asTextureProxy()) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001958 // If we can't get a texture copy of the contents then give up.
1959 callback(context, nullptr);
1960 return;
1961 }
1962 x = y = 0;
1963 }
Brian Salomon4d036892019-07-02 15:10:58 -04001964 // We assume the caller wants kPremul. There is no way to indicate a preference.
1965 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001966 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1967 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001968 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001969 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1970 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001971 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1972 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001973 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001974 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001975 return;
1976 }
Greg Daniel40903af2020-01-30 14:55:05 -05001977 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1978 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1979 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1980 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1981 SkMatrix::I(), std::move(xform));
1982 srcView = tempRTC->readSurfaceView();
1983 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001984 x = y = 0;
1985 }
1986 }
Greg Danielc594e622019-10-15 14:01:49 -04001987
Greg Daniele20fcad2020-01-08 11:52:34 -05001988 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1989 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1990 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001991 int halfW = dstSize.width()/2;
1992 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001993 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1994 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1995 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1996 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1997 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1998 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001999 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002000 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002001 return;
2002 }
2003
Brian Osman28ba5282019-10-30 14:18:07 -04002004 float baseM[20];
2005 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2006
Brian Salomon024bd002019-06-11 11:38:16 -04002007 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2008
2009 auto texMatrix = SkMatrix::MakeTrans(x, y);
2010
Brian Salomon9241a6d2019-10-03 13:26:54 -04002011 SkRect dstRectY = SkRect::Make(dstSize);
2012 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002013
Brian Salomon6aa65052020-01-28 12:16:53 -05002014 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2015 PixelTransferResult yTransfer, uTransfer, vTransfer;
2016
Brian Salomon024bd002019-06-11 11:38:16 -04002017 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2018 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002019 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002020 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002021 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002022 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002023 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002024 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2025 yPaint.addColorFragmentProcessor(std::move(yFP));
2026 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002027 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2028 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002029 if (!doSynchronousRead) {
2030 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2031 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2032 if (!yTransfer.fTransferBuffer) {
2033 callback(context, nullptr);
2034 return;
2035 }
Brian Salomon024bd002019-06-11 11:38:16 -04002036 }
2037
2038 texMatrix.preScale(2.f, 2.f);
2039 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2040 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002041 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002042 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002043 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002044 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002045 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002046 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2047 uPaint.addColorFragmentProcessor(std::move(uFP));
2048 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002049 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2050 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002051 if (!doSynchronousRead) {
2052 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2053 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2054 if (!uTransfer.fTransferBuffer) {
2055 callback(context, nullptr);
2056 return;
2057 }
Brian Salomon024bd002019-06-11 11:38:16 -04002058 }
2059
2060 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2061 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002062 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002063 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002064 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002065 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2066 this->colorInfo().alphaType(), texMatrix,
2067 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002068 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2069 vPaint.addColorFragmentProcessor(std::move(vFP));
2070 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002071 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2072 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002073 if (!doSynchronousRead) {
2074 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2075 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2076 if (!vTransfer.fTransferBuffer) {
2077 callback(context, nullptr);
2078 return;
2079 }
2080 }
2081
2082 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002083 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2084 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2085 size_t yRB = yInfo.minRowBytes();
2086 size_t uvRB = uvInfo.minRowBytes();
2087 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2088 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2089 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2090 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2091 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2092 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002093 callback(context, nullptr);
2094 return;
2095 }
2096 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002097 result->addCpuPlane(std::move(y), yRB );
2098 result->addCpuPlane(std::move(u), uvRB);
2099 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002100 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002101 return;
2102 }
2103
2104 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002105 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002106 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002107 GrClientMappedBufferManager* fMappedBufferManager;
2108 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002109 PixelTransferResult fYTransfer;
2110 PixelTransferResult fUTransfer;
2111 PixelTransferResult fVTransfer;
2112 };
2113 // Assumption is that the caller would like to flush. We could take a parameter or require an
2114 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2115 // callback to GrGpu until after the next flush that flushes our op list, though.
2116 auto* finishContext = new FinishContext{callback,
2117 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002118 direct->priv().clientMappedBufferManager(),
2119 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002120 std::move(yTransfer),
2121 std::move(uTransfer),
2122 std::move(vTransfer)};
2123 auto finishCallback = [](GrGpuFinishedContext c) {
2124 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002125 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002126 auto manager = context->fMappedBufferManager;
2127 size_t rowBytes = SkToSizeT(context->fSize.width());
2128 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2129 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002130 delete context;
2131 return;
2132 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002133 rowBytes /= 2;
2134 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2135 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2136 (*context->fClientCallback)(context->fClientContext, nullptr);
2137 delete context;
2138 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002139 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002140 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2141 (*context->fClientCallback)(context->fClientContext, nullptr);
2142 delete context;
2143 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002144 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002145 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002146 delete context;
2147 };
2148 GrFlushInfo flushInfo;
2149 flushInfo.fFinishedContext = finishContext;
2150 flushInfo.fFinishedProc = finishCallback;
2151 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002152}
2153
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002154GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2155 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002156 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002157 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002158 return GrSemaphoresSubmitted::kNo;
2159 }
robertphillips8c523e02016-07-26 07:41:00 -07002160 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002161 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002162
Greg Daniel55f040b2020-02-13 15:38:32 +00002163 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002164}
2165
Greg Danielc64ee462017-06-15 16:59:49 -04002166bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002167 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002168 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002169 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002170 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002171 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002172
2173 AutoCheckFlush acf(this->drawingManager());
2174
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002175 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002176 return false;
2177 }
2178
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002179 auto direct = fContext->priv().asDirectContext();
2180 if (!direct) {
2181 return false;
2182 }
2183
2184 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002185
Greg Daniel301015c2019-11-18 14:06:46 -05002186 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2187 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002188 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002189 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002190 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2191 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002192 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002193 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2194 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002195 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002196}
joshualitt33a5fce2015-11-18 13:28:51 -08002197
Brian Osman11052242016-10-27 14:47:55 -04002198void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002199 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002200 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002201 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002202 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002203 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002204 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002205 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002206 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002207 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2208
Brian Salomon40b77a62017-12-22 11:25:52 -05002209 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002210
Robert Phillips27927a52018-08-20 13:18:12 -04002211 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002212}
2213
2214void GrRenderTargetContext::drawShape(const GrClip& clip,
2215 GrPaint&& paint,
2216 GrAA aa,
2217 const SkMatrix& viewMatrix,
2218 const GrShape& shape) {
2219 ASSERT_SINGLE_OWNER
2220 RETURN_IF_ABANDONED
2221 SkDEBUGCODE(this->validate();)
2222 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2223
Brian Salomon2fad74a2017-12-20 13:28:55 -05002224 if (shape.isEmpty()) {
2225 if (shape.inverseFilled()) {
2226 this->drawPaint(clip, std::move(paint), viewMatrix);
2227 }
2228 return;
robertphillipsea461502015-05-26 11:38:03 -07002229 }
2230
Robert Phillips72152832017-01-25 17:31:35 -05002231 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002232
Brian Salomon2fad74a2017-12-20 13:28:55 -05002233 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002234 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002235 SkRRect rrect;
2236 // We can ignore the starting point and direction since there is no path effect.
2237 bool inverted;
2238 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2239 if (rrect.isRect()) {
2240 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2241 &shape.style());
2242 return;
2243 } else if (rrect.isOval()) {
2244 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002245 return;
2246 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002247 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2248 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002249 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2250 viewMatrix.rectStaysRect()) {
2251 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2252 // the matrix to all the points individually rather than just to the rect
2253 SkRect rects[2];
2254 if (shape.asNestedRects(rects)) {
2255 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002256 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002257 fContext, std::move(paint), viewMatrix, rects);
2258 if (op) {
2259 this->addDrawOp(clip, std::move(op));
2260 }
2261 // Returning here indicates that there is nothing to draw in this case.
2262 return;
2263 }
robertphillipsea461502015-05-26 11:38:03 -07002264 }
2265 }
robertphillips4bc31812016-03-01 12:22:49 -08002266
Brian Salomon2fad74a2017-12-20 13:28:55 -05002267 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002268}
2269
Chris Daltonbbfd5162017-11-07 13:35:22 -07002270bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002271 const GrUserStencilSettings* ss,
2272 SkRegion::Op op,
2273 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002274 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002275 const SkMatrix& viewMatrix,
2276 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002277 ASSERT_SINGLE_OWNER_PRIV
2278 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002279 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002280 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2281 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002282
2283 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002284 GrPaint paint;
2285 paint.setCoverageSetOpXPFactory(op, invert);
2286 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2287 SkRect::MakeIWH(fRenderTargetContext->width(),
2288 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002289 return true;
2290 }
2291
Robert Phillips72152832017-01-25 17:31:35 -05002292 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002293
2294 // An Assumption here is that path renderer would use some form of tweaking
2295 // the src color (either the input alpha or in the frag shader) to implement
2296 // aa. If we have some future driver-mojo path AA that can do the right
2297 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002298 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002299 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002300
Chris Daltondb91c6e2017-09-08 16:25:08 -06002301 SkIRect clipConservativeBounds;
2302 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2303 &clipConservativeBounds, nullptr);
2304
bsalomon8acedde2016-06-24 10:42:16 -07002305 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002306 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002307 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002308 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002309 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002310 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002311 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002312 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002313 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2314 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002315 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002316
2317 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002318 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002319 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002320 if (!pr) {
2321 return false;
2322 }
2323
2324 GrPaint paint;
2325 paint.setCoverageSetOpXPFactory(op, invert);
2326
Brian Salomonf3569f02017-10-24 12:52:33 -04002327 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2328 std::move(paint),
2329 ss,
2330 fRenderTargetContext,
2331 &clip,
2332 &clipConservativeBounds,
2333 &viewMatrix,
2334 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002335 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002336 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002337 pr->drawPath(args);
2338 return true;
2339}
2340
Brian Osman11052242016-10-27 14:47:55 -04002341SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002342 ASSERT_SINGLE_OWNER_PRIV
2343
Robert Phillips6a6de562019-02-15 15:19:15 -05002344 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002345 return SkBudgeted::kNo;
2346 }
2347
Brian Osman11052242016-10-27 14:47:55 -04002348 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002349
Greg Daniel3912a4b2020-01-14 09:56:04 -05002350 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002351}
2352
Brian Salomon2fad74a2017-12-20 13:28:55 -05002353void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2354 GrPaint&& paint,
2355 GrAA aa,
2356 const SkMatrix& viewMatrix,
2357 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002358 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002359 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002360 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2361
Jim Van Verthf86073a2018-10-02 13:05:38 -04002362 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2363 return;
2364 }
2365
Chris Daltondb91c6e2017-09-08 16:25:08 -06002366 SkIRect clipConservativeBounds;
2367 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2368
Brian Salomon2fad74a2017-12-20 13:28:55 -05002369 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002370 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002371
robertphillips68737822015-10-29 12:12:21 -07002372 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002373 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002374 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002375 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002376 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002377 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002378 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002379 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002380
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002381 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002382 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002383 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002384 return;
2385 }
2386
Chris Dalton6ce447a2019-06-23 18:07:38 -06002387 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002388
2389 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2390 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2391 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2392
Brian Salomon2fad74a2017-12-20 13:28:55 -05002393 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002394 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002395 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2396 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002397 return;
2398 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002399 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002400 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002401 }
2402 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002403 if (canDrawArgs.fShape->style().applies()) {
2404 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2405 styleScale);
2406 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002407 return;
2408 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002409 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002410 // This time, allow SW renderer
2411 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2412 } else {
2413 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002414 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002415 }
robertphillipsea461502015-05-26 11:38:03 -07002416
bsalomon8acedde2016-06-24 10:42:16 -07002417 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002418#ifdef SK_DEBUG
2419 SkDebugf("Unable to find path renderer compatible with path.\n");
2420#endif
2421 return;
2422 }
2423
Robert Phillips256c37b2017-03-01 14:32:46 -05002424 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002425 std::move(paint),
2426 &GrUserStencilSettings::kUnused,
2427 this,
2428 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002429 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002430 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002431 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002432 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002433 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002434 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002435}
2436
Brian Salomon467921e2017-03-06 16:17:12 -05002437static void op_bounds(SkRect* bounds, const GrOp* op) {
2438 *bounds = op->bounds();
2439 if (op->hasZeroArea()) {
2440 if (op->hasAABloat()) {
2441 bounds->outset(0.5f, 0.5f);
2442 } else {
2443 // We don't know which way the particular GPU will snap lines or points at integer
2444 // coords. So we ensure that the bounds is large enough for either snap.
2445 SkRect before = *bounds;
2446 bounds->roundOut(bounds);
2447 if (bounds->fLeft == before.fLeft) {
2448 bounds->fLeft -= 1;
2449 }
2450 if (bounds->fTop == before.fTop) {
2451 bounds->fTop -= 1;
2452 }
2453 if (bounds->fRight == before.fRight) {
2454 bounds->fRight += 1;
2455 }
2456 if (bounds->fBottom == before.fBottom) {
2457 bounds->fBottom += 1;
2458 }
2459 }
2460 }
2461}
2462
Chris Dalton08755122019-08-05 16:13:47 -06002463void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002464 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002465 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2466}
2467
Brian Salomon348a0372018-10-31 10:42:18 -04002468void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2469 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002470 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002471 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002472 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002473 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002474 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002475 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002476 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002477 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002478
Brian Salomon467921e2017-03-06 16:17:12 -05002479 // Setup clip
2480 SkRect bounds;
2481 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002482 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002483 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002484 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002485 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002486
Chris Dalton9acf6822019-11-12 11:52:40 -07002487 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002488 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002489 }
2490
Chris Dalton9acf6822019-11-12 11:52:40 -07002491 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002492 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002493 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002494 }
2495
Chris Dalton9acf6822019-11-12 11:52:40 -07002496 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2497 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2498
2499 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2500 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2501 // that uses a stencil test when the stencil buffer is multisampled.)
2502 bool hasMixedSampledCoverage = (
2503 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002504 SkASSERT(!hasMixedSampledCoverage ||
2505 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002506
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002507 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002508 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002509 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2510
Greg Daniel524e28b2019-11-01 11:48:53 -04002511 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002512 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002513 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002514 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002515 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002516 }
2517 }
2518
2519 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002520 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002521 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002522 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002523 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002524 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002525 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002526}
2527
Greg Daniel524e28b2019-11-01 11:48:53 -04002528bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2529 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002530 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2531 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2532 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002533 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002534 return false;
2535 }
2536
Greg Daniel3912a4b2020-01-14 09:56:04 -05002537 if (this->caps()->textureBarrierSupport() &&
2538 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002539 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002540 // The render target is a texture, so we can read from it directly in the shader. The XP
2541 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002542 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002543 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002544 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002545 }
2546 }
2547
Greg Daniel3912a4b2020-01-14 09:56:04 -05002548 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002549
Eric Karl74480882017-04-03 14:49:05 -07002550 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002551 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002552 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002553 SkRect opBounds = op.bounds();
2554 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2555 // 0.5 pixels.
2556 if (op.hasAABloat() || op.hasZeroArea()) {
2557 opBounds.outset(0.5f, 0.5f);
2558 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2559 // performance we may ignore the clip when the draw is entirely inside the clip is float
2560 // space but will hit pixels just outside the clip when actually rasterizing.
2561 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002562 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002563 }
2564 SkIRect opIBounds;
2565 opBounds.roundOut(&opIBounds);
2566 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002567#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002568 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002569#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002570 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002571 }
2572
Greg Daniel3155f7f2020-01-16 16:54:26 -05002573 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002574 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2575 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002576 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002577 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002578
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002579 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002580 copyRect = clippedRect;
2581 }
Brian Salomon467921e2017-03-06 16:17:12 -05002582
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002583 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002584 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002585 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002586 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002587 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002588 } else {
Eric Karl74480882017-04-03 14:49:05 -07002589 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002590 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002591 }
Greg Daniel40903af2020-01-30 14:55:05 -05002592 GrSurfaceProxyView newProxyView =
2593 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), colorType,
2594 GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2595 restrictions.fRectsMustMatch);
2596 SkASSERT(newProxyView.proxy());
Robert Phillipsbf25d432017-04-07 10:08:53 -04002597
Greg Daniel40903af2020-01-30 14:55:05 -05002598 dstProxyView->setProxyView(std::move(newProxyView));
Greg Daniel524e28b2019-11-01 11:48:53 -04002599 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002600 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002601}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002602
Greg Daniel573312e2020-02-07 17:22:35 -05002603bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002604 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002605 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002606 SkIRect clippedSrcRect;
2607 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002608 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2609 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002610 return false;
2611 }
2612
2613 GrPaint paint;
2614 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002615
Greg Danield2ccbb52020-02-05 10:45:39 -05002616 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002617 if (!fp) {
2618 return false;
2619 }
2620 paint.addColorFragmentProcessor(std::move(fp));
2621
2622 this->fillRectToRect(
2623 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2624 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2625 clippedSrcRect.height()),
2626 SkRect::Make(clippedSrcRect));
2627 return true;
2628}