blob: 6730cf2b93d229a4f3d88d33e6e5b76f6823b242 [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"
Brian Salomond005b692020-04-01 15:47:05 -040013#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/GrRecordingContext.h"
15#include "include/private/SkShadowFlags.h"
16#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040017#include "src/core/SkAutoPixmapStorage.h"
18#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkDrawShadowInfo.h"
20#include "src/core/SkGlyphRunPainter.h"
21#include "src/core/SkLatticeIter.h"
22#include "src/core/SkMatrixPriv.h"
23#include "src/core/SkRRectPriv.h"
24#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040025#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrBlurUtils.h"
29#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040030#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040033#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrDrawingManager.h"
35#include "src/gpu/GrFixedClip.h"
36#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040037#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040038#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrMemoryPool.h"
40#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040042#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/GrRenderTargetContextPriv.h"
44#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/GrStencilAttachment.h"
46#include "src/gpu/GrStyle.h"
47#include "src/gpu/GrTracing.h"
48#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040049#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040051#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040052#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000053#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040054#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#include "src/gpu/ops/GrAtlasTextOp.h"
56#include "src/gpu/ops/GrClearOp.h"
57#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058#include "src/gpu/ops/GrDrawAtlasOp.h"
59#include "src/gpu/ops/GrDrawOp.h"
60#include "src/gpu/ops/GrDrawVerticesOp.h"
61#include "src/gpu/ops/GrDrawableOp.h"
62#include "src/gpu/ops/GrFillRRectOp.h"
63#include "src/gpu/ops/GrFillRectOp.h"
64#include "src/gpu/ops/GrLatticeOp.h"
65#include "src/gpu/ops/GrOp.h"
66#include "src/gpu/ops/GrOvalOpFactory.h"
67#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068#include "src/gpu/ops/GrShadowRRectOp.h"
69#include "src/gpu/ops/GrStencilPathOp.h"
70#include "src/gpu/ops/GrStrokeRectOp.h"
71#include "src/gpu/ops/GrTextureOp.h"
72#include "src/gpu/text/GrTextContext.h"
73#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040074
Herb Derbyc1b482c2018-08-09 15:02:27 -040075class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040076public:
77 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040078 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040079 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040080 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040081 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040082
Robert Phillips7c525e62018-06-12 10:11:12 -040083 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040084 fRenderTargetContext->addDrawOp(clip, std::move(op));
85 }
86
Robert Phillips46a13382018-08-23 13:53:01 -040087 void drawShape(const GrClip& clip, const SkPaint& paint,
88 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040089 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
90 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040091 }
92
93 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040094 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050095 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040096 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040097 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040098 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040099 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400100 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400101 }
102 }
103
Robert Phillips69893702019-02-22 11:16:30 -0500104 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400105 return fRenderTargetContext->fContext;
106 }
107
Herb Derby65956872018-08-21 16:55:04 -0400108 SkGlyphRunListPainter* glyphPainter() override {
109 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400110 }
111
Brian Salomonf18b1d82017-10-27 11:30:49 -0400112private:
113 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400115
Brian Salomonf18b1d82017-10-27 11:30:49 -0400116};
joshualittbc907352016-01-13 06:45:40 -0800117
Robert Phillips72152832017-01-25 17:31:35 -0500118#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800119#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400120 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800121#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400122 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500123#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
124#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
125#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
126#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
127#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700128
Brian Salomone225b562017-06-14 13:00:03 -0400129//////////////////////////////////////////////////////////////////////////////
130
robertphillipsea461502015-05-26 11:38:03 -0700131class AutoCheckFlush {
132public:
halcanary9d524f22016-03-29 09:03:52 -0700133 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700134 SkASSERT(fDrawingManager);
135 }
bsalomonb77a9072016-09-07 10:02:04 -0700136 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700137
138private:
robertphillips77a2e522015-10-17 07:43:27 -0700139 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700140};
141
Greg Daniele20fcad2020-01-08 11:52:34 -0500142std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
143 GrRecordingContext* context,
144 GrColorType colorType,
145 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500146 sk_sp<GrSurfaceProxy> proxy,
147 GrSurfaceOrigin origin,
148 const SkSurfaceProps* surfaceProps,
149 bool managedOps) {
150 if (!proxy) {
151 return nullptr;
152 }
153
154 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400155 GrSwizzle readSwizzle, writeSwizzle;
156 if (colorType != GrColorType::kUnknown) {
157 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
158 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
159 }
Greg Danielba0ff782020-01-07 15:42:57 -0500160
Greg Daniel3912a4b2020-01-14 09:56:04 -0500161 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400162 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500163
164 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400165 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500166 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500167}
168
169std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
170 GrRecordingContext* context,
171 GrColorType colorType,
172 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500173 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500174 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500175 const GrBackendFormat& format,
176 int sampleCnt,
177 GrMipMapped mipMapped,
178 GrProtected isProtected,
179 GrSurfaceOrigin origin,
180 SkBudgeted budgeted,
181 const SkSurfaceProps* surfaceProps) {
182 // It is probably not necessary to check if the context is abandoned here since uses of the
183 // GrRenderTargetContext which need the context will mostly likely fail later on without an
184 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
185 // an abandoned context correctly. It also lets us early out of some extra work.
186 if (context->priv().abandoned()) {
187 return nullptr;
188 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500189
Greg Daniele20fcad2020-01-08 11:52:34 -0500190 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400191 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500192 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 if (!proxy) {
194 return nullptr;
195 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500196
Greg Danielba0ff782020-01-07 15:42:57 -0500197 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
198 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500199 if (!rtc) {
200 return nullptr;
201 }
202 rtc->discard();
203 return rtc;
204}
205
206std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
207 GrRecordingContext* context,
208 GrColorType colorType,
209 sk_sp<SkColorSpace> colorSpace,
210 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500211 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500212 int sampleCnt,
213 GrMipMapped mipMapped,
214 GrProtected isProtected,
215 GrSurfaceOrigin origin,
216 SkBudgeted budgeted,
217 const SkSurfaceProps* surfaceProps) {
218 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
219 if (!format.isValid()) {
220 return nullptr;
221 }
222
223 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
224 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
225 surfaceProps);
226}
227
228static inline GrColorType color_type_fallback(GrColorType ct) {
229 switch (ct) {
230 // kRGBA_8888 is our default fallback for many color types that may not have renderable
231 // backend formats.
232 case GrColorType::kAlpha_8:
233 case GrColorType::kBGR_565:
234 case GrColorType::kABGR_4444:
235 case GrColorType::kBGRA_8888:
236 case GrColorType::kRGBA_1010102:
237 case GrColorType::kRGBA_F16:
238 case GrColorType::kRGBA_F16_Clamped:
239 return GrColorType::kRGBA_8888;
240 case GrColorType::kAlpha_F16:
241 return GrColorType::kRGBA_F16;
242 case GrColorType::kGray_8:
243 return GrColorType::kRGB_888x;
244 default:
245 return GrColorType::kUnknown;
246 }
247}
248
Brian Salomond005b692020-04-01 15:47:05 -0400249std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400250 GrImageContext* context, GrColorType colorType, int sampleCnt) {
251 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400252 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400253 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
254 // We continue to the fallback color type if there no default renderable format or we
255 // requested msaa and the format doesn't support msaa.
256 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400257 return {colorType, format};
258 }
259 colorType = color_type_fallback(colorType);
260 } while (colorType != GrColorType::kUnknown);
261 return {GrColorType::kUnknown, {}};
262}
263
Greg Daniele20fcad2020-01-08 11:52:34 -0500264std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
265 GrRecordingContext* context,
266 GrColorType colorType,
267 sk_sp<SkColorSpace> colorSpace,
268 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500269 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500270 int sampleCnt,
271 GrMipMapped mipMapped,
272 GrProtected isProtected,
273 GrSurfaceOrigin origin,
274 SkBudgeted budgeted,
275 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400276 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400277 if (ct == GrColorType::kUnknown) {
278 return nullptr;
279 }
280 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
281 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500282}
283
Greg Danielba0ff782020-01-07 15:42:57 -0500284std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
285 GrRecordingContext* context,
286 GrColorType colorType,
287 sk_sp<SkColorSpace> colorSpace,
288 const GrBackendTexture& tex,
289 int sampleCnt,
290 GrSurfaceOrigin origin,
291 const SkSurfaceProps* surfaceProps,
292 ReleaseProc releaseProc,
293 ReleaseContext releaseCtx) {
294 SkASSERT(sampleCnt > 0);
295 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400296 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500297 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500298 if (!proxy) {
299 return nullptr;
300 }
301
302 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
303 origin, surfaceProps);
304}
305
306std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
307 GrRecordingContext* context,
308 GrColorType colorType,
309 sk_sp<SkColorSpace> colorSpace,
310 const GrBackendTexture& tex,
311 int sampleCnt,
312 GrSurfaceOrigin origin,
313 const SkSurfaceProps* surfaceProps) {
314 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400315 sk_sp<GrSurfaceProxy> proxy(
316 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500317 if (!proxy) {
318 return nullptr;
319 }
320
321 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
322 origin, surfaceProps);
323}
324
325std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
326 GrRecordingContext* context,
327 GrColorType colorType,
328 sk_sp<SkColorSpace> colorSpace,
329 const GrBackendRenderTarget& rt,
330 GrSurfaceOrigin origin,
331 const SkSurfaceProps* surfaceProps,
332 ReleaseProc releaseProc,
333 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400334 sk_sp<GrSurfaceProxy> proxy(
335 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500336 if (!proxy) {
337 return nullptr;
338 }
339
340 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
341 origin, surfaceProps);
342}
343
344std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
345 GrRecordingContext* context,
346 const SkImageInfo& imageInfo,
347 const GrVkDrawableInfo& vkInfo,
348 const SkSurfaceProps* props) {
349 sk_sp<GrSurfaceProxy> proxy(
350 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
351 vkInfo));
352 if (!proxy) {
353 return nullptr;
354 }
355
Greg Danielba0ff782020-01-07 15:42:57 -0500356 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
357 imageInfo.refColorSpace(), std::move(proxy),
358 kTopLeft_GrSurfaceOrigin, props);
359}
360
Greg Danielf41b2bd2019-08-22 16:19:24 -0400361// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
362// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
363// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
364// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500365GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500366 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400367 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400368 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400369 sk_sp<SkColorSpace> colorSpace,
370 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400371 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500372 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
373 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400374 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500375 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400376 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400377 , fManagedOpsTask(managedOpsTask) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400378 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
379 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500380
Brian Salomonf18b1d82017-10-27 11:30:49 -0400381 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700382 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700383}
384
robertphillips2e1e51f2015-10-15 08:01:48 -0700385#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500386void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400387 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400388 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700389 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700390}
391#endif
392
Brian Osman11052242016-10-27 14:47:55 -0400393GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800394 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700395}
396
Chris Dalton7d6748e2019-03-13 00:34:52 -0600397inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600398 if (GrAA::kNo == aa) {
399 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
400 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600401 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600402 return GrAAType::kMSAA;
403 }
404 return GrAAType::kNone;
405 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600406 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600407}
408
Greg Daniele252f082017-10-23 16:05:23 -0400409GrMipMapped GrRenderTargetContext::mipMapped() const {
410 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
411 return proxy->mipMapped();
412 }
413 return GrMipMapped::kNo;
414}
415
Greg Danielf41b2bd2019-08-22 16:19:24 -0400416GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800417 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700418 SkDEBUGCODE(this->validate();)
419
Greg Danielf41b2bd2019-08-22 16:19:24 -0400420 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600421 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400422 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600423 if (fOpsTask && fNumStencilSamples > 0) {
424 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600425 fOpsTask->setMustPreserveStencil();
426 // Reload the stencil buffer content at the beginning of newOpsTask.
427 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
428 // values?
429 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
430 }
431 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700432 }
433
Greg Danielf41b2bd2019-08-22 16:19:24 -0400434 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700435}
436
Herb Derbycddab252018-07-16 11:19:04 -0400437void GrRenderTargetContext::drawGlyphRunList(
438 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400439 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800440 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700441 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700442 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400443 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700444
Greg Danielbe7fc462019-01-03 16:40:42 -0500445 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
446 // secondary command buffers because it would require stopping and starting a render pass which
447 // we don't have access to.
448 if (this->wrapsVkSecondaryCB()) {
449 return;
450 }
451
Herb Derby26cbe512018-05-24 14:39:01 -0400452 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400453 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400454 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700455}
456
Brian Osman11052242016-10-27 14:47:55 -0400457void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800458 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700459 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700460 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400461 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700462
Robert Phillips72152832017-01-25 17:31:35 -0500463 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400464
Greg Danielf41b2bd2019-08-22 16:19:24 -0400465 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700466}
467
Brian Osman11052242016-10-27 14:47:55 -0400468void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500469 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700470 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800471 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700472 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700473 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400474 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700475
Robert Phillips72152832017-01-25 17:31:35 -0500476 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700477 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
478 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700479}
robertphillips9199a9f2016-07-13 07:48:43 -0700480
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500481void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
482 const SkPMColor4f& color,
483 CanClearFullscreen canClearFullscreen) {
484 ASSERT_SINGLE_OWNER_PRIV
485 RETURN_IF_ABANDONED_PRIV
486 SkDEBUGCODE(fRenderTargetContext->validate();)
487 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
488 fRenderTargetContext->fContext);
489
490 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
491 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
492}
493
494static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
495 paint->setColor4f(color);
496 if (color.isOpaque()) {
497 // Can just rely on the src-over blend mode to do the right thing
498 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
499 } else {
500 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
501 // were src blended
502 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
503 }
504}
505
506void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
507 const SkPMColor4f& color,
508 CanClearFullscreen canClearFullscreen) {
509 bool isFull = false;
510 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400511 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
512 // only clear the entire target if we knew it had not been cleared before. As
513 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500514 isFull = !clip.scissorEnabled() ||
515 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400516 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500517 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
518 }
519
520 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400521 GrOpsTask* opsTask = this->getOpsTask();
522 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500523 !this->caps()->performColorClearsAsDraws()) {
524 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400525 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500526 return;
527 } else {
528 // Will use an op for the clear, reset the load op to discard since the op will
529 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400530 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500531 }
532
533 // Must add an op to the list (either because we couldn't use a load op, or because the
534 // clear load op isn't supported)
535 if (this->caps()->performColorClearsAsDraws()) {
536 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
537 GrPaint paint;
538 clear_to_grpaint(color, &paint);
539 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400540 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
541 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500542 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600543 this->addOp(GrClearOp::Make(
544 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500545 }
546 } else {
547 if (this->caps()->performPartialClearsAsDraws()) {
548 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500549 GrPaint paint;
550 clear_to_grpaint(color, &paint);
551
Michael Ludwig64b28a72019-05-28 12:02:00 -0400552 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400553 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
554 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 } else {
556 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
557 this->asSurfaceProxy()));
558 // This version of the clear op factory can return null if the clip doesn't intersect
559 // with the surface proxy's boundary
560 if (!op) {
561 return;
562 }
Chris Dalton08755122019-08-05 16:13:47 -0600563 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500564 }
565 }
566}
567
Brian Osman11052242016-10-27 14:47:55 -0400568void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500569 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400570 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000571 // Start with the render target, since that is the maximum content we could possibly fill.
572 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500573 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400574 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000575 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
576 // inverse view matrix.
577 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
578 } else {
579 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
580 SkMatrix localMatrix;
581 if (!viewMatrix.invert(&localMatrix)) {
582 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400583 }
Michael Ludwig61328202019-06-19 14:48:58 +0000584 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
585 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700586 }
robertphillipsea461502015-05-26 11:38:03 -0700587}
588
Michael Ludwig61328202019-06-19 14:48:58 +0000589enum class GrRenderTargetContext::QuadOptimization {
590 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
591 kDiscarded,
592 // The rect to draw was converted to some other op and appended to the oplist, so no additional
593 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
594 // a constColor is provided.
595 kSubmitted,
596 // The clip was folded into the device quad, with updated edge flags and local coords, and
597 // caller is responsible for adding an appropriate op.
598 kClipApplied,
599 // No change to clip, but quad updated to better fit clip/render target, and caller is
600 // responsible for adding an appropriate op.
601 kCropped
602};
Michael Ludwig61a16512019-01-15 11:15:13 -0500603
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400604static bool make_vertex_finite(float* value) {
605 if (SkScalarIsNaN(*value)) {
606 return false;
607 }
608
609 if (!SkScalarIsFinite(*value)) {
610 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
611 // left when calculating crops.
612 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
613 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
614 }
615
616 return true;
617}
618
Michael Ludwig61328202019-06-19 14:48:58 +0000619GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400620 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500621 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000622 // Optimization requirements:
623 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500624 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
625 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
626 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
627 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
628 // 5. kCropped in all other scenarios (although a crop may be a no-op)
629
630 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
631 // better to just keep the old flags instead of introducing mixed edge flags.
632 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000633
Michael Ludwige08b4432019-06-19 18:00:48 -0400634 SkRect rtRect;
635 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400636 // Must use size at which the rendertarget will ultimately be allocated so that stencil
637 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500638 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400639 } else {
640 // Use the logical size of the render target, which allows for "fullscreen" clears even if
641 // the render target has an approximate backing fit
642 rtRect = SkRect::MakeWH(this->width(), this->height());
643 }
644
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500645 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000646 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400647 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
648 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500649 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400650 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500651 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
652 !make_vertex_finite(quad->fDevice.ys() + i) ||
653 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400654 // Discard if we see a nan
655 return QuadOptimization::kDiscarded;
656 }
657 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500658 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400659 }
660 } else {
661 // CropToRect requires the quads to be finite. If they are not finite and we have local
662 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500663 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400664 return QuadOptimization::kDiscarded;
665 }
Brian Salomon7694b902019-06-18 21:00:21 +0000666 }
667
Michael Ludwig61328202019-06-19 14:48:58 +0000668 // If the quad is entirely off screen, it doesn't matter what the clip does
669 if (!rtRect.intersects(drawBounds)) {
670 return QuadOptimization::kDiscarded;
671 }
Brian Salomon7694b902019-06-18 21:00:21 +0000672
Michael Ludwig61328202019-06-19 14:48:58 +0000673 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
674 // the render target).
675 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400676 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
677 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
678 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
679 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000680 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000681 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000682 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
683 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000684 }
Brian Salomon7694b902019-06-18 21:00:21 +0000685 }
686
Michael Ludwig61328202019-06-19 14:48:58 +0000687 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
688 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400689 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000690 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
691 // of the render target and the clip rect)
692 SkRect clipBounds = rtRect;
693 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
694 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000695 }
696
Michael Ludwig61328202019-06-19 14:48:58 +0000697 if (clipRRect.isRect()) {
698 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500699 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700700 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500701 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000702 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500703 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000704 if (drawBounds.contains(rtRect)) {
705 // Fullscreen clear
706 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
707 return QuadOptimization::kSubmitted;
708 } else if (GrClip::IsPixelAligned(drawBounds) &&
709 drawBounds.width() > 256 && drawBounds.height() > 256) {
710 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
711 SkIRect scissorRect;
712 drawBounds.round(&scissorRect);
713 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
714 return QuadOptimization::kSubmitted;
715 }
716 }
717
718 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000719 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500720 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000721 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
722 // properly reflect the smooth edge of the clip.
723 *aa = GrAA::kYes;
724 }
725 // We intentionally do not downgrade AA here because we don't know if we need to
726 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
727 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
728
729 // deviceQuad is exactly the intersection of original quad and clip, so it can be
730 // drawn with no clip (submitted by caller)
731 return QuadOptimization::kClipApplied;
732 } else {
733 // The quads have been updated to better fit the clip bounds, but can't get rid of
734 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500735 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000736 return QuadOptimization::kCropped;
737 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700738 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000739 // Rounded corners and constant filled color (limit ourselves to solid colors because
740 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500741 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
742 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
743 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000744 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
745 // we can draw the rrect directly and ignore the edge flags
746 GrPaint paint;
747 clear_to_grpaint(*constColor, &paint);
748 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
749 clipRRect, GrStyle::SimpleFill());
750 return QuadOptimization::kSubmitted;
751 } else {
752 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500753 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000754 return QuadOptimization::kCropped;
755 }
Brian Salomon7694b902019-06-18 21:00:21 +0000756 }
Brian Salomon7694b902019-06-18 21:00:21 +0000757 }
758
Michael Ludwig61328202019-06-19 14:48:58 +0000759 // Crop the quad to the conservative bounds of the clip.
760 SkIRect clipDevBounds;
761 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
762 SkRect clipBounds = SkRect::Make(clipDevBounds);
763
764 // One final check for discarding, since we may have gone here directly due to a complex clip
765 if (!clipBounds.intersects(drawBounds)) {
766 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000767 }
768
Michael Ludwig61328202019-06-19 14:48:58 +0000769 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
770 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500771 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
772 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000773
774 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000775}
776
Michael Ludwig61328202019-06-19 14:48:58 +0000777void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500778 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500779 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500780 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400781 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000782 ASSERT_SINGLE_OWNER
783 RETURN_IF_ABANDONED
784 SkDEBUGCODE(this->validate();)
785 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500786
Michael Ludwig61328202019-06-19 14:48:58 +0000787 AutoCheckFlush acf(this->drawingManager());
788
789 SkPMColor4f* constColor = nullptr;
790 SkPMColor4f paintColor;
791 if (!ss && !paint.numCoverageFragmentProcessors() &&
792 paint.isConstantBlendedColor(&paintColor)) {
793 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
794 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500795 }
796
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500797 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000798 if (opt >= QuadOptimization::kClipApplied) {
799 // These optimizations require caller to add an op themselves
800 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
801 : clip;
802 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
803 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500804 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
805 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700806 }
Michael Ludwig61328202019-06-19 14:48:58 +0000807 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800808}
809
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000810void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400811 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500812 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000813 sk_sp<GrColorSpaceXform> textureXform,
814 GrSamplerState::Filter filter,
815 const SkPMColor4f& color,
816 SkBlendMode blendMode,
817 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500818 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000819 const SkRect* domain) {
820 ASSERT_SINGLE_OWNER
821 RETURN_IF_ABANDONED
822 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400823 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000824 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
825
826 AutoCheckFlush acf(this->drawingManager());
827
828 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
829 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500830 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000831
832 SkASSERT(opt != QuadOptimization::kSubmitted);
833 if (opt != QuadOptimization::kDiscarded) {
834 // And the texture op if not discarded
835 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
836 : clip;
837 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400838 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400839 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
840 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000841 // Use the provided domain, although hypothetically we could detect that the cropped local
842 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500843 this->addDrawOp(finalClip,
844 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
845 std::move(textureXform), filter, color, saturate,
846 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000847 }
848}
849
Brian Osman11052242016-10-27 14:47:55 -0400850void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500851 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500852 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400853 const SkMatrix& viewMatrix,
854 const SkRect& rect,
855 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700856 if (!style) {
857 style = &GrStyle::SimpleFill();
858 }
joshualitt1de610a2016-01-06 08:26:09 -0800859 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700860 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700861 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400862 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700863
bsalomon6663acf2016-05-10 09:14:17 -0700864 // Path effects should've been devolved to a path in SkGpuDevice
865 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700866
Robert Phillips72152832017-01-25 17:31:35 -0500867 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700868
bsalomon6663acf2016-05-10 09:14:17 -0700869 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400870 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000871 // Fills the rect, using rect as its own local coordinates
872 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500873 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700874 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
875 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
876 if ((!rect.width() || !rect.height()) &&
877 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
878 SkScalar r = stroke.getWidth() / 2;
879 // TODO: Move these stroke->fill fallbacks to GrShape?
880 switch (stroke.getJoin()) {
881 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500882 this->drawRect(
883 clip, std::move(paint), aa, viewMatrix,
884 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
885 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700886 return;
887 case SkPaint::kRound_Join:
888 // Raster draws nothing when both dimensions are empty.
889 if (rect.width() || rect.height()){
890 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500891 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
892 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700893 return;
894 }
895 case SkPaint::kBevel_Join:
896 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500897 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700898 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
899 &GrStyle::SimpleFill());
900 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500901 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700902 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
903 &GrStyle::SimpleFill());
904 }
905 return;
906 }
907 }
robertphillips44302392016-07-08 14:43:03 -0700908
Brian Salomonbaaf4392017-06-15 09:59:23 -0400909 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700910
Chris Dalton7d6748e2019-03-13 00:34:52 -0600911 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500912 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
913 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
914 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500915 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400916 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700917 return;
robertphillips4bc31812016-03-01 12:22:49 -0800918 }
robertphillips4bc31812016-03-01 12:22:49 -0800919 }
Mike Klein16885072018-12-11 09:54:31 -0500920 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500921 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700922}
923
Michael Ludwig69858532018-11-28 15:34:34 -0500924void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
925 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
926 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600927 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500928
929 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
930 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500931}
932
Robert Phillipsec2249f2016-11-09 08:54:35 -0500933int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500934 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400935 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500936}
937
Greg Danielf41b2bd2019-08-22 16:19:24 -0400938GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600939 ) const {
940#if GR_TEST_UTILS
941 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400942 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600943 }
944#endif
945 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
946 // would normally be overwritten. The one exception is if the render target context is marked as
947 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
948 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
949 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
950 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400951 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600952}
953
Chris Dalton858cf232019-10-14 16:20:00 -0600954void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600955 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600956 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600957 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600958
Chris Daltoneffee202019-07-01 22:28:03 -0600959 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600960 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500961 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600962 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400963 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600964 }
965 SkASSERT(numRequiredSamples > 0);
966
967 if (numRequiredSamples > fNumStencilSamples) {
968 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500969 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600970 }
Chris Dalton6b982802019-06-27 13:53:46 -0600971
Chris Dalton674f77a2019-09-30 20:49:39 -0600972 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600973 if (this->caps()->performStencilClearsAsDraws()) {
974 // There is a driver bug with clearing stencil. We must use an op to manually clear the
975 // stencil buffer before the op that required 'setNeedsStencil'.
976 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
977 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600978 this->getOpsTask()->setInitialStencilContent(
979 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600980 }
981 }
982}
983
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000984void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700985 ASSERT_SINGLE_OWNER_PRIV
986 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400987 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400988 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
989 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700990
Robert Phillips72152832017-01-25 17:31:35 -0500991 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400992
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500993 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
994}
995
996void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600997 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
998
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500999 if (this->caps()->performStencilClearsAsDraws()) {
1000 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
1001 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
1002
1003 // Configure the paint to have no impact on the color buffer
1004 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001005 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001006 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1007 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001008 } else {
1009 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001010 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001011 if (!op) {
1012 return;
1013 }
Chris Dalton08755122019-08-05 16:13:47 -06001014 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001015 }
robertphillips976f5f02016-06-03 10:59:20 -07001016}
1017
Chris Daltonbbfd5162017-11-07 13:35:22 -07001018void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001019 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001020 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001021 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001022 ASSERT_SINGLE_OWNER_PRIV
1023 RETURN_IF_ABANDONED_PRIV
1024 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001025 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1026 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001027
Brian Salomon467921e2017-03-06 16:17:12 -05001028 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1029 SkASSERT(path);
1030 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1031
1032 // FIXME: Use path bounds instead of this WAR once
1033 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1034 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1035
1036 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001037 GrAppliedHardClip appliedClip;
1038 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1039 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001040 return;
1041 }
1042
Robert Phillips7c525e62018-06-12 10:11:12 -04001043 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1044 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001045 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001046 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001047 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001048 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001049 if (!op) {
1050 return;
1051 }
Brian Salomon97180af2017-03-14 13:42:58 -04001052 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001053
Chris Daltoneffee202019-07-01 22:28:03 -06001054 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001055 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001056}
1057
Michael Ludwig379e4962019-12-06 13:21:26 -05001058void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1059 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001060 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001061 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1062 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001063 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001064 ASSERT_SINGLE_OWNER
1065 RETURN_IF_ABANDONED
1066 SkDEBUGCODE(this->validate();)
1067 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001068
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001069 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1070 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1071 AutoCheckFlush acf(this->drawingManager());
1072 GrAAType aaType = this->chooseAAType(aa);
1073 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1074 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1075 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001076 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1077 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001078}
1079
Brian Osman11052242016-10-27 14:47:55 -04001080void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001081 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001082 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001083 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001084 GrPrimitiveType* overridePrimType,
1085 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001086 ASSERT_SINGLE_OWNER
1087 RETURN_IF_ABANDONED
1088 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001089 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001090
1091 AutoCheckFlush acf(this->drawingManager());
1092
1093 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001094 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001095 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001096 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001097 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001098 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001099}
1100
1101///////////////////////////////////////////////////////////////////////////////
1102
Brian Osman4d92b892019-03-24 00:53:23 +00001103void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1104 GrPaint&& paint,
1105 const SkMatrix& viewMatrix,
1106 int spriteCount,
1107 const SkRSXform xform[],
1108 const SkRect texRect[],
1109 const SkColor colors[]) {
1110 ASSERT_SINGLE_OWNER
1111 RETURN_IF_ABANDONED
1112 SkDEBUGCODE(this->validate();)
1113 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1114
1115 AutoCheckFlush acf(this->drawingManager());
1116
1117 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1118 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1119 aaType, spriteCount, xform, texRect, colors);
1120 this->addDrawOp(clip, std::move(op));
1121}
1122
1123///////////////////////////////////////////////////////////////////////////////
1124
Brian Osman11052242016-10-27 14:47:55 -04001125void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001126 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001127 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001128 const SkMatrix& viewMatrix,
1129 const SkRRect& rrect,
1130 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001131 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001132 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001133 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001134 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001135
1136 const SkStrokeRec& stroke = style.strokeRec();
1137 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001138 return;
1139 }
1140
bsalomon7f0d9f32016-08-15 14:49:10 -07001141 GrNoClip noclip;
1142 const GrClip* clip = &origClip;
1143#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1144 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001145 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001146 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1147 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001148 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1149 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001150 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001151 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1152 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001153 clip = &noclip;
1154 }
1155#endif
bsalomon6663acf2016-05-10 09:14:17 -07001156 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001157
Robert Phillips72152832017-01-25 17:31:35 -05001158 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001159
Chris Dalton7d6748e2019-03-13 00:34:52 -06001160 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001161
Chris Dalton0dffbab2019-03-27 13:08:50 -06001162 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001163 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1164 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1165 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1166 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1167 // to avoid perf regressions on some platforms.
1168 assert_alive(paint);
1169 op = GrOvalOpFactory::MakeCircularRRectOp(
1170 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1171 }
1172 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001173 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001174 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001175 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001176 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001177 assert_alive(paint);
1178 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001179 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001180 }
1181 if (op) {
1182 this->addDrawOp(*clip, std::move(op));
1183 return;
robertphillipsea461502015-05-26 11:38:03 -07001184 }
robertphillipsb56f9272016-02-25 11:03:52 -08001185
Mike Klein16885072018-12-11 09:54:31 -05001186 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001187 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1188 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001189}
1190
Jim Van Verthc5903412016-11-17 15:27:09 -05001191///////////////////////////////////////////////////////////////////////////////
1192
Jim Van Verth3af1af92017-05-18 15:06:54 -04001193static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1194 SkPoint3 result;
1195 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1196 result.fZ = pt.fZ;
1197 return result;
1198}
1199
1200bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001201 const SkMatrix& viewMatrix,
1202 const SkPath& path,
1203 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001204 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001205 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001206 return true;
1207 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001208 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001209 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001210
1211 // check z plane
1212 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1213 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1214 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1215 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1216 return false;
1217 }
1218
1219 SkRRect rrect;
1220 SkRect rect;
1221 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001222 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001223 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1224 if (!isRRect &&
1225 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1226 rect.width() > SK_ScalarNearlyZero) {
1227 rrect.setOval(rect);
1228 isRRect = true;
1229 }
1230 if (!isRRect && path.isRect(&rect)) {
1231 rrect.setRect(rect);
1232 isRRect = true;
1233 }
1234
1235 if (!isRRect) {
1236 return false;
1237 }
1238
Jim Van Verthc5903412016-11-17 15:27:09 -05001239 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001240 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001241 }
1242
Robert Phillips72152832017-01-25 17:31:35 -05001243 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001244
Jim Van Verth3af1af92017-05-18 15:06:54 -04001245 // transform light
1246 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1247
1248 // 1/scale
1249 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001250 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001251 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1252 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1253
1254 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1256
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001257 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001258 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1259 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1260 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001261
1262 // Outset the shadow rrect to the border of the penumbra
1263 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1264 SkRRect ambientRRect;
1265 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1266 // If the rrect was an oval then its outset will also be one.
1267 // We set it explicitly to avoid errors.
1268 if (rrect.isOval()) {
1269 ambientRRect = SkRRect::MakeOval(outsetRect);
1270 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001271 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001272 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1273 }
1274
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001275 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001276 if (transparent) {
1277 // set a large inset to force a fill
1278 devSpaceInsetWidth = ambientRRect.width();
1279 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001280
Robert Phillips7c525e62018-06-12 10:11:12 -04001281 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1282 ambientColor,
1283 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001284 ambientRRect,
1285 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001286 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001287 if (op) {
1288 this->addDrawOp(clip, std::move(op));
1289 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001290 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001291
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001292 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001293 SkScalar devSpaceSpotBlur;
1294 SkScalar spotScale;
1295 SkVector spotOffset;
1296 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1297 devLightPos.fZ, rec.fLightRadius,
1298 &devSpaceSpotBlur, &spotScale, &spotOffset);
1299 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001300 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1301
Jim Van Verth3af1af92017-05-18 15:06:54 -04001302 // Adjust translate for the effect of the scale.
1303 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1304 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1305 // This offset is in dev space, need to transform it into source space.
1306 SkMatrix ctmInverse;
1307 if (viewMatrix.invert(&ctmInverse)) {
1308 ctmInverse.mapPoints(&spotOffset, 1);
1309 } else {
1310 // Since the matrix is a similarity, this should never happen, but just in case...
1311 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1312 SkASSERT(false);
1313 }
1314
1315 // Compute the transformed shadow rrect
1316 SkRRect spotShadowRRect;
1317 SkMatrix shadowTransform;
1318 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1319 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001320 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001321
1322 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001323 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001324 SkScalar insetWidth = blurOutset;
1325 if (transparent) {
1326 // If transparent, just do a fill
1327 insetWidth += spotShadowRRect.width();
1328 } else {
1329 // For shadows, instead of using a stroke we specify an inset from the penumbra
1330 // border. We want to extend this inset area so that it meets up with the caster
1331 // geometry. The inset geometry will by default already be inset by the blur width.
1332 //
1333 // We compare the min and max corners inset by the radius between the original
1334 // rrect and the shadow rrect. The distance between the two plus the difference
1335 // between the scaled radius and the original radius gives the distance from the
1336 // transformed shadow shape to the original shape in that corner. The max
1337 // of these gives the maximum distance we need to cover.
1338 //
1339 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1340 // that to get the full insetWidth.
1341 SkScalar maxOffset;
1342 if (rrect.isRect()) {
1343 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001344 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001345 rrect.rect().fLeft),
1346 SkTAbs(spotShadowRRect.rect().fTop -
1347 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001348 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001349 rrect.rect().fRight),
1350 SkTAbs(spotShadowRRect.rect().fBottom -
1351 rrect.rect().fBottom)));
1352 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001353 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001354 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1355 rrect.rect().fLeft + dr,
1356 spotShadowRRect.rect().fTop -
1357 rrect.rect().fTop + dr);
1358 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1359 rrect.rect().fRight - dr,
1360 spotShadowRRect.rect().fBottom -
1361 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001362 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001363 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001364 }
Brian Osman788b9162020-02-07 10:36:46 -05001365 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366 }
1367
1368 // Outset the shadow rrect to the border of the penumbra
1369 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1370 if (spotShadowRRect.isOval()) {
1371 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1372 } else {
1373 SkScalar outsetRad = spotRadius + blurOutset;
1374 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1375 }
1376
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001377 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001378
Robert Phillips7c525e62018-06-12 10:11:12 -04001379 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1380 spotColor,
1381 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001382 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001383 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001384 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001385 if (op) {
1386 this->addDrawOp(clip, std::move(op));
1387 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001388 }
1389
1390 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001391}
1392
1393///////////////////////////////////////////////////////////////////////////////
1394
Brian Osman11052242016-10-27 14:47:55 -04001395bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001396 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001397 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001398 const SkMatrix& viewMatrix,
1399 const SkRRect& origOuter,
1400 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001401 SkASSERT(!origInner.isEmpty());
1402 SkASSERT(!origOuter.isEmpty());
1403
Brian Salomon65749212017-12-01 16:01:47 -05001404 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1405
Chris Dalton7d6748e2019-03-13 00:34:52 -06001406 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001407
1408 if (GrAAType::kMSAA == aaType) {
1409 return false;
1410 }
1411
Greg Daniel2655ede2019-04-10 00:49:28 +00001412 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1413 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001414 auto outerR = outer->width() / 2.f;
1415 auto innerR = inner->width() / 2.f;
1416 auto cx = outer->getBounds().fLeft + outerR;
1417 auto cy = outer->getBounds().fTop + outerR;
1418 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1419 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1420 auto avgR = (innerR + outerR) / 2.f;
1421 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1422 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1423 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001424 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001425 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001426 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001427 if (op) {
1428 this->addDrawOp(clip, std::move(op));
1429 return true;
1430 }
Mike Klein16885072018-12-11 09:54:31 -05001431 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001432 }
1433 }
1434
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001435 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001436 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001437 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1438 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001439 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001440 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1441 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001442 }
robertphillips00095892016-02-29 13:50:40 -08001443
robertphillips00095892016-02-29 13:50:40 -08001444 SkMatrix inverseVM;
1445 if (!viewMatrix.isIdentity()) {
1446 if (!origInner.transform(viewMatrix, inner.writable())) {
1447 return false;
1448 }
1449 if (!origOuter.transform(viewMatrix, outer.writable())) {
1450 return false;
1451 }
1452 if (!viewMatrix.invert(&inverseVM)) {
1453 return false;
1454 }
1455 } else {
1456 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001457 }
robertphillips00095892016-02-29 13:50:40 -08001458
Ethan Nicholaseace9352018-10-15 20:09:54 +00001459 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001460 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001461 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001462 if (!innerEffect) {
1463 return false;
1464 }
1465
Ethan Nicholaseace9352018-10-15 20:09:54 +00001466 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001467 if (!outerEffect) {
1468 return false;
1469 }
1470
Brian Salomon82f44312017-01-11 13:42:54 -05001471 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1472 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001473
1474 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001475 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001476 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1477 }
halcanary9d524f22016-03-29 09:03:52 -07001478
Brian Salomon82f44312017-01-11 13:42:54 -05001479 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1480 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001481 return true;
1482}
1483
Brian Osman11052242016-10-27 14:47:55 -04001484void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001485 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001486 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001487 const SkMatrix& viewMatrix,
1488 const SkRRect& outer,
1489 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001490 ASSERT_SINGLE_OWNER
1491 RETURN_IF_ABANDONED
1492 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001493 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001494
1495 SkASSERT(!outer.isEmpty());
1496 SkASSERT(!inner.isEmpty());
1497
Robert Phillips72152832017-01-25 17:31:35 -05001498 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001499
Brian Salomon82f44312017-01-11 13:42:54 -05001500 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001501 return;
1502 }
Mike Klein16885072018-12-11 09:54:31 -05001503 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001504
1505 SkPath path;
1506 path.setIsVolatile(true);
1507 path.addRRect(inner);
1508 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001509 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001510 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001511}
1512
robertphillipsea461502015-05-26 11:38:03 -07001513///////////////////////////////////////////////////////////////////////////////
1514
Brian Osman11052242016-10-27 14:47:55 -04001515void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001516 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001517 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001518 const SkMatrix& viewMatrix,
1519 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001520 const GrStyle& style,
1521 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001522 ASSERT_SINGLE_OWNER
1523 RETURN_IF_ABANDONED
1524 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001525 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001526
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001528 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001529 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001530 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001531 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1532 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001533 aa = GrAA::kNo;
1534 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001535 }
msarettcc319b92016-08-25 18:07:18 -07001536 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001537 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001538 SkPath path;
1539 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001540 path.setIsVolatile(true);
1541
Brian Salomon82f44312017-01-11 13:42:54 -05001542 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001543 }
1544
Chris Dalton7d6748e2019-03-13 00:34:52 -06001545 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001546 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1547 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001548 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001549}
1550
Brian Osman11052242016-10-27 14:47:55 -04001551void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001552 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001553 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001554 const SkMatrix& viewMatrix,
1555 const SkRect& oval,
1556 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001557 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001558 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001559 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001560 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001561
Robert Phillips7484d202018-07-03 09:09:08 -04001562 const SkStrokeRec& stroke = style.strokeRec();
1563
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001564 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001565 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1566 return;
1567 }
1568
1569 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001570 return;
robertphillipsea461502015-05-26 11:38:03 -07001571 }
1572
Robert Phillips72152832017-01-25 17:31:35 -05001573 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001574
Chris Dalton7d6748e2019-03-13 00:34:52 -06001575 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001576
1577 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001578 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1579 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001580 // We don't draw true circles as round rects in coverage mode, because it can
1581 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1582 assert_alive(paint);
1583 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1584 this->caps()->shaderCaps());
1585 }
1586 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001587 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1588 // the arc equation. This same special geometry and fragment branch also turn out to be a
1589 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1590 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1591 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001592 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001593 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1594 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001595 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001596 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001597 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001598 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1599 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001600 }
1601 if (op) {
1602 this->addDrawOp(clip, std::move(op));
1603 return;
robertphillipsea461502015-05-26 11:38:03 -07001604 }
robertphillipsb56f9272016-02-25 11:03:52 -08001605
Mike Klein16885072018-12-11 09:54:31 -05001606 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001607 this->drawShapeUsingPathRenderer(
1608 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001609 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001610}
1611
Brian Osman11052242016-10-27 14:47:55 -04001612void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001613 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001614 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001615 const SkMatrix& viewMatrix,
1616 const SkRect& oval,
1617 SkScalar startAngle,
1618 SkScalar sweepAngle,
1619 bool useCenter,
1620 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001621 ASSERT_SINGLE_OWNER
1622 RETURN_IF_ABANDONED
1623 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001624 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001625
1626 AutoCheckFlush acf(this->drawingManager());
1627
Chris Dalton7d6748e2019-03-13 00:34:52 -06001628 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001629 if (GrAAType::kCoverage == aaType) {
1630 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1631 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1632 std::move(paint),
1633 viewMatrix,
1634 oval,
1635 startAngle,
1636 sweepAngle,
1637 useCenter,
1638 style,
1639 shaderCaps);
1640 if (op) {
1641 this->addDrawOp(clip, std::move(op));
1642 return;
1643 }
1644 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001645 }
Brian Salomone4949402018-04-26 15:22:04 -04001646 this->drawShapeUsingPathRenderer(
1647 clip, std::move(paint), aa, viewMatrix,
1648 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001649}
1650
Brian Osman11052242016-10-27 14:47:55 -04001651void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001652 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001653 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001654 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001655 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001656 sk_sp<GrColorSpaceXform> csxf,
1657 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001658 std::unique_ptr<SkLatticeIter> iter,
1659 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001660 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001661 RETURN_IF_ABANDONED
1662 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001663 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001664
Robert Phillips72152832017-01-25 17:31:35 -05001665 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001666
Brian Salomon2a943df2018-05-04 13:43:19 -04001667 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001668 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001669 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001670 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001671}
1672
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001673void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1674 const SkRect& bounds) {
1675 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1676 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001677 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001678}
1679
Brian Salomon031b0ba2019-05-23 11:05:26 -04001680void GrRenderTargetContext::asyncRescaleAndReadPixels(
1681 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1682 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1683 auto direct = fContext->priv().asDirectContext();
1684 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001685 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001686 return;
1687 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001688 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001689 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001690 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001691 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001692 if (this->asRenderTargetProxy()->framebufferOnly()) {
1693 callback(context, nullptr);
1694 return;
1695 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001696 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001697 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001698 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001699 return;
1700 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001701 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001702 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001703 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001704 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001705 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001706 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1707 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001709 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001710 backendFormatOfFinalContext, dstCT);
1711 // Fail if we can't read from the source surface's color type.
1712 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001713 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001714 return;
1715 }
Brian Salomon624f9062019-07-02 14:23:00 -04001716 // Fail if read color type does not have all of dstCT's color channels and those missing color
1717 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001718 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1719 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1720 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1721 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001722 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001723 return;
1724 }
1725
Brian Salomonbf6b9792019-08-21 09:38:10 -04001726 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001727 int x = srcRect.fLeft;
1728 int y = srcRect.fTop;
1729 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001730 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1731 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001732 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001733 return;
1734 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001735 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001736 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001737 x = y = 0;
1738 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001739 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1740 this->colorInfo().alphaType(),
1741 info.colorSpace(),
1742 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001743 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1744 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001745 // We flip or color convert by drawing and we don't currently support drawing to
1746 // kPremul.
1747 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001748 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001749 return;
1750 }
Greg Daniel40903af2020-01-30 14:55:05 -05001751 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001752 SkRect srcRectToDraw = SkRect::Make(srcRect);
1753 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001754 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001755 texProxyView =
1756 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1757 SkBackingFit::kApprox, SkBudgeted::kNo);
1758 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001759 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001760 return;
1761 }
Brian Salomonc5243782020-04-02 12:50:34 -04001762 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001763 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1764 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001765 tempRTC = GrRenderTargetContext::Make(
1766 direct, this->colorInfo().colorType(), info.refColorSpace(),
1767 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001768 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001769 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001770 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001771 return;
1772 }
Greg Daniel40903af2020-01-30 14:55:05 -05001773 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1774 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1775 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001776 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1777 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1778 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001779 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001780 }
1781 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001782 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001783 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1784 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001785}
1786
Brian Salomon9241a6d2019-10-03 13:26:54 -04001787class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1788public:
1789 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1790 ~AsyncReadResult() override {
1791 for (int i = 0; i < fPlanes.count(); ++i) {
1792 if (!fPlanes[i].fMappedBuffer) {
1793 delete[] static_cast<const char*>(fPlanes[i].fData);
1794 } else {
1795 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1796 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1797 }
1798 }
1799 }
1800
1801 int count() const override { return fPlanes.count(); }
1802 const void* data(int i) const override { return fPlanes[i].fData; }
1803 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1804
1805 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001806 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001807 size_t rowBytes,
1808 GrClientMappedBufferManager* manager) {
1809 SkASSERT(!result.fTransferBuffer->isMapped());
1810 const void* mappedData = result.fTransferBuffer->map();
1811 if (!mappedData) {
1812 return false;
1813 }
1814 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001815 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001816 result.fPixelConverter(convertedData.get(), mappedData);
1817 this->addCpuPlane(std::move(convertedData), rowBytes);
1818 result.fTransferBuffer->unmap();
1819 } else {
1820 manager->insert(result.fTransferBuffer);
1821 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1822 }
1823 return true;
1824 }
1825
1826 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1827 SkASSERT(data);
1828 SkASSERT(rowBytes > 0);
1829 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1830 }
1831
1832private:
1833 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1834 SkASSERT(data);
1835 SkASSERT(rowBytes > 0);
1836 SkASSERT(mappedBuffer);
1837 SkASSERT(mappedBuffer->isMapped());
1838 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1839 }
1840
1841 struct Plane {
1842 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1843 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1844 const void* fData;
1845 size_t fRowBytes;
1846 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1847 sk_sp<GrGpuBuffer> fMappedBuffer;
1848 };
1849 SkSTArray<3, Plane> fPlanes;
1850 uint32_t fInboxID;
1851};
1852
Brian Salomon024bd002019-06-11 11:38:16 -04001853void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1854 ReadPixelsCallback callback,
1855 ReadPixelsContext context) {
1856 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1857 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1858
Greg Danielca9dbe22020-01-02 13:44:30 -05001859 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001860 callback(context, nullptr);
1861 return;
1862 }
1863
Brian Salomon9241a6d2019-10-03 13:26:54 -04001864 auto directContext = fContext->priv().asDirectContext();
1865 SkASSERT(directContext);
1866 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1867
Brian Salomon024bd002019-06-11 11:38:16 -04001868 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1869
1870 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001871 auto ii = SkImageInfo::Make(rect.size(), colorType,
1872 this->colorInfo().alphaType(),
1873 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001874 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001875 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1876 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1877 result->addCpuPlane(std::move(data), pm.rowBytes());
1878
Brian Salomon1d435302019-07-01 13:05:28 -04001879 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001880 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001881 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001882 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001883 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001884 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001885 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001886
Brian Salomonab32f652019-05-10 14:24:50 -04001887 struct FinishContext {
1888 ReadPixelsCallback* fClientCallback;
1889 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001890 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001891 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001892 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001893 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001894 };
1895 // Assumption is that the caller would like to flush. We could take a parameter or require an
1896 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1897 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001898 auto* finishContext = new FinishContext{callback,
1899 context,
1900 rect.size(),
1901 colorType,
1902 mappedBufferManager,
1903 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001904 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001905 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001906 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001907 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1908 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1909 context->fMappedBufferManager)) {
1910 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001911 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001912 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001913 delete context;
1914 };
1915 GrFlushInfo flushInfo;
1916 flushInfo.fFinishedContext = finishContext;
1917 flushInfo.fFinishedProc = finishCallback;
1918 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1919}
1920
Brian Salomon9241a6d2019-10-03 13:26:54 -04001921void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1922 sk_sp<SkColorSpace> dstColorSpace,
1923 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001924 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001925 RescaleGamma rescaleGamma,
1926 SkFilterQuality rescaleQuality,
1927 ReadPixelsCallback callback,
1928 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001929 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1930 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001931 SkASSERT(!dstSize.isZero());
1932 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1933
Brian Salomon024bd002019-06-11 11:38:16 -04001934 auto direct = fContext->priv().asDirectContext();
1935 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001937 return;
1938 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001939 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001940 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001941 return;
1942 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001943 if (this->asRenderTargetProxy()->framebufferOnly()) {
1944 callback(context, nullptr);
1945 return;
1946 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001947 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001948 callback(context, nullptr);
1949 return;
1950 }
Brian Salomon024bd002019-06-11 11:38:16 -04001951 int x = srcRect.fLeft;
1952 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001953 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001954 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001955 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001956 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001957 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001958 dstColorSpace);
1959 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001960 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001961 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001962 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001963 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001964 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001965 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001966 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001967 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001968 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001969 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001970 srcView = this->readSurfaceView();
1971 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001972 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1973 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1974 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001975 // If we can't get a texture copy of the contents then give up.
1976 callback(context, nullptr);
1977 return;
1978 }
Brian Salomonc5243782020-04-02 12:50:34 -04001979 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001980 x = y = 0;
1981 }
Brian Salomon4d036892019-07-02 15:10:58 -04001982 // We assume the caller wants kPremul. There is no way to indicate a preference.
1983 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001984 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1985 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001986 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001987 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1988 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001989 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1990 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001991 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001992 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001993 return;
1994 }
Greg Daniel40903af2020-01-30 14:55:05 -05001995 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1996 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1997 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1998 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1999 SkMatrix::I(), std::move(xform));
2000 srcView = tempRTC->readSurfaceView();
2001 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002002 x = y = 0;
2003 }
2004 }
Greg Danielc594e622019-10-15 14:01:49 -04002005
Greg Daniele20fcad2020-01-08 11:52:34 -05002006 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2007 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2008 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002009 int halfW = dstSize.width()/2;
2010 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002011 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2012 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2013 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2014 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2015 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2016 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002017 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002018 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002019 return;
2020 }
2021
Brian Osman28ba5282019-10-30 14:18:07 -04002022 float baseM[20];
2023 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2024
Brian Salomon024bd002019-06-11 11:38:16 -04002025 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2026
2027 auto texMatrix = SkMatrix::MakeTrans(x, y);
2028
Brian Salomon9241a6d2019-10-03 13:26:54 -04002029 SkRect dstRectY = SkRect::Make(dstSize);
2030 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002031
Brian Salomon6aa65052020-01-28 12:16:53 -05002032 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2033 PixelTransferResult yTransfer, uTransfer, vTransfer;
2034
Brian Salomon024bd002019-06-11 11:38:16 -04002035 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2036 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002037 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002038 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002039 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002040 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002041 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002042 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2043 yPaint.addColorFragmentProcessor(std::move(yFP));
2044 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002045 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2046 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002047 if (!doSynchronousRead) {
2048 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2049 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2050 if (!yTransfer.fTransferBuffer) {
2051 callback(context, nullptr);
2052 return;
2053 }
Brian Salomon024bd002019-06-11 11:38:16 -04002054 }
2055
2056 texMatrix.preScale(2.f, 2.f);
2057 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2058 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002059 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002060 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002061 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002062 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002063 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002064 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2065 uPaint.addColorFragmentProcessor(std::move(uFP));
2066 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002067 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2068 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002069 if (!doSynchronousRead) {
2070 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2071 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2072 if (!uTransfer.fTransferBuffer) {
2073 callback(context, nullptr);
2074 return;
2075 }
Brian Salomon024bd002019-06-11 11:38:16 -04002076 }
2077
2078 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2079 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002080 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002081 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002082 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002083 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2084 this->colorInfo().alphaType(), texMatrix,
2085 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002086 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2087 vPaint.addColorFragmentProcessor(std::move(vFP));
2088 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002089 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2090 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002091 if (!doSynchronousRead) {
2092 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2093 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2094 if (!vTransfer.fTransferBuffer) {
2095 callback(context, nullptr);
2096 return;
2097 }
2098 }
2099
2100 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002101 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2102 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2103 size_t yRB = yInfo.minRowBytes();
2104 size_t uvRB = uvInfo.minRowBytes();
2105 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2106 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2107 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2108 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2109 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2110 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002111 callback(context, nullptr);
2112 return;
2113 }
2114 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002115 result->addCpuPlane(std::move(y), yRB );
2116 result->addCpuPlane(std::move(u), uvRB);
2117 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002118 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002119 return;
2120 }
2121
2122 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002123 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002124 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002125 GrClientMappedBufferManager* fMappedBufferManager;
2126 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002127 PixelTransferResult fYTransfer;
2128 PixelTransferResult fUTransfer;
2129 PixelTransferResult fVTransfer;
2130 };
2131 // Assumption is that the caller would like to flush. We could take a parameter or require an
2132 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2133 // callback to GrGpu until after the next flush that flushes our op list, though.
2134 auto* finishContext = new FinishContext{callback,
2135 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002136 direct->priv().clientMappedBufferManager(),
2137 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002138 std::move(yTransfer),
2139 std::move(uTransfer),
2140 std::move(vTransfer)};
2141 auto finishCallback = [](GrGpuFinishedContext c) {
2142 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002143 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002144 auto manager = context->fMappedBufferManager;
2145 size_t rowBytes = SkToSizeT(context->fSize.width());
2146 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2147 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002148 delete context;
2149 return;
2150 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002151 rowBytes /= 2;
2152 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2153 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2154 (*context->fClientCallback)(context->fClientContext, nullptr);
2155 delete context;
2156 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002157 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002158 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2159 (*context->fClientCallback)(context->fClientContext, nullptr);
2160 delete context;
2161 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002162 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002163 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002164 delete context;
2165 };
2166 GrFlushInfo flushInfo;
2167 flushInfo.fFinishedContext = finishContext;
2168 flushInfo.fFinishedProc = finishCallback;
2169 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002170}
2171
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002172GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2173 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002174 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002175 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002176 return GrSemaphoresSubmitted::kNo;
2177 }
robertphillips8c523e02016-07-26 07:41:00 -07002178 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002179 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002180
Greg Daniel55f040b2020-02-13 15:38:32 +00002181 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002182}
2183
Greg Danielc64ee462017-06-15 16:59:49 -04002184bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002185 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002186 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002187 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002188 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002189 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002190
2191 AutoCheckFlush acf(this->drawingManager());
2192
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002193 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002194 return false;
2195 }
2196
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002197 auto direct = fContext->priv().asDirectContext();
2198 if (!direct) {
2199 return false;
2200 }
2201
2202 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002203
Greg Daniel301015c2019-11-18 14:06:46 -05002204 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2205 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002206 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002207 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002208 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2209 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002210 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002211 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2212 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002213 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002214}
joshualitt33a5fce2015-11-18 13:28:51 -08002215
Brian Osman11052242016-10-27 14:47:55 -04002216void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002217 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002218 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002219 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002220 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002221 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002222 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002223 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002224 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002225 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2226
Brian Salomon40b77a62017-12-22 11:25:52 -05002227 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002228
Robert Phillips27927a52018-08-20 13:18:12 -04002229 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002230}
2231
2232void GrRenderTargetContext::drawShape(const GrClip& clip,
2233 GrPaint&& paint,
2234 GrAA aa,
2235 const SkMatrix& viewMatrix,
2236 const GrShape& shape) {
2237 ASSERT_SINGLE_OWNER
2238 RETURN_IF_ABANDONED
2239 SkDEBUGCODE(this->validate();)
2240 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2241
Brian Salomon2fad74a2017-12-20 13:28:55 -05002242 if (shape.isEmpty()) {
2243 if (shape.inverseFilled()) {
2244 this->drawPaint(clip, std::move(paint), viewMatrix);
2245 }
2246 return;
robertphillipsea461502015-05-26 11:38:03 -07002247 }
2248
Robert Phillips72152832017-01-25 17:31:35 -05002249 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002250
Brian Salomon2fad74a2017-12-20 13:28:55 -05002251 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002252 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002253 SkRRect rrect;
2254 // We can ignore the starting point and direction since there is no path effect.
2255 bool inverted;
2256 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2257 if (rrect.isRect()) {
2258 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2259 &shape.style());
2260 return;
2261 } else if (rrect.isOval()) {
2262 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002263 return;
2264 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002265 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2266 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002267 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2268 viewMatrix.rectStaysRect()) {
2269 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2270 // the matrix to all the points individually rather than just to the rect
2271 SkRect rects[2];
2272 if (shape.asNestedRects(rects)) {
2273 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002274 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002275 fContext, std::move(paint), viewMatrix, rects);
2276 if (op) {
2277 this->addDrawOp(clip, std::move(op));
2278 }
2279 // Returning here indicates that there is nothing to draw in this case.
2280 return;
2281 }
robertphillipsea461502015-05-26 11:38:03 -07002282 }
2283 }
robertphillips4bc31812016-03-01 12:22:49 -08002284
Brian Salomon2fad74a2017-12-20 13:28:55 -05002285 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002286}
2287
Chris Daltonbbfd5162017-11-07 13:35:22 -07002288bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002289 const GrUserStencilSettings* ss,
2290 SkRegion::Op op,
2291 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002292 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002293 const SkMatrix& viewMatrix,
2294 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002295 ASSERT_SINGLE_OWNER_PRIV
2296 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002297 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002298 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2299 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002300
2301 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002302 GrPaint paint;
2303 paint.setCoverageSetOpXPFactory(op, invert);
2304 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2305 SkRect::MakeIWH(fRenderTargetContext->width(),
2306 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002307 return true;
2308 }
2309
Robert Phillips72152832017-01-25 17:31:35 -05002310 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002311
2312 // An Assumption here is that path renderer would use some form of tweaking
2313 // the src color (either the input alpha or in the frag shader) to implement
2314 // aa. If we have some future driver-mojo path AA that can do the right
2315 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002316 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002317 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002318
Chris Daltondb91c6e2017-09-08 16:25:08 -06002319 SkIRect clipConservativeBounds;
2320 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2321 &clipConservativeBounds, nullptr);
2322
bsalomon8acedde2016-06-24 10:42:16 -07002323 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002324 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002325 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002326 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002327 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002328 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002329 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002330 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002331 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2332 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002333 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002334
2335 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002336 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002337 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002338 if (!pr) {
2339 return false;
2340 }
2341
2342 GrPaint paint;
2343 paint.setCoverageSetOpXPFactory(op, invert);
2344
Brian Salomonf3569f02017-10-24 12:52:33 -04002345 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2346 std::move(paint),
2347 ss,
2348 fRenderTargetContext,
2349 &clip,
2350 &clipConservativeBounds,
2351 &viewMatrix,
2352 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002353 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002354 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002355 pr->drawPath(args);
2356 return true;
2357}
2358
Brian Osman11052242016-10-27 14:47:55 -04002359SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002360 ASSERT_SINGLE_OWNER_PRIV
2361
Robert Phillips6a6de562019-02-15 15:19:15 -05002362 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002363 return SkBudgeted::kNo;
2364 }
2365
Brian Osman11052242016-10-27 14:47:55 -04002366 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002367
Greg Daniel3912a4b2020-01-14 09:56:04 -05002368 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002369}
2370
Brian Salomon2fad74a2017-12-20 13:28:55 -05002371void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2372 GrPaint&& paint,
2373 GrAA aa,
2374 const SkMatrix& viewMatrix,
2375 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002376 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002377 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002378 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2379
Jim Van Verthf86073a2018-10-02 13:05:38 -04002380 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2381 return;
2382 }
2383
Chris Daltondb91c6e2017-09-08 16:25:08 -06002384 SkIRect clipConservativeBounds;
2385 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2386
Brian Salomon2fad74a2017-12-20 13:28:55 -05002387 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002388 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002389
robertphillips68737822015-10-29 12:12:21 -07002390 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002391 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002392 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002393 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002394 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002395 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002396 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002397 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002398
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002399 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002400 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002401 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002402 return;
2403 }
2404
Chris Dalton6ce447a2019-06-23 18:07:38 -06002405 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002406
2407 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2408 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2409 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2410
Brian Salomon2fad74a2017-12-20 13:28:55 -05002411 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002412 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002413 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2414 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002415 return;
2416 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002417 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002418 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002419 }
2420 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002421 if (canDrawArgs.fShape->style().applies()) {
2422 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2423 styleScale);
2424 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002425 return;
2426 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002427 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002428 // This time, allow SW renderer
2429 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2430 } else {
2431 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002432 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002433 }
robertphillipsea461502015-05-26 11:38:03 -07002434
bsalomon8acedde2016-06-24 10:42:16 -07002435 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002436#ifdef SK_DEBUG
2437 SkDebugf("Unable to find path renderer compatible with path.\n");
2438#endif
2439 return;
2440 }
2441
Robert Phillips256c37b2017-03-01 14:32:46 -05002442 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002443 std::move(paint),
2444 &GrUserStencilSettings::kUnused,
2445 this,
2446 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002447 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002448 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002449 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002450 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002451 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002452 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002453}
2454
Brian Salomon467921e2017-03-06 16:17:12 -05002455static void op_bounds(SkRect* bounds, const GrOp* op) {
2456 *bounds = op->bounds();
2457 if (op->hasZeroArea()) {
2458 if (op->hasAABloat()) {
2459 bounds->outset(0.5f, 0.5f);
2460 } else {
2461 // We don't know which way the particular GPU will snap lines or points at integer
2462 // coords. So we ensure that the bounds is large enough for either snap.
2463 SkRect before = *bounds;
2464 bounds->roundOut(bounds);
2465 if (bounds->fLeft == before.fLeft) {
2466 bounds->fLeft -= 1;
2467 }
2468 if (bounds->fTop == before.fTop) {
2469 bounds->fTop -= 1;
2470 }
2471 if (bounds->fRight == before.fRight) {
2472 bounds->fRight += 1;
2473 }
2474 if (bounds->fBottom == before.fBottom) {
2475 bounds->fBottom += 1;
2476 }
2477 }
2478 }
2479}
2480
Chris Dalton08755122019-08-05 16:13:47 -06002481void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002482 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002483 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2484}
2485
Brian Salomon348a0372018-10-31 10:42:18 -04002486void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2487 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002488 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002489 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002490 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002491 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002492 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002493 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002494 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002495 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002496
Brian Salomon467921e2017-03-06 16:17:12 -05002497 // Setup clip
2498 SkRect bounds;
2499 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002500 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002501 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002502 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002503 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002504
Chris Dalton9acf6822019-11-12 11:52:40 -07002505 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002506 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002507 }
2508
Chris Dalton9acf6822019-11-12 11:52:40 -07002509 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002510 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002511 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002512 }
2513
Chris Dalton9acf6822019-11-12 11:52:40 -07002514 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2515 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2516
2517 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2518 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2519 // that uses a stencil test when the stencil buffer is multisampled.)
2520 bool hasMixedSampledCoverage = (
2521 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002522 SkASSERT(!hasMixedSampledCoverage ||
2523 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002524
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002525 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002526 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002527 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2528
Greg Daniel524e28b2019-11-01 11:48:53 -04002529 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002530 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002531 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002532 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002533 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002534 }
2535 }
2536
2537 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002538 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002539 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002540 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002541 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002542 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002543 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002544}
2545
Greg Daniel524e28b2019-11-01 11:48:53 -04002546bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2547 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002548 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2549 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2550 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002551 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002552 return false;
2553 }
2554
Greg Daniel3912a4b2020-01-14 09:56:04 -05002555 if (this->caps()->textureBarrierSupport() &&
2556 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002557 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002558 // The render target is a texture, so we can read from it directly in the shader. The XP
2559 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002560 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002561 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002562 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002563 }
2564 }
2565
Greg Daniel3912a4b2020-01-14 09:56:04 -05002566 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002567
Eric Karl74480882017-04-03 14:49:05 -07002568 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002569 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002570 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002571 SkRect opBounds = op.bounds();
2572 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2573 // 0.5 pixels.
2574 if (op.hasAABloat() || op.hasZeroArea()) {
2575 opBounds.outset(0.5f, 0.5f);
2576 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2577 // performance we may ignore the clip when the draw is entirely inside the clip is float
2578 // space but will hit pixels just outside the clip when actually rasterizing.
2579 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002580 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002581 }
2582 SkIRect opIBounds;
2583 opBounds.roundOut(&opIBounds);
2584 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002585#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002586 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002587#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002588 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002589 }
2590
Greg Daniel3155f7f2020-01-16 16:54:26 -05002591 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002592 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2593 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002594 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002595 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002596
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002597 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002598 copyRect = clippedRect;
2599 }
Brian Salomon467921e2017-03-06 16:17:12 -05002600
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002601 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002602 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002603 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002604 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002605 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002606 } else {
Eric Karl74480882017-04-03 14:49:05 -07002607 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002608 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002609 }
Brian Salomonc5243782020-04-02 12:50:34 -04002610 auto copy =
2611 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2612 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2613 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002614
Brian Salomonc5243782020-04-02 12:50:34 -04002615 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002616 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002617 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002618}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002619
Greg Daniel573312e2020-02-07 17:22:35 -05002620bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002621 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002622 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002623 SkIRect clippedSrcRect;
2624 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002625 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2626 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002627 return false;
2628 }
2629
2630 GrPaint paint;
2631 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002632
Greg Danield2ccbb52020-02-05 10:45:39 -05002633 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002634 if (!fp) {
2635 return false;
2636 }
2637 paint.addColorFragmentProcessor(std::move(fp));
2638
2639 this->fillRectToRect(
2640 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2641 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2642 clippedSrcRect.height()),
2643 SkRect::Make(clippedSrcRect));
2644 return true;
2645}