blob: a3b93676a8595bde3a3d10d8c66f6db5af5c7971 [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();
155 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400156 GrSwizzle writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
Greg Danielba0ff782020-01-07 15:42:57 -0500157
Greg Daniel3912a4b2020-01-14 09:56:04 -0500158 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400159 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500160
161 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400162 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500163 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500164}
165
166std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
167 GrRecordingContext* context,
168 GrColorType colorType,
169 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500170 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500171 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500172 const GrBackendFormat& format,
173 int sampleCnt,
174 GrMipMapped mipMapped,
175 GrProtected isProtected,
176 GrSurfaceOrigin origin,
177 SkBudgeted budgeted,
178 const SkSurfaceProps* surfaceProps) {
179 // It is probably not necessary to check if the context is abandoned here since uses of the
180 // GrRenderTargetContext which need the context will mostly likely fail later on without an
181 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
182 // an abandoned context correctly. It also lets us early out of some extra work.
183 if (context->priv().abandoned()) {
184 return nullptr;
185 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500186
Greg Daniele20fcad2020-01-08 11:52:34 -0500187 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400188 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500189 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500190 if (!proxy) {
191 return nullptr;
192 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500193
Greg Danielba0ff782020-01-07 15:42:57 -0500194 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
195 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500196 if (!rtc) {
197 return nullptr;
198 }
199 rtc->discard();
200 return rtc;
201}
202
203std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
204 GrRecordingContext* context,
205 GrColorType colorType,
206 sk_sp<SkColorSpace> colorSpace,
207 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500208 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500209 int sampleCnt,
210 GrMipMapped mipMapped,
211 GrProtected isProtected,
212 GrSurfaceOrigin origin,
213 SkBudgeted budgeted,
214 const SkSurfaceProps* surfaceProps) {
215 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
216 if (!format.isValid()) {
217 return nullptr;
218 }
219
220 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
221 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
222 surfaceProps);
223}
224
225static inline GrColorType color_type_fallback(GrColorType ct) {
226 switch (ct) {
227 // kRGBA_8888 is our default fallback for many color types that may not have renderable
228 // backend formats.
229 case GrColorType::kAlpha_8:
230 case GrColorType::kBGR_565:
231 case GrColorType::kABGR_4444:
232 case GrColorType::kBGRA_8888:
233 case GrColorType::kRGBA_1010102:
234 case GrColorType::kRGBA_F16:
235 case GrColorType::kRGBA_F16_Clamped:
236 return GrColorType::kRGBA_8888;
237 case GrColorType::kAlpha_F16:
238 return GrColorType::kRGBA_F16;
239 case GrColorType::kGray_8:
240 return GrColorType::kRGB_888x;
241 default:
242 return GrColorType::kUnknown;
243 }
244}
245
Brian Salomond005b692020-04-01 15:47:05 -0400246std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400247 GrImageContext* context, GrColorType colorType, int sampleCnt) {
248 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400249 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400250 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
251 // We continue to the fallback color type if there no default renderable format or we
252 // requested msaa and the format doesn't support msaa.
253 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400254 return {colorType, format};
255 }
256 colorType = color_type_fallback(colorType);
257 } while (colorType != GrColorType::kUnknown);
258 return {GrColorType::kUnknown, {}};
259}
260
Greg Daniele20fcad2020-01-08 11:52:34 -0500261std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
262 GrRecordingContext* context,
263 GrColorType colorType,
264 sk_sp<SkColorSpace> colorSpace,
265 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500266 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500267 int sampleCnt,
268 GrMipMapped mipMapped,
269 GrProtected isProtected,
270 GrSurfaceOrigin origin,
271 SkBudgeted budgeted,
272 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400273 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400274 if (ct == GrColorType::kUnknown) {
275 return nullptr;
276 }
277 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
278 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500279}
280
Greg Danielba0ff782020-01-07 15:42:57 -0500281std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
282 GrRecordingContext* context,
283 GrColorType colorType,
284 sk_sp<SkColorSpace> colorSpace,
285 const GrBackendTexture& tex,
286 int sampleCnt,
287 GrSurfaceOrigin origin,
288 const SkSurfaceProps* surfaceProps,
289 ReleaseProc releaseProc,
290 ReleaseContext releaseCtx) {
291 SkASSERT(sampleCnt > 0);
292 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400293 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500294 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500295 if (!proxy) {
296 return nullptr;
297 }
298
299 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
300 origin, surfaceProps);
301}
302
303std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
304 GrRecordingContext* context,
305 GrColorType colorType,
306 sk_sp<SkColorSpace> colorSpace,
307 const GrBackendTexture& tex,
308 int sampleCnt,
309 GrSurfaceOrigin origin,
310 const SkSurfaceProps* surfaceProps) {
311 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400312 sk_sp<GrSurfaceProxy> proxy(
313 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500314 if (!proxy) {
315 return nullptr;
316 }
317
318 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
319 origin, surfaceProps);
320}
321
322std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
323 GrRecordingContext* context,
324 GrColorType colorType,
325 sk_sp<SkColorSpace> colorSpace,
326 const GrBackendRenderTarget& rt,
327 GrSurfaceOrigin origin,
328 const SkSurfaceProps* surfaceProps,
329 ReleaseProc releaseProc,
330 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400331 sk_sp<GrSurfaceProxy> proxy(
332 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500333 if (!proxy) {
334 return nullptr;
335 }
336
337 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
338 origin, surfaceProps);
339}
340
341std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
342 GrRecordingContext* context,
343 const SkImageInfo& imageInfo,
344 const GrVkDrawableInfo& vkInfo,
345 const SkSurfaceProps* props) {
346 sk_sp<GrSurfaceProxy> proxy(
347 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
348 vkInfo));
349 if (!proxy) {
350 return nullptr;
351 }
352
Greg Danielba0ff782020-01-07 15:42:57 -0500353 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
354 imageInfo.refColorSpace(), std::move(proxy),
355 kTopLeft_GrSurfaceOrigin, props);
356}
357
Greg Danielf41b2bd2019-08-22 16:19:24 -0400358// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
359// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
360// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
361// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500362GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500363 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400364 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400365 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400366 sk_sp<SkColorSpace> colorSpace,
367 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
370 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400371 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500372 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400373 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400374 , fManagedOpsTask(managedOpsTask) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400375 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
376 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500377
Brian Salomonf18b1d82017-10-27 11:30:49 -0400378 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700379 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700380}
381
robertphillips2e1e51f2015-10-15 08:01:48 -0700382#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500383void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400384 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400385 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700386 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700387}
388#endif
389
Brian Osman11052242016-10-27 14:47:55 -0400390GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800391 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700392}
393
Chris Dalton7d6748e2019-03-13 00:34:52 -0600394inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600395 if (GrAA::kNo == aa) {
396 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
397 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600398 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600399 return GrAAType::kMSAA;
400 }
401 return GrAAType::kNone;
402 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600403 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600404}
405
Greg Daniele252f082017-10-23 16:05:23 -0400406GrMipMapped GrRenderTargetContext::mipMapped() const {
407 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
408 return proxy->mipMapped();
409 }
410 return GrMipMapped::kNo;
411}
412
Greg Danielf41b2bd2019-08-22 16:19:24 -0400413GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800414 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700415 SkDEBUGCODE(this->validate();)
416
Greg Danielf41b2bd2019-08-22 16:19:24 -0400417 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600418 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400419 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600420 if (fOpsTask && fNumStencilSamples > 0) {
421 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600422 fOpsTask->setMustPreserveStencil();
423 // Reload the stencil buffer content at the beginning of newOpsTask.
424 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
425 // values?
426 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
427 }
428 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700429 }
430
Greg Danielf41b2bd2019-08-22 16:19:24 -0400431 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700432}
433
Herb Derbycddab252018-07-16 11:19:04 -0400434void GrRenderTargetContext::drawGlyphRunList(
435 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400436 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800437 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700438 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700439 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400440 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700441
Greg Danielbe7fc462019-01-03 16:40:42 -0500442 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
443 // secondary command buffers because it would require stopping and starting a render pass which
444 // we don't have access to.
445 if (this->wrapsVkSecondaryCB()) {
446 return;
447 }
448
Herb Derby26cbe512018-05-24 14:39:01 -0400449 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400450 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400451 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700452}
453
Brian Osman11052242016-10-27 14:47:55 -0400454void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800455 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700456 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700457 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400458 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700459
Robert Phillips72152832017-01-25 17:31:35 -0500460 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400461
Greg Danielf41b2bd2019-08-22 16:19:24 -0400462 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700463}
464
Brian Osman11052242016-10-27 14:47:55 -0400465void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500466 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700467 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800468 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700469 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700470 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400471 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700472
Robert Phillips72152832017-01-25 17:31:35 -0500473 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700474 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
475 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700476}
robertphillips9199a9f2016-07-13 07:48:43 -0700477
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500478void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
479 const SkPMColor4f& color,
480 CanClearFullscreen canClearFullscreen) {
481 ASSERT_SINGLE_OWNER_PRIV
482 RETURN_IF_ABANDONED_PRIV
483 SkDEBUGCODE(fRenderTargetContext->validate();)
484 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
485 fRenderTargetContext->fContext);
486
487 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
488 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
489}
490
491static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
492 paint->setColor4f(color);
493 if (color.isOpaque()) {
494 // Can just rely on the src-over blend mode to do the right thing
495 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
496 } else {
497 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
498 // were src blended
499 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
500 }
501}
502
503void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
504 const SkPMColor4f& color,
505 CanClearFullscreen canClearFullscreen) {
506 bool isFull = false;
507 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400508 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
509 // only clear the entire target if we knew it had not been cleared before. As
510 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500511 isFull = !clip.scissorEnabled() ||
512 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400513 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500514 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
515 }
516
517 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400518 GrOpsTask* opsTask = this->getOpsTask();
519 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500520 !this->caps()->performColorClearsAsDraws()) {
521 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400522 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500523 return;
524 } else {
525 // Will use an op for the clear, reset the load op to discard since the op will
526 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400527 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500528 }
529
530 // Must add an op to the list (either because we couldn't use a load op, or because the
531 // clear load op isn't supported)
532 if (this->caps()->performColorClearsAsDraws()) {
533 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
534 GrPaint paint;
535 clear_to_grpaint(color, &paint);
536 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400537 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
538 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500539 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600540 this->addOp(GrClearOp::Make(
541 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500542 }
543 } else {
544 if (this->caps()->performPartialClearsAsDraws()) {
545 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500546 GrPaint paint;
547 clear_to_grpaint(color, &paint);
548
Michael Ludwig64b28a72019-05-28 12:02:00 -0400549 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400550 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
551 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500552 } else {
553 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
554 this->asSurfaceProxy()));
555 // This version of the clear op factory can return null if the clip doesn't intersect
556 // with the surface proxy's boundary
557 if (!op) {
558 return;
559 }
Chris Dalton08755122019-08-05 16:13:47 -0600560 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500561 }
562 }
563}
564
Brian Osman11052242016-10-27 14:47:55 -0400565void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500566 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400567 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000568 // Start with the render target, since that is the maximum content we could possibly fill.
569 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500570 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400571 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000572 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
573 // inverse view matrix.
574 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
575 } else {
576 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
577 SkMatrix localMatrix;
578 if (!viewMatrix.invert(&localMatrix)) {
579 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400580 }
Michael Ludwig61328202019-06-19 14:48:58 +0000581 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
582 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700583 }
robertphillipsea461502015-05-26 11:38:03 -0700584}
585
Michael Ludwig61328202019-06-19 14:48:58 +0000586enum class GrRenderTargetContext::QuadOptimization {
587 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
588 kDiscarded,
589 // The rect to draw was converted to some other op and appended to the oplist, so no additional
590 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
591 // a constColor is provided.
592 kSubmitted,
593 // The clip was folded into the device quad, with updated edge flags and local coords, and
594 // caller is responsible for adding an appropriate op.
595 kClipApplied,
596 // No change to clip, but quad updated to better fit clip/render target, and caller is
597 // responsible for adding an appropriate op.
598 kCropped
599};
Michael Ludwig61a16512019-01-15 11:15:13 -0500600
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400601static bool make_vertex_finite(float* value) {
602 if (SkScalarIsNaN(*value)) {
603 return false;
604 }
605
606 if (!SkScalarIsFinite(*value)) {
607 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
608 // left when calculating crops.
609 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
610 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
611 }
612
613 return true;
614}
615
Michael Ludwig61328202019-06-19 14:48:58 +0000616GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400617 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500618 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000619 // Optimization requirements:
620 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500621 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
622 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
623 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
624 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
625 // 5. kCropped in all other scenarios (although a crop may be a no-op)
626
627 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
628 // better to just keep the old flags instead of introducing mixed edge flags.
629 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000630
Michael Ludwige08b4432019-06-19 18:00:48 -0400631 SkRect rtRect;
632 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400633 // Must use size at which the rendertarget will ultimately be allocated so that stencil
634 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500635 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400636 } else {
637 // Use the logical size of the render target, which allows for "fullscreen" clears even if
638 // the render target has an approximate backing fit
639 rtRect = SkRect::MakeWH(this->width(), this->height());
640 }
641
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500642 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000643 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400644 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
645 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500646 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400647 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500648 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
649 !make_vertex_finite(quad->fDevice.ys() + i) ||
650 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400651 // Discard if we see a nan
652 return QuadOptimization::kDiscarded;
653 }
654 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500655 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400656 }
657 } else {
658 // CropToRect requires the quads to be finite. If they are not finite and we have local
659 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500660 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400661 return QuadOptimization::kDiscarded;
662 }
Brian Salomon7694b902019-06-18 21:00:21 +0000663 }
664
Michael Ludwig61328202019-06-19 14:48:58 +0000665 // If the quad is entirely off screen, it doesn't matter what the clip does
666 if (!rtRect.intersects(drawBounds)) {
667 return QuadOptimization::kDiscarded;
668 }
Brian Salomon7694b902019-06-18 21:00:21 +0000669
Michael Ludwig61328202019-06-19 14:48:58 +0000670 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
671 // the render target).
672 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400673 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
674 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
675 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
676 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000677 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000678 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000679 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
680 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000681 }
Brian Salomon7694b902019-06-18 21:00:21 +0000682 }
683
Michael Ludwig61328202019-06-19 14:48:58 +0000684 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
685 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400686 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000687 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
688 // of the render target and the clip rect)
689 SkRect clipBounds = rtRect;
690 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
691 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000692 }
693
Michael Ludwig61328202019-06-19 14:48:58 +0000694 if (clipRRect.isRect()) {
695 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500696 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700697 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500698 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000699 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500700 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000701 if (drawBounds.contains(rtRect)) {
702 // Fullscreen clear
703 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
704 return QuadOptimization::kSubmitted;
705 } else if (GrClip::IsPixelAligned(drawBounds) &&
706 drawBounds.width() > 256 && drawBounds.height() > 256) {
707 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
708 SkIRect scissorRect;
709 drawBounds.round(&scissorRect);
710 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
711 return QuadOptimization::kSubmitted;
712 }
713 }
714
715 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000716 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500717 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000718 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
719 // properly reflect the smooth edge of the clip.
720 *aa = GrAA::kYes;
721 }
722 // We intentionally do not downgrade AA here because we don't know if we need to
723 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
724 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
725
726 // deviceQuad is exactly the intersection of original quad and clip, so it can be
727 // drawn with no clip (submitted by caller)
728 return QuadOptimization::kClipApplied;
729 } else {
730 // The quads have been updated to better fit the clip bounds, but can't get rid of
731 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500732 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000733 return QuadOptimization::kCropped;
734 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700735 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000736 // Rounded corners and constant filled color (limit ourselves to solid colors because
737 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500738 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
739 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
740 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000741 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
742 // we can draw the rrect directly and ignore the edge flags
743 GrPaint paint;
744 clear_to_grpaint(*constColor, &paint);
745 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
746 clipRRect, GrStyle::SimpleFill());
747 return QuadOptimization::kSubmitted;
748 } else {
749 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500750 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000751 return QuadOptimization::kCropped;
752 }
Brian Salomon7694b902019-06-18 21:00:21 +0000753 }
Brian Salomon7694b902019-06-18 21:00:21 +0000754 }
755
Michael Ludwig61328202019-06-19 14:48:58 +0000756 // Crop the quad to the conservative bounds of the clip.
757 SkIRect clipDevBounds;
758 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
759 SkRect clipBounds = SkRect::Make(clipDevBounds);
760
761 // One final check for discarding, since we may have gone here directly due to a complex clip
762 if (!clipBounds.intersects(drawBounds)) {
763 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000764 }
765
Michael Ludwig61328202019-06-19 14:48:58 +0000766 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
767 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500768 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
769 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000770
771 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000772}
773
Michael Ludwig61328202019-06-19 14:48:58 +0000774void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500775 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500776 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500777 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400778 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000779 ASSERT_SINGLE_OWNER
780 RETURN_IF_ABANDONED
781 SkDEBUGCODE(this->validate();)
782 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500783
Michael Ludwig61328202019-06-19 14:48:58 +0000784 AutoCheckFlush acf(this->drawingManager());
785
786 SkPMColor4f* constColor = nullptr;
787 SkPMColor4f paintColor;
788 if (!ss && !paint.numCoverageFragmentProcessors() &&
789 paint.isConstantBlendedColor(&paintColor)) {
790 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
791 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500792 }
793
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500794 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000795 if (opt >= QuadOptimization::kClipApplied) {
796 // These optimizations require caller to add an op themselves
797 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
798 : clip;
799 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
800 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500801 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
802 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700803 }
Michael Ludwig61328202019-06-19 14:48:58 +0000804 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800805}
806
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000807void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400808 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500809 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000810 sk_sp<GrColorSpaceXform> textureXform,
811 GrSamplerState::Filter filter,
812 const SkPMColor4f& color,
813 SkBlendMode blendMode,
814 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500815 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000816 const SkRect* domain) {
817 ASSERT_SINGLE_OWNER
818 RETURN_IF_ABANDONED
819 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400820 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000821 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
822
823 AutoCheckFlush acf(this->drawingManager());
824
825 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
826 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500827 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000828
829 SkASSERT(opt != QuadOptimization::kSubmitted);
830 if (opt != QuadOptimization::kDiscarded) {
831 // And the texture op if not discarded
832 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
833 : clip;
834 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400835 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400836 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
837 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000838 // Use the provided domain, although hypothetically we could detect that the cropped local
839 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500840 this->addDrawOp(finalClip,
841 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
842 std::move(textureXform), filter, color, saturate,
843 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000844 }
845}
846
Brian Osman11052242016-10-27 14:47:55 -0400847void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500848 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500849 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400850 const SkMatrix& viewMatrix,
851 const SkRect& rect,
852 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700853 if (!style) {
854 style = &GrStyle::SimpleFill();
855 }
joshualitt1de610a2016-01-06 08:26:09 -0800856 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700857 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700858 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400859 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700860
bsalomon6663acf2016-05-10 09:14:17 -0700861 // Path effects should've been devolved to a path in SkGpuDevice
862 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700863
Robert Phillips72152832017-01-25 17:31:35 -0500864 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700865
bsalomon6663acf2016-05-10 09:14:17 -0700866 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400867 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000868 // Fills the rect, using rect as its own local coordinates
869 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500870 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700871 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
872 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
873 if ((!rect.width() || !rect.height()) &&
874 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
875 SkScalar r = stroke.getWidth() / 2;
876 // TODO: Move these stroke->fill fallbacks to GrShape?
877 switch (stroke.getJoin()) {
878 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500879 this->drawRect(
880 clip, std::move(paint), aa, viewMatrix,
881 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
882 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700883 return;
884 case SkPaint::kRound_Join:
885 // Raster draws nothing when both dimensions are empty.
886 if (rect.width() || rect.height()){
887 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500888 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
889 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700890 return;
891 }
892 case SkPaint::kBevel_Join:
893 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500894 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700895 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
896 &GrStyle::SimpleFill());
897 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500898 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700899 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
900 &GrStyle::SimpleFill());
901 }
902 return;
903 }
904 }
robertphillips44302392016-07-08 14:43:03 -0700905
Brian Salomonbaaf4392017-06-15 09:59:23 -0400906 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700907
Chris Dalton7d6748e2019-03-13 00:34:52 -0600908 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500909 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
910 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
911 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500912 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400913 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700914 return;
robertphillips4bc31812016-03-01 12:22:49 -0800915 }
robertphillips4bc31812016-03-01 12:22:49 -0800916 }
Mike Klein16885072018-12-11 09:54:31 -0500917 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500918 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700919}
920
Michael Ludwig69858532018-11-28 15:34:34 -0500921void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
922 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
923 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600924 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500925
926 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
927 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500928}
929
Robert Phillipsec2249f2016-11-09 08:54:35 -0500930int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500931 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400932 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500933}
934
Greg Danielf41b2bd2019-08-22 16:19:24 -0400935GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600936 ) const {
937#if GR_TEST_UTILS
938 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400939 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600940 }
941#endif
942 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
943 // would normally be overwritten. The one exception is if the render target context is marked as
944 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
945 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
946 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
947 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400948 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600949}
950
Chris Dalton858cf232019-10-14 16:20:00 -0600951void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600952 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600953 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600954 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600955
Chris Daltoneffee202019-07-01 22:28:03 -0600956 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600957 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500958 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600959 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400960 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600961 }
962 SkASSERT(numRequiredSamples > 0);
963
964 if (numRequiredSamples > fNumStencilSamples) {
965 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500966 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600967 }
Chris Dalton6b982802019-06-27 13:53:46 -0600968
Chris Dalton674f77a2019-09-30 20:49:39 -0600969 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600970 if (this->caps()->performStencilClearsAsDraws()) {
971 // There is a driver bug with clearing stencil. We must use an op to manually clear the
972 // stencil buffer before the op that required 'setNeedsStencil'.
973 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
974 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600975 this->getOpsTask()->setInitialStencilContent(
976 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600977 }
978 }
979}
980
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000981void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700982 ASSERT_SINGLE_OWNER_PRIV
983 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400984 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400985 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
986 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700987
Robert Phillips72152832017-01-25 17:31:35 -0500988 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400989
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500990 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
991}
992
993void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600994 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
995
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500996 if (this->caps()->performStencilClearsAsDraws()) {
997 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
998 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
999
1000 // Configure the paint to have no impact on the color buffer
1001 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001002 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001003 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1004 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001005 } else {
1006 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001007 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001008 if (!op) {
1009 return;
1010 }
Chris Dalton08755122019-08-05 16:13:47 -06001011 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001012 }
robertphillips976f5f02016-06-03 10:59:20 -07001013}
1014
Chris Daltonbbfd5162017-11-07 13:35:22 -07001015void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001016 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001017 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001018 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001019 ASSERT_SINGLE_OWNER_PRIV
1020 RETURN_IF_ABANDONED_PRIV
1021 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001022 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1023 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001024
Brian Salomon467921e2017-03-06 16:17:12 -05001025 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1026 SkASSERT(path);
1027 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1028
1029 // FIXME: Use path bounds instead of this WAR once
1030 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1031 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1032
1033 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001034 GrAppliedHardClip appliedClip;
1035 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1036 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001037 return;
1038 }
1039
Robert Phillips7c525e62018-06-12 10:11:12 -04001040 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1041 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001042 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001043 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001044 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001045 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001046 if (!op) {
1047 return;
1048 }
Brian Salomon97180af2017-03-14 13:42:58 -04001049 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001050
Chris Daltoneffee202019-07-01 22:28:03 -06001051 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001052 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001053}
1054
Michael Ludwig379e4962019-12-06 13:21:26 -05001055void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1056 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001057 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001058 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1059 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001060 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001061 ASSERT_SINGLE_OWNER
1062 RETURN_IF_ABANDONED
1063 SkDEBUGCODE(this->validate();)
1064 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001065
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001066 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1067 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1068 AutoCheckFlush acf(this->drawingManager());
1069 GrAAType aaType = this->chooseAAType(aa);
1070 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1071 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1072 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001073 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1074 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001075}
1076
Brian Osman11052242016-10-27 14:47:55 -04001077void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001078 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001079 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001080 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001081 GrPrimitiveType* overridePrimType,
1082 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001083 ASSERT_SINGLE_OWNER
1084 RETURN_IF_ABANDONED
1085 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001086 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001087
1088 AutoCheckFlush acf(this->drawingManager());
1089
1090 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001091 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001092 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001093 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001094 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001095 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001096}
1097
1098///////////////////////////////////////////////////////////////////////////////
1099
Brian Osman4d92b892019-03-24 00:53:23 +00001100void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1101 GrPaint&& paint,
1102 const SkMatrix& viewMatrix,
1103 int spriteCount,
1104 const SkRSXform xform[],
1105 const SkRect texRect[],
1106 const SkColor colors[]) {
1107 ASSERT_SINGLE_OWNER
1108 RETURN_IF_ABANDONED
1109 SkDEBUGCODE(this->validate();)
1110 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1111
1112 AutoCheckFlush acf(this->drawingManager());
1113
1114 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1115 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1116 aaType, spriteCount, xform, texRect, colors);
1117 this->addDrawOp(clip, std::move(op));
1118}
1119
1120///////////////////////////////////////////////////////////////////////////////
1121
Brian Osman11052242016-10-27 14:47:55 -04001122void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001123 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001124 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001125 const SkMatrix& viewMatrix,
1126 const SkRRect& rrect,
1127 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001128 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001129 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001130 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001131 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001132
1133 const SkStrokeRec& stroke = style.strokeRec();
1134 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001135 return;
1136 }
1137
bsalomon7f0d9f32016-08-15 14:49:10 -07001138 GrNoClip noclip;
1139 const GrClip* clip = &origClip;
1140#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1141 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001142 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001143 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1144 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001145 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1146 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001147 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001148 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1149 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001150 clip = &noclip;
1151 }
1152#endif
bsalomon6663acf2016-05-10 09:14:17 -07001153 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001154
Robert Phillips72152832017-01-25 17:31:35 -05001155 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001156
Chris Dalton7d6748e2019-03-13 00:34:52 -06001157 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001158
Chris Dalton0dffbab2019-03-27 13:08:50 -06001159 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001160 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1161 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1162 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1163 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1164 // to avoid perf regressions on some platforms.
1165 assert_alive(paint);
1166 op = GrOvalOpFactory::MakeCircularRRectOp(
1167 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1168 }
1169 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001170 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001171 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001172 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001173 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001174 assert_alive(paint);
1175 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001176 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001177 }
1178 if (op) {
1179 this->addDrawOp(*clip, std::move(op));
1180 return;
robertphillipsea461502015-05-26 11:38:03 -07001181 }
robertphillipsb56f9272016-02-25 11:03:52 -08001182
Mike Klein16885072018-12-11 09:54:31 -05001183 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001184 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1185 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001186}
1187
Jim Van Verthc5903412016-11-17 15:27:09 -05001188///////////////////////////////////////////////////////////////////////////////
1189
Jim Van Verth3af1af92017-05-18 15:06:54 -04001190static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1191 SkPoint3 result;
1192 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1193 result.fZ = pt.fZ;
1194 return result;
1195}
1196
1197bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001198 const SkMatrix& viewMatrix,
1199 const SkPath& path,
1200 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001201 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001202 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203 return true;
1204 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001205 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001206 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001207
1208 // check z plane
1209 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1210 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1211 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1212 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1213 return false;
1214 }
1215
1216 SkRRect rrect;
1217 SkRect rect;
1218 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001219 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001220 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1221 if (!isRRect &&
1222 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1223 rect.width() > SK_ScalarNearlyZero) {
1224 rrect.setOval(rect);
1225 isRRect = true;
1226 }
1227 if (!isRRect && path.isRect(&rect)) {
1228 rrect.setRect(rect);
1229 isRRect = true;
1230 }
1231
1232 if (!isRRect) {
1233 return false;
1234 }
1235
Jim Van Verthc5903412016-11-17 15:27:09 -05001236 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001237 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001238 }
1239
Robert Phillips72152832017-01-25 17:31:35 -05001240 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001241
Jim Van Verth3af1af92017-05-18 15:06:54 -04001242 // transform light
1243 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1244
1245 // 1/scale
1246 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001247 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1249 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1250
1251 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001252 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1253
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001254 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001255 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1256 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1257 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001258
1259 // Outset the shadow rrect to the border of the penumbra
1260 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1261 SkRRect ambientRRect;
1262 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1263 // If the rrect was an oval then its outset will also be one.
1264 // We set it explicitly to avoid errors.
1265 if (rrect.isOval()) {
1266 ambientRRect = SkRRect::MakeOval(outsetRect);
1267 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001268 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001269 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1270 }
1271
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001272 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001273 if (transparent) {
1274 // set a large inset to force a fill
1275 devSpaceInsetWidth = ambientRRect.width();
1276 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001277
Robert Phillips7c525e62018-06-12 10:11:12 -04001278 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1279 ambientColor,
1280 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001281 ambientRRect,
1282 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001283 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001284 if (op) {
1285 this->addDrawOp(clip, std::move(op));
1286 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001287 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001288
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001289 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001290 SkScalar devSpaceSpotBlur;
1291 SkScalar spotScale;
1292 SkVector spotOffset;
1293 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1294 devLightPos.fZ, rec.fLightRadius,
1295 &devSpaceSpotBlur, &spotScale, &spotOffset);
1296 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001297 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1298
Jim Van Verth3af1af92017-05-18 15:06:54 -04001299 // Adjust translate for the effect of the scale.
1300 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1301 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1302 // This offset is in dev space, need to transform it into source space.
1303 SkMatrix ctmInverse;
1304 if (viewMatrix.invert(&ctmInverse)) {
1305 ctmInverse.mapPoints(&spotOffset, 1);
1306 } else {
1307 // Since the matrix is a similarity, this should never happen, but just in case...
1308 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1309 SkASSERT(false);
1310 }
1311
1312 // Compute the transformed shadow rrect
1313 SkRRect spotShadowRRect;
1314 SkMatrix shadowTransform;
1315 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1316 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001317 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001318
1319 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001320 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001321 SkScalar insetWidth = blurOutset;
1322 if (transparent) {
1323 // If transparent, just do a fill
1324 insetWidth += spotShadowRRect.width();
1325 } else {
1326 // For shadows, instead of using a stroke we specify an inset from the penumbra
1327 // border. We want to extend this inset area so that it meets up with the caster
1328 // geometry. The inset geometry will by default already be inset by the blur width.
1329 //
1330 // We compare the min and max corners inset by the radius between the original
1331 // rrect and the shadow rrect. The distance between the two plus the difference
1332 // between the scaled radius and the original radius gives the distance from the
1333 // transformed shadow shape to the original shape in that corner. The max
1334 // of these gives the maximum distance we need to cover.
1335 //
1336 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1337 // that to get the full insetWidth.
1338 SkScalar maxOffset;
1339 if (rrect.isRect()) {
1340 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001341 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 rrect.rect().fLeft),
1343 SkTAbs(spotShadowRRect.rect().fTop -
1344 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001345 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001346 rrect.rect().fRight),
1347 SkTAbs(spotShadowRRect.rect().fBottom -
1348 rrect.rect().fBottom)));
1349 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001350 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001351 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1352 rrect.rect().fLeft + dr,
1353 spotShadowRRect.rect().fTop -
1354 rrect.rect().fTop + dr);
1355 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1356 rrect.rect().fRight - dr,
1357 spotShadowRRect.rect().fBottom -
1358 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001359 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001360 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001361 }
Brian Osman788b9162020-02-07 10:36:46 -05001362 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001363 }
1364
1365 // Outset the shadow rrect to the border of the penumbra
1366 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1367 if (spotShadowRRect.isOval()) {
1368 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1369 } else {
1370 SkScalar outsetRad = spotRadius + blurOutset;
1371 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1372 }
1373
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001374 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001375
Robert Phillips7c525e62018-06-12 10:11:12 -04001376 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1377 spotColor,
1378 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001379 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001380 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001381 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001382 if (op) {
1383 this->addDrawOp(clip, std::move(op));
1384 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001385 }
1386
1387 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001388}
1389
1390///////////////////////////////////////////////////////////////////////////////
1391
Brian Osman11052242016-10-27 14:47:55 -04001392bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001393 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001394 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001395 const SkMatrix& viewMatrix,
1396 const SkRRect& origOuter,
1397 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001398 SkASSERT(!origInner.isEmpty());
1399 SkASSERT(!origOuter.isEmpty());
1400
Brian Salomon65749212017-12-01 16:01:47 -05001401 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1402
Chris Dalton7d6748e2019-03-13 00:34:52 -06001403 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001404
1405 if (GrAAType::kMSAA == aaType) {
1406 return false;
1407 }
1408
Greg Daniel2655ede2019-04-10 00:49:28 +00001409 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1410 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001411 auto outerR = outer->width() / 2.f;
1412 auto innerR = inner->width() / 2.f;
1413 auto cx = outer->getBounds().fLeft + outerR;
1414 auto cy = outer->getBounds().fTop + outerR;
1415 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1416 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1417 auto avgR = (innerR + outerR) / 2.f;
1418 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1419 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1420 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001421 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001422 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001423 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001424 if (op) {
1425 this->addDrawOp(clip, std::move(op));
1426 return true;
1427 }
Mike Klein16885072018-12-11 09:54:31 -05001428 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001429 }
1430 }
1431
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001432 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001433 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001434 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1435 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001436 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001437 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1438 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001439 }
robertphillips00095892016-02-29 13:50:40 -08001440
robertphillips00095892016-02-29 13:50:40 -08001441 SkMatrix inverseVM;
1442 if (!viewMatrix.isIdentity()) {
1443 if (!origInner.transform(viewMatrix, inner.writable())) {
1444 return false;
1445 }
1446 if (!origOuter.transform(viewMatrix, outer.writable())) {
1447 return false;
1448 }
1449 if (!viewMatrix.invert(&inverseVM)) {
1450 return false;
1451 }
1452 } else {
1453 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001454 }
robertphillips00095892016-02-29 13:50:40 -08001455
Ethan Nicholaseace9352018-10-15 20:09:54 +00001456 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001457 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001458 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001459 if (!innerEffect) {
1460 return false;
1461 }
1462
Ethan Nicholaseace9352018-10-15 20:09:54 +00001463 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001464 if (!outerEffect) {
1465 return false;
1466 }
1467
Brian Salomon82f44312017-01-11 13:42:54 -05001468 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1469 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001470
1471 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001472 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001473 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1474 }
halcanary9d524f22016-03-29 09:03:52 -07001475
Brian Salomon82f44312017-01-11 13:42:54 -05001476 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1477 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001478 return true;
1479}
1480
Brian Osman11052242016-10-27 14:47:55 -04001481void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001482 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001483 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001484 const SkMatrix& viewMatrix,
1485 const SkRRect& outer,
1486 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001487 ASSERT_SINGLE_OWNER
1488 RETURN_IF_ABANDONED
1489 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001490 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001491
1492 SkASSERT(!outer.isEmpty());
1493 SkASSERT(!inner.isEmpty());
1494
Robert Phillips72152832017-01-25 17:31:35 -05001495 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001496
Brian Salomon82f44312017-01-11 13:42:54 -05001497 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001498 return;
1499 }
Mike Klein16885072018-12-11 09:54:31 -05001500 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001501
1502 SkPath path;
1503 path.setIsVolatile(true);
1504 path.addRRect(inner);
1505 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001506 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001507 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001508}
1509
robertphillipsea461502015-05-26 11:38:03 -07001510///////////////////////////////////////////////////////////////////////////////
1511
Brian Osman11052242016-10-27 14:47:55 -04001512void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001513 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001514 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001515 const SkMatrix& viewMatrix,
1516 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001517 const GrStyle& style,
1518 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001519 ASSERT_SINGLE_OWNER
1520 RETURN_IF_ABANDONED
1521 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001522 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001523
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001524 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001525 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001526 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001528 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1529 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001530 aa = GrAA::kNo;
1531 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001532 }
msarettcc319b92016-08-25 18:07:18 -07001533 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001534 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001535 SkPath path;
1536 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001537 path.setIsVolatile(true);
1538
Brian Salomon82f44312017-01-11 13:42:54 -05001539 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001540 }
1541
Chris Dalton7d6748e2019-03-13 00:34:52 -06001542 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001543 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1544 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001545 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001546}
1547
Brian Osman11052242016-10-27 14:47:55 -04001548void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001549 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001550 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001551 const SkMatrix& viewMatrix,
1552 const SkRect& oval,
1553 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001554 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001555 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001556 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001557 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001558
Robert Phillips7484d202018-07-03 09:09:08 -04001559 const SkStrokeRec& stroke = style.strokeRec();
1560
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001561 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001562 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1563 return;
1564 }
1565
1566 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001567 return;
robertphillipsea461502015-05-26 11:38:03 -07001568 }
1569
Robert Phillips72152832017-01-25 17:31:35 -05001570 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001571
Chris Dalton7d6748e2019-03-13 00:34:52 -06001572 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001573
1574 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001575 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1576 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001577 // We don't draw true circles as round rects in coverage mode, because it can
1578 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1579 assert_alive(paint);
1580 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1581 this->caps()->shaderCaps());
1582 }
1583 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001584 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1585 // the arc equation. This same special geometry and fragment branch also turn out to be a
1586 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1587 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1588 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001589 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001590 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1591 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001592 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001593 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001594 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001595 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1596 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001597 }
1598 if (op) {
1599 this->addDrawOp(clip, std::move(op));
1600 return;
robertphillipsea461502015-05-26 11:38:03 -07001601 }
robertphillipsb56f9272016-02-25 11:03:52 -08001602
Mike Klein16885072018-12-11 09:54:31 -05001603 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001604 this->drawShapeUsingPathRenderer(
1605 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001606 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001607}
1608
Brian Osman11052242016-10-27 14:47:55 -04001609void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001610 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001611 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001612 const SkMatrix& viewMatrix,
1613 const SkRect& oval,
1614 SkScalar startAngle,
1615 SkScalar sweepAngle,
1616 bool useCenter,
1617 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001618 ASSERT_SINGLE_OWNER
1619 RETURN_IF_ABANDONED
1620 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001621 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001622
1623 AutoCheckFlush acf(this->drawingManager());
1624
Chris Dalton7d6748e2019-03-13 00:34:52 -06001625 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001626 if (GrAAType::kCoverage == aaType) {
1627 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1628 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1629 std::move(paint),
1630 viewMatrix,
1631 oval,
1632 startAngle,
1633 sweepAngle,
1634 useCenter,
1635 style,
1636 shaderCaps);
1637 if (op) {
1638 this->addDrawOp(clip, std::move(op));
1639 return;
1640 }
1641 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001642 }
Brian Salomone4949402018-04-26 15:22:04 -04001643 this->drawShapeUsingPathRenderer(
1644 clip, std::move(paint), aa, viewMatrix,
1645 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001646}
1647
Brian Osman11052242016-10-27 14:47:55 -04001648void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001649 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001650 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001651 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001652 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001653 sk_sp<GrColorSpaceXform> csxf,
1654 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001655 std::unique_ptr<SkLatticeIter> iter,
1656 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001657 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001658 RETURN_IF_ABANDONED
1659 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001660 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001661
Robert Phillips72152832017-01-25 17:31:35 -05001662 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001663
Brian Salomon2a943df2018-05-04 13:43:19 -04001664 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001665 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001666 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001667 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001668}
1669
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001670void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1671 const SkRect& bounds) {
1672 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1673 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001674 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001675}
1676
Brian Salomon031b0ba2019-05-23 11:05:26 -04001677void GrRenderTargetContext::asyncRescaleAndReadPixels(
1678 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1679 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1680 auto direct = fContext->priv().asDirectContext();
1681 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001682 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001683 return;
1684 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001685 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001686 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001687 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001688 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001689 if (this->asRenderTargetProxy()->framebufferOnly()) {
1690 callback(context, nullptr);
1691 return;
1692 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001693 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001694 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001695 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001696 return;
1697 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001698 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001699 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001700 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001701 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001702 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001703 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1704 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001705 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001706 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001707 backendFormatOfFinalContext, dstCT);
1708 // Fail if we can't read from the source surface's color type.
1709 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001710 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001711 return;
1712 }
Brian Salomon624f9062019-07-02 14:23:00 -04001713 // Fail if read color type does not have all of dstCT's color channels and those missing color
1714 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001715 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1716 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1717 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1718 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001719 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001720 return;
1721 }
1722
Brian Salomonbf6b9792019-08-21 09:38:10 -04001723 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001724 int x = srcRect.fLeft;
1725 int y = srcRect.fTop;
1726 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001727 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1728 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001729 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001730 return;
1731 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001732 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001733 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001734 x = y = 0;
1735 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001736 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1737 this->colorInfo().alphaType(),
1738 info.colorSpace(),
1739 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001740 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1741 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001742 // We flip or color convert by drawing and we don't currently support drawing to
1743 // kPremul.
1744 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001745 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001746 return;
1747 }
Greg Daniel40903af2020-01-30 14:55:05 -05001748 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001749 SkRect srcRectToDraw = SkRect::Make(srcRect);
1750 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001751 if (!texProxyView.asTextureProxy()) {
1752 texProxyView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
1753 this->origin(), this->colorInfo().colorType(),
1754 GrMipMapped::kNo, srcRect,
1755 SkBackingFit::kApprox, SkBudgeted::kNo);
1756 if (!texProxyView.asTextureProxy()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001757 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001758 return;
1759 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001760 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1761 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001762 tempRTC = GrRenderTargetContext::Make(
1763 direct, this->colorInfo().colorType(), info.refColorSpace(),
1764 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001765 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001766 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001767 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001768 return;
1769 }
Greg Daniel40903af2020-01-30 14:55:05 -05001770 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1771 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1772 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001773 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1774 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1775 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001776 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001777 }
1778 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001779 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001780 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1781 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001782}
1783
Brian Salomon9241a6d2019-10-03 13:26:54 -04001784class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1785public:
1786 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1787 ~AsyncReadResult() override {
1788 for (int i = 0; i < fPlanes.count(); ++i) {
1789 if (!fPlanes[i].fMappedBuffer) {
1790 delete[] static_cast<const char*>(fPlanes[i].fData);
1791 } else {
1792 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1793 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1794 }
1795 }
1796 }
1797
1798 int count() const override { return fPlanes.count(); }
1799 const void* data(int i) const override { return fPlanes[i].fData; }
1800 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1801
1802 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001803 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001804 size_t rowBytes,
1805 GrClientMappedBufferManager* manager) {
1806 SkASSERT(!result.fTransferBuffer->isMapped());
1807 const void* mappedData = result.fTransferBuffer->map();
1808 if (!mappedData) {
1809 return false;
1810 }
1811 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001812 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001813 result.fPixelConverter(convertedData.get(), mappedData);
1814 this->addCpuPlane(std::move(convertedData), rowBytes);
1815 result.fTransferBuffer->unmap();
1816 } else {
1817 manager->insert(result.fTransferBuffer);
1818 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1819 }
1820 return true;
1821 }
1822
1823 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1824 SkASSERT(data);
1825 SkASSERT(rowBytes > 0);
1826 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1827 }
1828
1829private:
1830 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1831 SkASSERT(data);
1832 SkASSERT(rowBytes > 0);
1833 SkASSERT(mappedBuffer);
1834 SkASSERT(mappedBuffer->isMapped());
1835 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1836 }
1837
1838 struct Plane {
1839 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1840 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1841 const void* fData;
1842 size_t fRowBytes;
1843 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1844 sk_sp<GrGpuBuffer> fMappedBuffer;
1845 };
1846 SkSTArray<3, Plane> fPlanes;
1847 uint32_t fInboxID;
1848};
1849
Brian Salomon024bd002019-06-11 11:38:16 -04001850void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1851 ReadPixelsCallback callback,
1852 ReadPixelsContext context) {
1853 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1854 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1855
Greg Danielca9dbe22020-01-02 13:44:30 -05001856 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001857 callback(context, nullptr);
1858 return;
1859 }
1860
Brian Salomon9241a6d2019-10-03 13:26:54 -04001861 auto directContext = fContext->priv().asDirectContext();
1862 SkASSERT(directContext);
1863 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1864
Brian Salomon024bd002019-06-11 11:38:16 -04001865 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1866
1867 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001868 auto ii = SkImageInfo::Make(rect.size(), colorType,
1869 this->colorInfo().alphaType(),
1870 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001871 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001872 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1873 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1874 result->addCpuPlane(std::move(data), pm.rowBytes());
1875
Brian Salomon1d435302019-07-01 13:05:28 -04001876 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001877 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001878 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001879 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001880 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001881 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001882 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001883
Brian Salomonab32f652019-05-10 14:24:50 -04001884 struct FinishContext {
1885 ReadPixelsCallback* fClientCallback;
1886 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001887 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001888 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001889 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001890 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001891 };
1892 // Assumption is that the caller would like to flush. We could take a parameter or require an
1893 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1894 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 auto* finishContext = new FinishContext{callback,
1896 context,
1897 rect.size(),
1898 colorType,
1899 mappedBufferManager,
1900 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001901 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001902 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001903 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001904 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1905 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1906 context->fMappedBufferManager)) {
1907 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001908 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001909 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001910 delete context;
1911 };
1912 GrFlushInfo flushInfo;
1913 flushInfo.fFinishedContext = finishContext;
1914 flushInfo.fFinishedProc = finishCallback;
1915 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1916}
1917
Brian Salomon9241a6d2019-10-03 13:26:54 -04001918void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1919 sk_sp<SkColorSpace> dstColorSpace,
1920 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001921 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001922 RescaleGamma rescaleGamma,
1923 SkFilterQuality rescaleQuality,
1924 ReadPixelsCallback callback,
1925 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001926 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1927 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001928 SkASSERT(!dstSize.isZero());
1929 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1930
Brian Salomon024bd002019-06-11 11:38:16 -04001931 auto direct = fContext->priv().asDirectContext();
1932 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001933 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001934 return;
1935 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001936 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001937 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001938 return;
1939 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001940 if (this->asRenderTargetProxy()->framebufferOnly()) {
1941 callback(context, nullptr);
1942 return;
1943 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001944 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001945 callback(context, nullptr);
1946 return;
1947 }
Brian Salomon024bd002019-06-11 11:38:16 -04001948 int x = srcRect.fLeft;
1949 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001950 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001951 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001952 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001953 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001954 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001955 dstColorSpace);
1956 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001957 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001958 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001959 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001960 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001961 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001962 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001963 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001964 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001965 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001966 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001967 srcView = this->readSurfaceView();
1968 if (!srcView.asTextureProxy()) {
1969 srcView = GrSurfaceProxy::Copy(fContext, fReadView.proxy(), this->origin(),
1970 this->colorInfo().colorType(), GrMipMapped::kNo,
1971 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1972 if (!srcView.asTextureProxy()) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001973 // If we can't get a texture copy of the contents then give up.
1974 callback(context, nullptr);
1975 return;
1976 }
1977 x = y = 0;
1978 }
Brian Salomon4d036892019-07-02 15:10:58 -04001979 // We assume the caller wants kPremul. There is no way to indicate a preference.
1980 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001981 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1982 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001983 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001984 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1985 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001986 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1987 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001988 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001989 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001990 return;
1991 }
Greg Daniel40903af2020-01-30 14:55:05 -05001992 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1993 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1994 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1995 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1996 SkMatrix::I(), std::move(xform));
1997 srcView = tempRTC->readSurfaceView();
1998 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001999 x = y = 0;
2000 }
2001 }
Greg Danielc594e622019-10-15 14:01:49 -04002002
Greg Daniele20fcad2020-01-08 11:52:34 -05002003 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2004 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2005 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002006 int halfW = dstSize.width()/2;
2007 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002008 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2009 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2010 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2011 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2012 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2013 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002014 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002015 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002016 return;
2017 }
2018
Brian Osman28ba5282019-10-30 14:18:07 -04002019 float baseM[20];
2020 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2021
Brian Salomon024bd002019-06-11 11:38:16 -04002022 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2023
2024 auto texMatrix = SkMatrix::MakeTrans(x, y);
2025
Brian Salomon9241a6d2019-10-03 13:26:54 -04002026 SkRect dstRectY = SkRect::Make(dstSize);
2027 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002028
Brian Salomon6aa65052020-01-28 12:16:53 -05002029 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2030 PixelTransferResult yTransfer, uTransfer, vTransfer;
2031
Brian Salomon024bd002019-06-11 11:38:16 -04002032 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2033 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002034 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002035 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002036 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002037 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002038 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002039 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2040 yPaint.addColorFragmentProcessor(std::move(yFP));
2041 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002042 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2043 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002044 if (!doSynchronousRead) {
2045 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2046 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2047 if (!yTransfer.fTransferBuffer) {
2048 callback(context, nullptr);
2049 return;
2050 }
Brian Salomon024bd002019-06-11 11:38:16 -04002051 }
2052
2053 texMatrix.preScale(2.f, 2.f);
2054 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2055 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002056 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002057 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002058 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002059 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002060 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002061 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2062 uPaint.addColorFragmentProcessor(std::move(uFP));
2063 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002064 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2065 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002066 if (!doSynchronousRead) {
2067 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2068 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2069 if (!uTransfer.fTransferBuffer) {
2070 callback(context, nullptr);
2071 return;
2072 }
Brian Salomon024bd002019-06-11 11:38:16 -04002073 }
2074
2075 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2076 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002077 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002078 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002079 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002080 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2081 this->colorInfo().alphaType(), texMatrix,
2082 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002083 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2084 vPaint.addColorFragmentProcessor(std::move(vFP));
2085 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002086 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2087 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002088 if (!doSynchronousRead) {
2089 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2090 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2091 if (!vTransfer.fTransferBuffer) {
2092 callback(context, nullptr);
2093 return;
2094 }
2095 }
2096
2097 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002098 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2099 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2100 size_t yRB = yInfo.minRowBytes();
2101 size_t uvRB = uvInfo.minRowBytes();
2102 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2103 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2104 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2105 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2106 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2107 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002108 callback(context, nullptr);
2109 return;
2110 }
2111 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002112 result->addCpuPlane(std::move(y), yRB );
2113 result->addCpuPlane(std::move(u), uvRB);
2114 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002115 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002116 return;
2117 }
2118
2119 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002120 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002121 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002122 GrClientMappedBufferManager* fMappedBufferManager;
2123 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002124 PixelTransferResult fYTransfer;
2125 PixelTransferResult fUTransfer;
2126 PixelTransferResult fVTransfer;
2127 };
2128 // Assumption is that the caller would like to flush. We could take a parameter or require an
2129 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2130 // callback to GrGpu until after the next flush that flushes our op list, though.
2131 auto* finishContext = new FinishContext{callback,
2132 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002133 direct->priv().clientMappedBufferManager(),
2134 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002135 std::move(yTransfer),
2136 std::move(uTransfer),
2137 std::move(vTransfer)};
2138 auto finishCallback = [](GrGpuFinishedContext c) {
2139 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002140 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002141 auto manager = context->fMappedBufferManager;
2142 size_t rowBytes = SkToSizeT(context->fSize.width());
2143 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2144 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002145 delete context;
2146 return;
2147 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002148 rowBytes /= 2;
2149 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2150 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2151 (*context->fClientCallback)(context->fClientContext, nullptr);
2152 delete context;
2153 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002154 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002155 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2156 (*context->fClientCallback)(context->fClientContext, nullptr);
2157 delete context;
2158 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002159 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002160 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002161 delete context;
2162 };
2163 GrFlushInfo flushInfo;
2164 flushInfo.fFinishedContext = finishContext;
2165 flushInfo.fFinishedProc = finishCallback;
2166 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002167}
2168
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002169GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2170 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002171 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002172 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002173 return GrSemaphoresSubmitted::kNo;
2174 }
robertphillips8c523e02016-07-26 07:41:00 -07002175 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002176 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002177
Greg Daniel55f040b2020-02-13 15:38:32 +00002178 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002179}
2180
Greg Danielc64ee462017-06-15 16:59:49 -04002181bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002182 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002183 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002184 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002185 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002186 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002187
2188 AutoCheckFlush acf(this->drawingManager());
2189
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002190 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002191 return false;
2192 }
2193
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002194 auto direct = fContext->priv().asDirectContext();
2195 if (!direct) {
2196 return false;
2197 }
2198
2199 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002200
Greg Daniel301015c2019-11-18 14:06:46 -05002201 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2202 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002203 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002204 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002205 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2206 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002207 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002208 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2209 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002210 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002211}
joshualitt33a5fce2015-11-18 13:28:51 -08002212
Brian Osman11052242016-10-27 14:47:55 -04002213void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002214 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002215 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002216 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002217 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002218 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002219 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002220 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002221 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002222 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2223
Brian Salomon40b77a62017-12-22 11:25:52 -05002224 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002225
Robert Phillips27927a52018-08-20 13:18:12 -04002226 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002227}
2228
2229void GrRenderTargetContext::drawShape(const GrClip& clip,
2230 GrPaint&& paint,
2231 GrAA aa,
2232 const SkMatrix& viewMatrix,
2233 const GrShape& shape) {
2234 ASSERT_SINGLE_OWNER
2235 RETURN_IF_ABANDONED
2236 SkDEBUGCODE(this->validate();)
2237 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2238
Brian Salomon2fad74a2017-12-20 13:28:55 -05002239 if (shape.isEmpty()) {
2240 if (shape.inverseFilled()) {
2241 this->drawPaint(clip, std::move(paint), viewMatrix);
2242 }
2243 return;
robertphillipsea461502015-05-26 11:38:03 -07002244 }
2245
Robert Phillips72152832017-01-25 17:31:35 -05002246 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002247
Brian Salomon2fad74a2017-12-20 13:28:55 -05002248 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002249 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002250 SkRRect rrect;
2251 // We can ignore the starting point and direction since there is no path effect.
2252 bool inverted;
2253 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2254 if (rrect.isRect()) {
2255 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2256 &shape.style());
2257 return;
2258 } else if (rrect.isOval()) {
2259 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002260 return;
2261 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002262 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2263 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002264 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2265 viewMatrix.rectStaysRect()) {
2266 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2267 // the matrix to all the points individually rather than just to the rect
2268 SkRect rects[2];
2269 if (shape.asNestedRects(rects)) {
2270 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002271 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002272 fContext, std::move(paint), viewMatrix, rects);
2273 if (op) {
2274 this->addDrawOp(clip, std::move(op));
2275 }
2276 // Returning here indicates that there is nothing to draw in this case.
2277 return;
2278 }
robertphillipsea461502015-05-26 11:38:03 -07002279 }
2280 }
robertphillips4bc31812016-03-01 12:22:49 -08002281
Brian Salomon2fad74a2017-12-20 13:28:55 -05002282 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002283}
2284
Chris Daltonbbfd5162017-11-07 13:35:22 -07002285bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002286 const GrUserStencilSettings* ss,
2287 SkRegion::Op op,
2288 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002289 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002290 const SkMatrix& viewMatrix,
2291 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002292 ASSERT_SINGLE_OWNER_PRIV
2293 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002294 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002295 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2296 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002297
2298 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002299 GrPaint paint;
2300 paint.setCoverageSetOpXPFactory(op, invert);
2301 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2302 SkRect::MakeIWH(fRenderTargetContext->width(),
2303 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002304 return true;
2305 }
2306
Robert Phillips72152832017-01-25 17:31:35 -05002307 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002308
2309 // An Assumption here is that path renderer would use some form of tweaking
2310 // the src color (either the input alpha or in the frag shader) to implement
2311 // aa. If we have some future driver-mojo path AA that can do the right
2312 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002313 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002314 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002315
Chris Daltondb91c6e2017-09-08 16:25:08 -06002316 SkIRect clipConservativeBounds;
2317 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2318 &clipConservativeBounds, nullptr);
2319
bsalomon8acedde2016-06-24 10:42:16 -07002320 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002321 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002322 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002323 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002324 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002325 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002326 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002327 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002328 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2329 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002330 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002331
2332 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002333 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002334 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002335 if (!pr) {
2336 return false;
2337 }
2338
2339 GrPaint paint;
2340 paint.setCoverageSetOpXPFactory(op, invert);
2341
Brian Salomonf3569f02017-10-24 12:52:33 -04002342 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2343 std::move(paint),
2344 ss,
2345 fRenderTargetContext,
2346 &clip,
2347 &clipConservativeBounds,
2348 &viewMatrix,
2349 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002350 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002351 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002352 pr->drawPath(args);
2353 return true;
2354}
2355
Brian Osman11052242016-10-27 14:47:55 -04002356SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002357 ASSERT_SINGLE_OWNER_PRIV
2358
Robert Phillips6a6de562019-02-15 15:19:15 -05002359 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002360 return SkBudgeted::kNo;
2361 }
2362
Brian Osman11052242016-10-27 14:47:55 -04002363 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002364
Greg Daniel3912a4b2020-01-14 09:56:04 -05002365 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002366}
2367
Brian Salomon2fad74a2017-12-20 13:28:55 -05002368void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2369 GrPaint&& paint,
2370 GrAA aa,
2371 const SkMatrix& viewMatrix,
2372 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002373 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002374 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002375 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2376
Jim Van Verthf86073a2018-10-02 13:05:38 -04002377 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2378 return;
2379 }
2380
Chris Daltondb91c6e2017-09-08 16:25:08 -06002381 SkIRect clipConservativeBounds;
2382 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2383
Brian Salomon2fad74a2017-12-20 13:28:55 -05002384 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002385 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002386
robertphillips68737822015-10-29 12:12:21 -07002387 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002388 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002389 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002390 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002391 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002392 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002393 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002394 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002395
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002396 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002397 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002398 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002399 return;
2400 }
2401
Chris Dalton6ce447a2019-06-23 18:07:38 -06002402 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002403
2404 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2405 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2406 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2407
Brian Salomon2fad74a2017-12-20 13:28:55 -05002408 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002409 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002410 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2411 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002412 return;
2413 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002414 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002415 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002416 }
2417 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002418 if (canDrawArgs.fShape->style().applies()) {
2419 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2420 styleScale);
2421 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002422 return;
2423 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002424 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002425 // This time, allow SW renderer
2426 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2427 } else {
2428 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002429 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002430 }
robertphillipsea461502015-05-26 11:38:03 -07002431
bsalomon8acedde2016-06-24 10:42:16 -07002432 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002433#ifdef SK_DEBUG
2434 SkDebugf("Unable to find path renderer compatible with path.\n");
2435#endif
2436 return;
2437 }
2438
Robert Phillips256c37b2017-03-01 14:32:46 -05002439 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002440 std::move(paint),
2441 &GrUserStencilSettings::kUnused,
2442 this,
2443 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002444 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002445 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002446 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002447 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002448 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002449 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002450}
2451
Brian Salomon467921e2017-03-06 16:17:12 -05002452static void op_bounds(SkRect* bounds, const GrOp* op) {
2453 *bounds = op->bounds();
2454 if (op->hasZeroArea()) {
2455 if (op->hasAABloat()) {
2456 bounds->outset(0.5f, 0.5f);
2457 } else {
2458 // We don't know which way the particular GPU will snap lines or points at integer
2459 // coords. So we ensure that the bounds is large enough for either snap.
2460 SkRect before = *bounds;
2461 bounds->roundOut(bounds);
2462 if (bounds->fLeft == before.fLeft) {
2463 bounds->fLeft -= 1;
2464 }
2465 if (bounds->fTop == before.fTop) {
2466 bounds->fTop -= 1;
2467 }
2468 if (bounds->fRight == before.fRight) {
2469 bounds->fRight += 1;
2470 }
2471 if (bounds->fBottom == before.fBottom) {
2472 bounds->fBottom += 1;
2473 }
2474 }
2475 }
2476}
2477
Chris Dalton08755122019-08-05 16:13:47 -06002478void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002479 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002480 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2481}
2482
Brian Salomon348a0372018-10-31 10:42:18 -04002483void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2484 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002485 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002486 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002487 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002488 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002489 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002490 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002491 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002492 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002493
Brian Salomon467921e2017-03-06 16:17:12 -05002494 // Setup clip
2495 SkRect bounds;
2496 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002497 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002498 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002499 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002500 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002501
Chris Dalton9acf6822019-11-12 11:52:40 -07002502 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002503 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002504 }
2505
Chris Dalton9acf6822019-11-12 11:52:40 -07002506 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002507 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002508 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002509 }
2510
Chris Dalton9acf6822019-11-12 11:52:40 -07002511 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2512 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2513
2514 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2515 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2516 // that uses a stencil test when the stencil buffer is multisampled.)
2517 bool hasMixedSampledCoverage = (
2518 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002519 SkASSERT(!hasMixedSampledCoverage ||
2520 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002521
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002522 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002523 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002524 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2525
Greg Daniel524e28b2019-11-01 11:48:53 -04002526 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002527 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002528 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002529 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002530 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002531 }
2532 }
2533
2534 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002535 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002536 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002537 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002538 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002539 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002540 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002541}
2542
Greg Daniel524e28b2019-11-01 11:48:53 -04002543bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2544 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002545 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2546 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2547 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002548 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002549 return false;
2550 }
2551
Greg Daniel3912a4b2020-01-14 09:56:04 -05002552 if (this->caps()->textureBarrierSupport() &&
2553 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002554 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002555 // The render target is a texture, so we can read from it directly in the shader. The XP
2556 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002557 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002558 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002559 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002560 }
2561 }
2562
Greg Daniel3912a4b2020-01-14 09:56:04 -05002563 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002564
Eric Karl74480882017-04-03 14:49:05 -07002565 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002566 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002567 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002568 SkRect opBounds = op.bounds();
2569 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2570 // 0.5 pixels.
2571 if (op.hasAABloat() || op.hasZeroArea()) {
2572 opBounds.outset(0.5f, 0.5f);
2573 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2574 // performance we may ignore the clip when the draw is entirely inside the clip is float
2575 // space but will hit pixels just outside the clip when actually rasterizing.
2576 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002577 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002578 }
2579 SkIRect opIBounds;
2580 opBounds.roundOut(&opIBounds);
2581 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002582#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002583 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002584#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002585 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002586 }
2587
Greg Daniel3155f7f2020-01-16 16:54:26 -05002588 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002589 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2590 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002591 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002592 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002593
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002594 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002595 copyRect = clippedRect;
2596 }
Brian Salomon467921e2017-03-06 16:17:12 -05002597
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002598 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002600 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002601 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002602 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002603 } else {
Eric Karl74480882017-04-03 14:49:05 -07002604 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002605 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002606 }
Greg Daniel40903af2020-01-30 14:55:05 -05002607 GrSurfaceProxyView newProxyView =
2608 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), colorType,
2609 GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2610 restrictions.fRectsMustMatch);
2611 SkASSERT(newProxyView.proxy());
Robert Phillipsbf25d432017-04-07 10:08:53 -04002612
Greg Daniel40903af2020-01-30 14:55:05 -05002613 dstProxyView->setProxyView(std::move(newProxyView));
Greg Daniel524e28b2019-11-01 11:48:53 -04002614 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002615 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002616}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002617
Greg Daniel573312e2020-02-07 17:22:35 -05002618bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002619 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002620 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002621 SkIRect clippedSrcRect;
2622 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002623 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2624 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002625 return false;
2626 }
2627
2628 GrPaint paint;
2629 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002630
Greg Danield2ccbb52020-02-05 10:45:39 -05002631 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002632 if (!fp) {
2633 return false;
2634 }
2635 paint.addColorFragmentProcessor(std::move(fp));
2636
2637 this->fillRectToRect(
2638 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2639 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2640 clippedSrcRect.height()),
2641 SkRect::Make(clippedSrcRect));
2642 return true;
2643}