blob: 2fbc50c57d28c43b78c82ecf0d9cb73871bdec2c [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"
Robert Phillipse19babf2020-04-06 13:57:30 -040041#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040043#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/GrRenderTargetContextPriv.h"
45#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrStencilAttachment.h"
47#include "src/gpu/GrStyle.h"
48#include "src/gpu/GrTracing.h"
49#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040050#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040052#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040053#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000054#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000055#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrAtlasTextOp.h"
57#include "src/gpu/ops/GrClearOp.h"
58#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059#include "src/gpu/ops/GrDrawAtlasOp.h"
60#include "src/gpu/ops/GrDrawOp.h"
61#include "src/gpu/ops/GrDrawVerticesOp.h"
62#include "src/gpu/ops/GrDrawableOp.h"
63#include "src/gpu/ops/GrFillRRectOp.h"
64#include "src/gpu/ops/GrFillRectOp.h"
65#include "src/gpu/ops/GrLatticeOp.h"
66#include "src/gpu/ops/GrOp.h"
67#include "src/gpu/ops/GrOvalOpFactory.h"
68#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069#include "src/gpu/ops/GrShadowRRectOp.h"
70#include "src/gpu/ops/GrStencilPathOp.h"
71#include "src/gpu/ops/GrStrokeRectOp.h"
72#include "src/gpu/ops/GrTextureOp.h"
73#include "src/gpu/text/GrTextContext.h"
74#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040075
Herb Derbyc1b482c2018-08-09 15:02:27 -040076class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040077public:
78 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040079 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040081 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040082 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040083
Robert Phillips7c525e62018-06-12 10:11:12 -040084 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040085 fRenderTargetContext->addDrawOp(clip, std::move(op));
86 }
87
Brian Osman449b1152020-04-15 16:43:00 -040088 void drawShape(const GrClip& clip,
89 const SkPaint& paint,
90 const SkMatrixProvider& matrixProvider,
91 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040092 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040093 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 }
95
Brian Osman449b1152020-04-15 16:43:00 -040096 void makeGrPaint(GrMaskFormat maskFormat,
97 const SkPaint& skPaint,
98 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040099 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -0500100 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400101 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400103 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
104 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400105 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400106 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107 }
108 }
109
Robert Phillips69893702019-02-22 11:16:30 -0500110 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 return fRenderTargetContext->fContext;
112 }
113
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter* glyphPainter() override {
115 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400116 }
117
Brian Salomonf18b1d82017-10-27 11:30:49 -0400118private:
119 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400120 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400121
Brian Salomonf18b1d82017-10-27 11:30:49 -0400122};
joshualittbc907352016-01-13 06:45:40 -0800123
Robert Phillips72152832017-01-25 17:31:35 -0500124#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800125#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400126 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800127#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400128 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500129#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
130#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
131#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
132#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
133#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700134
Brian Salomone225b562017-06-14 13:00:03 -0400135//////////////////////////////////////////////////////////////////////////////
136
robertphillipsea461502015-05-26 11:38:03 -0700137class AutoCheckFlush {
138public:
halcanary9d524f22016-03-29 09:03:52 -0700139 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700140 SkASSERT(fDrawingManager);
141 }
bsalomonb77a9072016-09-07 10:02:04 -0700142 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700143
144private:
robertphillips77a2e522015-10-17 07:43:27 -0700145 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700146};
147
Greg Daniele20fcad2020-01-08 11:52:34 -0500148std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
149 GrRecordingContext* context,
150 GrColorType colorType,
151 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500152 sk_sp<GrSurfaceProxy> proxy,
153 GrSurfaceOrigin origin,
154 const SkSurfaceProps* surfaceProps,
155 bool managedOps) {
156 if (!proxy) {
157 return nullptr;
158 }
159
160 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400161 GrSwizzle readSwizzle, writeSwizzle;
162 if (colorType != GrColorType::kUnknown) {
163 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
164 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
165 }
Greg Danielba0ff782020-01-07 15:42:57 -0500166
Greg Daniel3912a4b2020-01-14 09:56:04 -0500167 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400168 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500169
170 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400171 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500172 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500173}
174
175std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
176 GrRecordingContext* context,
177 GrColorType colorType,
178 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500180 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500181 const GrBackendFormat& format,
182 int sampleCnt,
183 GrMipMapped mipMapped,
184 GrProtected isProtected,
185 GrSurfaceOrigin origin,
186 SkBudgeted budgeted,
187 const SkSurfaceProps* surfaceProps) {
188 // It is probably not necessary to check if the context is abandoned here since uses of the
189 // GrRenderTargetContext which need the context will mostly likely fail later on without an
190 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
191 // an abandoned context correctly. It also lets us early out of some extra work.
192 if (context->priv().abandoned()) {
193 return nullptr;
194 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500195
Greg Daniele20fcad2020-01-08 11:52:34 -0500196 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400197 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500198 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500199 if (!proxy) {
200 return nullptr;
201 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500202
Greg Danielba0ff782020-01-07 15:42:57 -0500203 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
204 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500205 if (!rtc) {
206 return nullptr;
207 }
208 rtc->discard();
209 return rtc;
210}
211
212std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
213 GrRecordingContext* context,
214 GrColorType colorType,
215 sk_sp<SkColorSpace> colorSpace,
216 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500217 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500218 int sampleCnt,
219 GrMipMapped mipMapped,
220 GrProtected isProtected,
221 GrSurfaceOrigin origin,
222 SkBudgeted budgeted,
223 const SkSurfaceProps* surfaceProps) {
224 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
225 if (!format.isValid()) {
226 return nullptr;
227 }
228
229 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
230 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
231 surfaceProps);
232}
233
234static inline GrColorType color_type_fallback(GrColorType ct) {
235 switch (ct) {
236 // kRGBA_8888 is our default fallback for many color types that may not have renderable
237 // backend formats.
238 case GrColorType::kAlpha_8:
239 case GrColorType::kBGR_565:
240 case GrColorType::kABGR_4444:
241 case GrColorType::kBGRA_8888:
242 case GrColorType::kRGBA_1010102:
243 case GrColorType::kRGBA_F16:
244 case GrColorType::kRGBA_F16_Clamped:
245 return GrColorType::kRGBA_8888;
246 case GrColorType::kAlpha_F16:
247 return GrColorType::kRGBA_F16;
248 case GrColorType::kGray_8:
249 return GrColorType::kRGB_888x;
250 default:
251 return GrColorType::kUnknown;
252 }
253}
254
Brian Salomond005b692020-04-01 15:47:05 -0400255std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400256 GrImageContext* context, GrColorType colorType, int sampleCnt) {
257 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400258 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400259 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
260 // We continue to the fallback color type if there no default renderable format or we
261 // requested msaa and the format doesn't support msaa.
262 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400263 return {colorType, format};
264 }
265 colorType = color_type_fallback(colorType);
266 } while (colorType != GrColorType::kUnknown);
267 return {GrColorType::kUnknown, {}};
268}
269
Greg Daniele20fcad2020-01-08 11:52:34 -0500270std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
271 GrRecordingContext* context,
272 GrColorType colorType,
273 sk_sp<SkColorSpace> colorSpace,
274 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500275 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500276 int sampleCnt,
277 GrMipMapped mipMapped,
278 GrProtected isProtected,
279 GrSurfaceOrigin origin,
280 SkBudgeted budgeted,
281 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400282 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400283 if (ct == GrColorType::kUnknown) {
284 return nullptr;
285 }
286 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
287 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500288}
289
Greg Danielba0ff782020-01-07 15:42:57 -0500290std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
291 GrRecordingContext* context,
292 GrColorType colorType,
293 sk_sp<SkColorSpace> colorSpace,
294 const GrBackendTexture& tex,
295 int sampleCnt,
296 GrSurfaceOrigin origin,
297 const SkSurfaceProps* surfaceProps,
298 ReleaseProc releaseProc,
299 ReleaseContext releaseCtx) {
300 SkASSERT(sampleCnt > 0);
301 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400302 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500303 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500304 if (!proxy) {
305 return nullptr;
306 }
307
308 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
309 origin, surfaceProps);
310}
311
312std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
313 GrRecordingContext* context,
314 GrColorType colorType,
315 sk_sp<SkColorSpace> colorSpace,
316 const GrBackendTexture& tex,
317 int sampleCnt,
318 GrSurfaceOrigin origin,
319 const SkSurfaceProps* surfaceProps) {
320 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400321 sk_sp<GrSurfaceProxy> proxy(
322 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500323 if (!proxy) {
324 return nullptr;
325 }
326
327 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
328 origin, surfaceProps);
329}
330
331std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
332 GrRecordingContext* context,
333 GrColorType colorType,
334 sk_sp<SkColorSpace> colorSpace,
335 const GrBackendRenderTarget& rt,
336 GrSurfaceOrigin origin,
337 const SkSurfaceProps* surfaceProps,
338 ReleaseProc releaseProc,
339 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400340 sk_sp<GrSurfaceProxy> proxy(
341 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500342 if (!proxy) {
343 return nullptr;
344 }
345
346 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
347 origin, surfaceProps);
348}
349
350std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
351 GrRecordingContext* context,
352 const SkImageInfo& imageInfo,
353 const GrVkDrawableInfo& vkInfo,
354 const SkSurfaceProps* props) {
355 sk_sp<GrSurfaceProxy> proxy(
356 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
357 vkInfo));
358 if (!proxy) {
359 return nullptr;
360 }
361
Greg Danielba0ff782020-01-07 15:42:57 -0500362 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
363 imageInfo.refColorSpace(), std::move(proxy),
364 kTopLeft_GrSurfaceOrigin, props);
365}
366
Greg Danielf41b2bd2019-08-22 16:19:24 -0400367// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
368// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
369// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
370// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500371GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500372 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400373 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400374 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400375 sk_sp<SkColorSpace> colorSpace,
376 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400377 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500378 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
379 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400380 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500381 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400382 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400383 , fManagedOpsTask(managedOpsTask) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400384 if (fOpsTask) {
385 fOpsTask->setClosedObserver(this);
386 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400387 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
388 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500389
Brian Salomonf18b1d82017-10-27 11:30:49 -0400390 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700391 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700392}
393
robertphillips2e1e51f2015-10-15 08:01:48 -0700394#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500395void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400396 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400397 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700398 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700399}
400#endif
401
Brian Osman11052242016-10-27 14:47:55 -0400402GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800403 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400404 if (fOpsTask) {
405 fOpsTask->setClosedObserver(nullptr);
406 }
robertphillipsa106c622015-10-16 09:07:06 -0700407}
408
Chris Dalton7d6748e2019-03-13 00:34:52 -0600409inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600410 if (GrAA::kNo == aa) {
411 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
412 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600413 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600414 return GrAAType::kMSAA;
415 }
416 return GrAAType::kNone;
417 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600418 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600419}
420
Greg Daniele252f082017-10-23 16:05:23 -0400421GrMipMapped GrRenderTargetContext::mipMapped() const {
422 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
423 return proxy->mipMapped();
424 }
425 return GrMipMapped::kNo;
426}
427
Greg Danielf41b2bd2019-08-22 16:19:24 -0400428GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800429 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700430 SkDEBUGCODE(this->validate();)
431
Brian Salomon3b8486a2020-04-21 12:43:26 -0400432 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600433 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400434 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600435 if (fOpsTask && fNumStencilSamples > 0) {
436 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600437 fOpsTask->setMustPreserveStencil();
438 // Reload the stencil buffer content at the beginning of newOpsTask.
439 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
440 // values?
441 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
442 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400443 newOpsTask->setClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600444 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700445 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400446 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400447 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700448}
449
Brian Osman449b1152020-04-15 16:43:00 -0400450void GrRenderTargetContext::drawGlyphRunList(const GrClip& clip,
451 const SkMatrixProvider& matrixProvider,
452 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800453 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700454 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700455 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400456 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700457
Greg Danielbe7fc462019-01-03 16:40:42 -0500458 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
459 // secondary command buffers because it would require stopping and starting a render pass which
460 // we don't have access to.
461 if (this->wrapsVkSecondaryCB()) {
462 return;
463 }
464
Herb Derby26cbe512018-05-24 14:39:01 -0400465 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400466 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400467 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700468}
469
Brian Osman11052242016-10-27 14:47:55 -0400470void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800471 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700472 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700473 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400474 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700475
Robert Phillips72152832017-01-25 17:31:35 -0500476 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400477
Greg Danielf41b2bd2019-08-22 16:19:24 -0400478 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700479}
480
Brian Osman11052242016-10-27 14:47:55 -0400481void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500482 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700483 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800484 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700485 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700486 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400487 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700488
Robert Phillips72152832017-01-25 17:31:35 -0500489 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700490 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
491 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700492}
robertphillips9199a9f2016-07-13 07:48:43 -0700493
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500494void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
495 const SkPMColor4f& color,
496 CanClearFullscreen canClearFullscreen) {
497 ASSERT_SINGLE_OWNER_PRIV
498 RETURN_IF_ABANDONED_PRIV
499 SkDEBUGCODE(fRenderTargetContext->validate();)
500 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
501 fRenderTargetContext->fContext);
502
503 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
504 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
505}
506
507static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
508 paint->setColor4f(color);
509 if (color.isOpaque()) {
510 // Can just rely on the src-over blend mode to do the right thing
511 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
512 } else {
513 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
514 // were src blended
515 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
516 }
517}
518
519void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
520 const SkPMColor4f& color,
521 CanClearFullscreen canClearFullscreen) {
522 bool isFull = false;
523 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400524 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
525 // only clear the entire target if we knew it had not been cleared before. As
526 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500527 isFull = !clip.scissorEnabled() ||
528 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400529 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500530 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
531 }
532
533 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400534 GrOpsTask* opsTask = this->getOpsTask();
535 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 !this->caps()->performColorClearsAsDraws()) {
537 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400538 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500539 return;
540 } else {
541 // Will use an op for the clear, reset the load op to discard since the op will
542 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400543 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500544 }
545
546 // Must add an op to the list (either because we couldn't use a load op, or because the
547 // clear load op isn't supported)
548 if (this->caps()->performColorClearsAsDraws()) {
549 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
550 GrPaint paint;
551 clear_to_grpaint(color, &paint);
552 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400553 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
554 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600556 this->addOp(GrClearOp::Make(
557 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500558 }
559 } else {
560 if (this->caps()->performPartialClearsAsDraws()) {
561 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500562 GrPaint paint;
563 clear_to_grpaint(color, &paint);
564
Michael Ludwig64b28a72019-05-28 12:02:00 -0400565 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400566 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
567 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500568 } else {
569 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
570 this->asSurfaceProxy()));
571 // This version of the clear op factory can return null if the clip doesn't intersect
572 // with the surface proxy's boundary
573 if (!op) {
574 return;
575 }
Chris Dalton08755122019-08-05 16:13:47 -0600576 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500577 }
578 }
579}
580
Brian Osman11052242016-10-27 14:47:55 -0400581void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500582 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400583 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000584 // Start with the render target, since that is the maximum content we could possibly fill.
585 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500586 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400587 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000588 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
589 // inverse view matrix.
590 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
591 } else {
592 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
593 SkMatrix localMatrix;
594 if (!viewMatrix.invert(&localMatrix)) {
595 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400596 }
Michael Ludwig61328202019-06-19 14:48:58 +0000597 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
598 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700599 }
robertphillipsea461502015-05-26 11:38:03 -0700600}
601
Michael Ludwig61328202019-06-19 14:48:58 +0000602enum class GrRenderTargetContext::QuadOptimization {
603 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
604 kDiscarded,
605 // The rect to draw was converted to some other op and appended to the oplist, so no additional
606 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
607 // a constColor is provided.
608 kSubmitted,
609 // The clip was folded into the device quad, with updated edge flags and local coords, and
610 // caller is responsible for adding an appropriate op.
611 kClipApplied,
612 // No change to clip, but quad updated to better fit clip/render target, and caller is
613 // responsible for adding an appropriate op.
614 kCropped
615};
Michael Ludwig61a16512019-01-15 11:15:13 -0500616
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400617static bool make_vertex_finite(float* value) {
618 if (SkScalarIsNaN(*value)) {
619 return false;
620 }
621
622 if (!SkScalarIsFinite(*value)) {
623 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
624 // left when calculating crops.
625 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
626 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
627 }
628
629 return true;
630}
631
Michael Ludwig61328202019-06-19 14:48:58 +0000632GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400633 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500634 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000635 // Optimization requirements:
636 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500637 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
638 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
639 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
640 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
641 // 5. kCropped in all other scenarios (although a crop may be a no-op)
642
643 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
644 // better to just keep the old flags instead of introducing mixed edge flags.
645 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000646
Michael Ludwige08b4432019-06-19 18:00:48 -0400647 SkRect rtRect;
648 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400649 // Must use size at which the rendertarget will ultimately be allocated so that stencil
650 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500651 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400652 } else {
653 // Use the logical size of the render target, which allows for "fullscreen" clears even if
654 // the render target has an approximate backing fit
655 rtRect = SkRect::MakeWH(this->width(), this->height());
656 }
657
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500658 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000659 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400660 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
661 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500662 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400663 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500664 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
665 !make_vertex_finite(quad->fDevice.ys() + i) ||
666 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400667 // Discard if we see a nan
668 return QuadOptimization::kDiscarded;
669 }
670 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500671 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400672 }
673 } else {
674 // CropToRect requires the quads to be finite. If they are not finite and we have local
675 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500676 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400677 return QuadOptimization::kDiscarded;
678 }
Brian Salomon7694b902019-06-18 21:00:21 +0000679 }
680
Michael Ludwig61328202019-06-19 14:48:58 +0000681 // If the quad is entirely off screen, it doesn't matter what the clip does
682 if (!rtRect.intersects(drawBounds)) {
683 return QuadOptimization::kDiscarded;
684 }
Brian Salomon7694b902019-06-18 21:00:21 +0000685
Michael Ludwig61328202019-06-19 14:48:58 +0000686 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
687 // the render target).
688 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400689 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
690 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
691 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
692 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000693 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000694 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000695 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
696 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000697 }
Brian Salomon7694b902019-06-18 21:00:21 +0000698 }
699
Michael Ludwig61328202019-06-19 14:48:58 +0000700 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
701 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400702 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000703 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
704 // of the render target and the clip rect)
705 SkRect clipBounds = rtRect;
706 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
707 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000708 }
709
Michael Ludwig61328202019-06-19 14:48:58 +0000710 if (clipRRect.isRect()) {
711 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500712 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700713 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500714 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000715 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500716 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000717 if (drawBounds.contains(rtRect)) {
718 // Fullscreen clear
719 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
720 return QuadOptimization::kSubmitted;
721 } else if (GrClip::IsPixelAligned(drawBounds) &&
722 drawBounds.width() > 256 && drawBounds.height() > 256) {
723 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
724 SkIRect scissorRect;
725 drawBounds.round(&scissorRect);
726 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
727 return QuadOptimization::kSubmitted;
728 }
729 }
730
731 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000732 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500733 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000734 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
735 // properly reflect the smooth edge of the clip.
736 *aa = GrAA::kYes;
737 }
738 // We intentionally do not downgrade AA here because we don't know if we need to
739 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
740 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
741
742 // deviceQuad is exactly the intersection of original quad and clip, so it can be
743 // drawn with no clip (submitted by caller)
744 return QuadOptimization::kClipApplied;
745 } else {
746 // The quads have been updated to better fit the clip bounds, but can't get rid of
747 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500748 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000749 return QuadOptimization::kCropped;
750 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700751 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000752 // Rounded corners and constant filled color (limit ourselves to solid colors because
753 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500754 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
755 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
756 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000757 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
758 // we can draw the rrect directly and ignore the edge flags
759 GrPaint paint;
760 clear_to_grpaint(*constColor, &paint);
761 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
762 clipRRect, GrStyle::SimpleFill());
763 return QuadOptimization::kSubmitted;
764 } else {
765 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500766 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000767 return QuadOptimization::kCropped;
768 }
Brian Salomon7694b902019-06-18 21:00:21 +0000769 }
Brian Salomon7694b902019-06-18 21:00:21 +0000770 }
771
Michael Ludwig61328202019-06-19 14:48:58 +0000772 // Crop the quad to the conservative bounds of the clip.
773 SkIRect clipDevBounds;
774 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
775 SkRect clipBounds = SkRect::Make(clipDevBounds);
776
777 // One final check for discarding, since we may have gone here directly due to a complex clip
778 if (!clipBounds.intersects(drawBounds)) {
779 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000780 }
781
Michael Ludwig61328202019-06-19 14:48:58 +0000782 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
783 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500784 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
785 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000786
787 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000788}
789
Michael Ludwig61328202019-06-19 14:48:58 +0000790void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500791 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500792 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500793 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400794 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000795 ASSERT_SINGLE_OWNER
796 RETURN_IF_ABANDONED
797 SkDEBUGCODE(this->validate();)
798 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500799
Michael Ludwig61328202019-06-19 14:48:58 +0000800 AutoCheckFlush acf(this->drawingManager());
801
802 SkPMColor4f* constColor = nullptr;
803 SkPMColor4f paintColor;
804 if (!ss && !paint.numCoverageFragmentProcessors() &&
805 paint.isConstantBlendedColor(&paintColor)) {
806 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
807 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500808 }
809
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500810 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000811 if (opt >= QuadOptimization::kClipApplied) {
812 // These optimizations require caller to add an op themselves
813 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
814 : clip;
815 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
816 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500817 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
818 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700819 }
Michael Ludwig61328202019-06-19 14:48:58 +0000820 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800821}
822
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000823void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400824 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500825 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000826 sk_sp<GrColorSpaceXform> textureXform,
827 GrSamplerState::Filter filter,
828 const SkPMColor4f& color,
829 SkBlendMode blendMode,
830 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500831 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000832 const SkRect* domain) {
833 ASSERT_SINGLE_OWNER
834 RETURN_IF_ABANDONED
835 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400836 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000837 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
838
839 AutoCheckFlush acf(this->drawingManager());
840
841 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
842 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500843 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000844
845 SkASSERT(opt != QuadOptimization::kSubmitted);
846 if (opt != QuadOptimization::kDiscarded) {
847 // And the texture op if not discarded
848 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
849 : clip;
850 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400851 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400852 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
853 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000854 // Use the provided domain, although hypothetically we could detect that the cropped local
855 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500856 this->addDrawOp(finalClip,
857 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
858 std::move(textureXform), filter, color, saturate,
859 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000860 }
861}
862
Brian Osman11052242016-10-27 14:47:55 -0400863void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500864 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500865 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400866 const SkMatrix& viewMatrix,
867 const SkRect& rect,
868 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700869 if (!style) {
870 style = &GrStyle::SimpleFill();
871 }
joshualitt1de610a2016-01-06 08:26:09 -0800872 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700873 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700874 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400875 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700876
bsalomon6663acf2016-05-10 09:14:17 -0700877 // Path effects should've been devolved to a path in SkGpuDevice
878 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700879
Robert Phillips72152832017-01-25 17:31:35 -0500880 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700881
bsalomon6663acf2016-05-10 09:14:17 -0700882 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400883 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000884 // Fills the rect, using rect as its own local coordinates
885 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500886 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700887 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
888 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
889 if ((!rect.width() || !rect.height()) &&
890 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
891 SkScalar r = stroke.getWidth() / 2;
Michael Ludwig2686d692020-04-17 20:21:37 +0000892 // TODO: Move these stroke->fill fallbacks to GrStyledShape?
bsalomona7d85ba2016-07-06 11:54:59 -0700893 switch (stroke.getJoin()) {
894 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500895 this->drawRect(
896 clip, std::move(paint), aa, viewMatrix,
897 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
898 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700899 return;
900 case SkPaint::kRound_Join:
901 // Raster draws nothing when both dimensions are empty.
902 if (rect.width() || rect.height()){
903 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500904 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
905 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700906 return;
907 }
908 case SkPaint::kBevel_Join:
909 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500910 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700911 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
912 &GrStyle::SimpleFill());
913 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500914 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700915 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
916 &GrStyle::SimpleFill());
917 }
918 return;
919 }
920 }
robertphillips44302392016-07-08 14:43:03 -0700921
Brian Salomonbaaf4392017-06-15 09:59:23 -0400922 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700923
Chris Dalton7d6748e2019-03-13 00:34:52 -0600924 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500925 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
926 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
927 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500928 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400929 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700930 return;
robertphillips4bc31812016-03-01 12:22:49 -0800931 }
robertphillips4bc31812016-03-01 12:22:49 -0800932 }
Mike Klein16885072018-12-11 09:54:31 -0500933 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000934 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
935 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700936}
937
Michael Ludwig69858532018-11-28 15:34:34 -0500938void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
939 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
940 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600941 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500942
943 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
944 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500945}
946
Robert Phillipsec2249f2016-11-09 08:54:35 -0500947int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500948 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400949 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500950}
951
Greg Danielf41b2bd2019-08-22 16:19:24 -0400952GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600953 ) const {
954#if GR_TEST_UTILS
955 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400956 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600957 }
958#endif
959 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
960 // would normally be overwritten. The one exception is if the render target context is marked as
961 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
962 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
963 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
964 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400965 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600966}
967
Chris Dalton858cf232019-10-14 16:20:00 -0600968void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600969 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600970 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600971 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600972
Chris Daltoneffee202019-07-01 22:28:03 -0600973 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600974 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500975 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600976 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400977 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600978 }
979 SkASSERT(numRequiredSamples > 0);
980
981 if (numRequiredSamples > fNumStencilSamples) {
982 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500983 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600984 }
Chris Dalton6b982802019-06-27 13:53:46 -0600985
Chris Dalton674f77a2019-09-30 20:49:39 -0600986 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600987 if (this->caps()->performStencilClearsAsDraws()) {
988 // There is a driver bug with clearing stencil. We must use an op to manually clear the
989 // stencil buffer before the op that required 'setNeedsStencil'.
990 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
991 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600992 this->getOpsTask()->setInitialStencilContent(
993 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600994 }
995 }
996}
997
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000998void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700999 ASSERT_SINGLE_OWNER_PRIV
1000 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001001 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001002 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
1003 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -07001004
Robert Phillips72152832017-01-25 17:31:35 -05001005 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001006
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001007 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
1008}
1009
1010void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001011 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1012
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001013 if (this->caps()->performStencilClearsAsDraws()) {
1014 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
1015 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
1016
1017 // Configure the paint to have no impact on the color buffer
1018 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001019 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001020 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1021 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001022 } else {
1023 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001024 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001025 if (!op) {
1026 return;
1027 }
Chris Dalton08755122019-08-05 16:13:47 -06001028 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001029 }
robertphillips976f5f02016-06-03 10:59:20 -07001030}
1031
Chris Daltonbbfd5162017-11-07 13:35:22 -07001032void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001033 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001034 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001035 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001036 ASSERT_SINGLE_OWNER_PRIV
1037 RETURN_IF_ABANDONED_PRIV
1038 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001039 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1040 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001041
Brian Salomon467921e2017-03-06 16:17:12 -05001042 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1043 SkASSERT(path);
1044 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1045
1046 // FIXME: Use path bounds instead of this WAR once
1047 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1048 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1049
1050 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001051 GrAppliedHardClip appliedClip;
1052 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1053 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001054 return;
1055 }
1056
Robert Phillips7c525e62018-06-12 10:11:12 -04001057 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1058 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001059 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001060 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001061 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001062 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001063 if (!op) {
1064 return;
1065 }
Brian Salomon97180af2017-03-14 13:42:58 -04001066 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001067
Chris Daltoneffee202019-07-01 22:28:03 -06001068 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001069 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001070}
1071
Michael Ludwig379e4962019-12-06 13:21:26 -05001072void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1073 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001074 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001075 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1076 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001077 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001078 ASSERT_SINGLE_OWNER
1079 RETURN_IF_ABANDONED
1080 SkDEBUGCODE(this->validate();)
1081 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001082
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001083 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1084 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1085 AutoCheckFlush acf(this->drawingManager());
1086 GrAAType aaType = this->chooseAAType(aa);
1087 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1088 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1089 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001090 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1091 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001092}
1093
Brian Osman11052242016-10-27 14:47:55 -04001094void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001095 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001096 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001097 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001098 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001099 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001100 ASSERT_SINGLE_OWNER
1101 RETURN_IF_ABANDONED
1102 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001103 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001104
1105 AutoCheckFlush acf(this->drawingManager());
1106
1107 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001108 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001109 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001110 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001111 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001112 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001113 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001114}
1115
1116///////////////////////////////////////////////////////////////////////////////
1117
Brian Osman4d92b892019-03-24 00:53:23 +00001118void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1119 GrPaint&& paint,
1120 const SkMatrix& viewMatrix,
1121 int spriteCount,
1122 const SkRSXform xform[],
1123 const SkRect texRect[],
1124 const SkColor colors[]) {
1125 ASSERT_SINGLE_OWNER
1126 RETURN_IF_ABANDONED
1127 SkDEBUGCODE(this->validate();)
1128 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1129
1130 AutoCheckFlush acf(this->drawingManager());
1131
1132 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1133 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1134 aaType, spriteCount, xform, texRect, colors);
1135 this->addDrawOp(clip, std::move(op));
1136}
1137
1138///////////////////////////////////////////////////////////////////////////////
1139
Brian Osman11052242016-10-27 14:47:55 -04001140void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001141 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001142 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001143 const SkMatrix& viewMatrix,
1144 const SkRRect& rrect,
1145 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001146 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001147 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001148 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001149 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001150
1151 const SkStrokeRec& stroke = style.strokeRec();
1152 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001153 return;
1154 }
1155
bsalomon7f0d9f32016-08-15 14:49:10 -07001156 GrNoClip noclip;
1157 const GrClip* clip = &origClip;
1158#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1159 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001160 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001161 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1162 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001163 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1164 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001165 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001166 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1167 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001168 clip = &noclip;
1169 }
1170#endif
bsalomon6663acf2016-05-10 09:14:17 -07001171 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001172
Robert Phillips72152832017-01-25 17:31:35 -05001173 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001174
Chris Dalton7d6748e2019-03-13 00:34:52 -06001175 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001176
Chris Dalton0dffbab2019-03-27 13:08:50 -06001177 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001178 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1179 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1180 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1181 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1182 // to avoid perf regressions on some platforms.
1183 assert_alive(paint);
1184 op = GrOvalOpFactory::MakeCircularRRectOp(
1185 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1186 }
1187 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001188 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001189 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001190 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001191 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001192 assert_alive(paint);
1193 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001194 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001195 }
1196 if (op) {
1197 this->addDrawOp(*clip, std::move(op));
1198 return;
robertphillipsea461502015-05-26 11:38:03 -07001199 }
robertphillipsb56f9272016-02-25 11:03:52 -08001200
Mike Klein16885072018-12-11 09:54:31 -05001201 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001202 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001203 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001204}
1205
Jim Van Verthc5903412016-11-17 15:27:09 -05001206///////////////////////////////////////////////////////////////////////////////
1207
Jim Van Verth3af1af92017-05-18 15:06:54 -04001208static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1209 SkPoint3 result;
1210 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1211 result.fZ = pt.fZ;
1212 return result;
1213}
1214
1215bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001216 const SkMatrix& viewMatrix,
1217 const SkPath& path,
1218 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001219 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001220 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001221 return true;
1222 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001223 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001224 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001225
1226 // check z plane
1227 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1228 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1229 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1230 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1231 return false;
1232 }
1233
1234 SkRRect rrect;
1235 SkRect rect;
1236 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001237 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001238 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1239 if (!isRRect &&
1240 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1241 rect.width() > SK_ScalarNearlyZero) {
1242 rrect.setOval(rect);
1243 isRRect = true;
1244 }
1245 if (!isRRect && path.isRect(&rect)) {
1246 rrect.setRect(rect);
1247 isRRect = true;
1248 }
1249
1250 if (!isRRect) {
1251 return false;
1252 }
1253
Jim Van Verthc5903412016-11-17 15:27:09 -05001254 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001256 }
1257
Robert Phillips72152832017-01-25 17:31:35 -05001258 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001259
Jim Van Verth3af1af92017-05-18 15:06:54 -04001260 // transform light
1261 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1262
1263 // 1/scale
1264 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001265 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001266 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1267 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1268
1269 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001270 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1271
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001272 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001273 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1274 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1275 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001276
1277 // Outset the shadow rrect to the border of the penumbra
1278 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1279 SkRRect ambientRRect;
1280 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1281 // If the rrect was an oval then its outset will also be one.
1282 // We set it explicitly to avoid errors.
1283 if (rrect.isOval()) {
1284 ambientRRect = SkRRect::MakeOval(outsetRect);
1285 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001286 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001287 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1288 }
1289
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001290 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001291 if (transparent) {
1292 // set a large inset to force a fill
1293 devSpaceInsetWidth = ambientRRect.width();
1294 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001295
Robert Phillips7c525e62018-06-12 10:11:12 -04001296 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1297 ambientColor,
1298 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001299 ambientRRect,
1300 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001301 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001302 if (op) {
1303 this->addDrawOp(clip, std::move(op));
1304 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001305 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001306
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001307 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001308 SkScalar devSpaceSpotBlur;
1309 SkScalar spotScale;
1310 SkVector spotOffset;
1311 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1312 devLightPos.fZ, rec.fLightRadius,
1313 &devSpaceSpotBlur, &spotScale, &spotOffset);
1314 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001315 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1316
Jim Van Verth3af1af92017-05-18 15:06:54 -04001317 // Adjust translate for the effect of the scale.
1318 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1319 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1320 // This offset is in dev space, need to transform it into source space.
1321 SkMatrix ctmInverse;
1322 if (viewMatrix.invert(&ctmInverse)) {
1323 ctmInverse.mapPoints(&spotOffset, 1);
1324 } else {
1325 // Since the matrix is a similarity, this should never happen, but just in case...
1326 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1327 SkASSERT(false);
1328 }
1329
1330 // Compute the transformed shadow rrect
1331 SkRRect spotShadowRRect;
1332 SkMatrix shadowTransform;
1333 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1334 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001335 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001336
1337 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001338 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001339 SkScalar insetWidth = blurOutset;
1340 if (transparent) {
1341 // If transparent, just do a fill
1342 insetWidth += spotShadowRRect.width();
1343 } else {
1344 // For shadows, instead of using a stroke we specify an inset from the penumbra
1345 // border. We want to extend this inset area so that it meets up with the caster
1346 // geometry. The inset geometry will by default already be inset by the blur width.
1347 //
1348 // We compare the min and max corners inset by the radius between the original
1349 // rrect and the shadow rrect. The distance between the two plus the difference
1350 // between the scaled radius and the original radius gives the distance from the
1351 // transformed shadow shape to the original shape in that corner. The max
1352 // of these gives the maximum distance we need to cover.
1353 //
1354 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1355 // that to get the full insetWidth.
1356 SkScalar maxOffset;
1357 if (rrect.isRect()) {
1358 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001359 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001360 rrect.rect().fLeft),
1361 SkTAbs(spotShadowRRect.rect().fTop -
1362 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001363 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001364 rrect.rect().fRight),
1365 SkTAbs(spotShadowRRect.rect().fBottom -
1366 rrect.rect().fBottom)));
1367 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001368 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001369 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1370 rrect.rect().fLeft + dr,
1371 spotShadowRRect.rect().fTop -
1372 rrect.rect().fTop + dr);
1373 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1374 rrect.rect().fRight - dr,
1375 spotShadowRRect.rect().fBottom -
1376 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001377 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001378 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001379 }
Brian Osman788b9162020-02-07 10:36:46 -05001380 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001381 }
1382
1383 // Outset the shadow rrect to the border of the penumbra
1384 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1385 if (spotShadowRRect.isOval()) {
1386 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1387 } else {
1388 SkScalar outsetRad = spotRadius + blurOutset;
1389 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1390 }
1391
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001392 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001393
Robert Phillips7c525e62018-06-12 10:11:12 -04001394 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1395 spotColor,
1396 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001397 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001398 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001399 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001400 if (op) {
1401 this->addDrawOp(clip, std::move(op));
1402 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001403 }
1404
1405 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001406}
1407
1408///////////////////////////////////////////////////////////////////////////////
1409
Brian Osman11052242016-10-27 14:47:55 -04001410bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001411 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001412 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001413 const SkMatrix& viewMatrix,
1414 const SkRRect& origOuter,
1415 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001416 SkASSERT(!origInner.isEmpty());
1417 SkASSERT(!origOuter.isEmpty());
1418
Brian Salomon65749212017-12-01 16:01:47 -05001419 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1420
Chris Dalton7d6748e2019-03-13 00:34:52 -06001421 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001422
1423 if (GrAAType::kMSAA == aaType) {
1424 return false;
1425 }
1426
Greg Daniel2655ede2019-04-10 00:49:28 +00001427 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1428 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001429 auto outerR = outer->width() / 2.f;
1430 auto innerR = inner->width() / 2.f;
1431 auto cx = outer->getBounds().fLeft + outerR;
1432 auto cy = outer->getBounds().fTop + outerR;
1433 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1434 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1435 auto avgR = (innerR + outerR) / 2.f;
1436 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1437 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1438 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001439 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001440 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001441 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001442 if (op) {
1443 this->addDrawOp(clip, std::move(op));
1444 return true;
1445 }
Mike Klein16885072018-12-11 09:54:31 -05001446 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001447 }
1448 }
1449
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001450 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001451 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001452 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1453 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001454 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001455 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1456 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001457 }
robertphillips00095892016-02-29 13:50:40 -08001458
robertphillips00095892016-02-29 13:50:40 -08001459 SkMatrix inverseVM;
1460 if (!viewMatrix.isIdentity()) {
1461 if (!origInner.transform(viewMatrix, inner.writable())) {
1462 return false;
1463 }
1464 if (!origOuter.transform(viewMatrix, outer.writable())) {
1465 return false;
1466 }
1467 if (!viewMatrix.invert(&inverseVM)) {
1468 return false;
1469 }
1470 } else {
1471 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001472 }
robertphillips00095892016-02-29 13:50:40 -08001473
Ethan Nicholaseace9352018-10-15 20:09:54 +00001474 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001475 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001476 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001477 if (!innerEffect) {
1478 return false;
1479 }
1480
Ethan Nicholaseace9352018-10-15 20:09:54 +00001481 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001482 if (!outerEffect) {
1483 return false;
1484 }
1485
Brian Salomon82f44312017-01-11 13:42:54 -05001486 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1487 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001488
1489 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001490 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001491 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1492 }
halcanary9d524f22016-03-29 09:03:52 -07001493
Brian Salomon82f44312017-01-11 13:42:54 -05001494 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1495 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001496 return true;
1497}
1498
Brian Osman11052242016-10-27 14:47:55 -04001499void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001500 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001501 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001502 const SkMatrix& viewMatrix,
1503 const SkRRect& outer,
1504 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001505 ASSERT_SINGLE_OWNER
1506 RETURN_IF_ABANDONED
1507 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001508 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001509
1510 SkASSERT(!outer.isEmpty());
1511 SkASSERT(!inner.isEmpty());
1512
Robert Phillips72152832017-01-25 17:31:35 -05001513 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001514
Brian Salomon82f44312017-01-11 13:42:54 -05001515 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001516 return;
1517 }
Mike Klein16885072018-12-11 09:54:31 -05001518 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001519
1520 SkPath path;
1521 path.setIsVolatile(true);
1522 path.addRRect(inner);
1523 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001524 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001525 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001526}
1527
robertphillipsea461502015-05-26 11:38:03 -07001528///////////////////////////////////////////////////////////////////////////////
1529
Brian Osman11052242016-10-27 14:47:55 -04001530void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001531 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001532 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001533 const SkMatrix& viewMatrix,
1534 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001535 const GrStyle& style,
1536 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001537 ASSERT_SINGLE_OWNER
1538 RETURN_IF_ABANDONED
1539 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001540 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001541
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001542 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001543 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001544 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001545 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001546 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1547 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001548 aa = GrAA::kNo;
1549 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001550 }
msarettcc319b92016-08-25 18:07:18 -07001551 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001552 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001553 SkPath path;
1554 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001555 path.setIsVolatile(true);
1556
Brian Salomon82f44312017-01-11 13:42:54 -05001557 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001558 }
1559
Chris Dalton7d6748e2019-03-13 00:34:52 -06001560 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001561 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1562 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001563 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001564}
1565
Brian Osman11052242016-10-27 14:47:55 -04001566void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001567 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001568 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001569 const SkMatrix& viewMatrix,
1570 const SkRect& oval,
1571 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001572 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001573 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001574 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001575 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001576
Robert Phillips7484d202018-07-03 09:09:08 -04001577 const SkStrokeRec& stroke = style.strokeRec();
1578
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001579 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001580 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1581 return;
1582 }
1583
1584 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001585 return;
robertphillipsea461502015-05-26 11:38:03 -07001586 }
1587
Robert Phillips72152832017-01-25 17:31:35 -05001588 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001589
Chris Dalton7d6748e2019-03-13 00:34:52 -06001590 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001591
1592 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001593 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1594 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001595 // We don't draw true circles as round rects in coverage mode, because it can
1596 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1597 assert_alive(paint);
1598 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1599 this->caps()->shaderCaps());
1600 }
1601 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001602 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1603 // the arc equation. This same special geometry and fragment branch also turn out to be a
1604 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1605 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1606 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001607 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001608 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1609 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001610 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001611 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001612 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001613 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1614 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001615 }
1616 if (op) {
1617 this->addDrawOp(clip, std::move(op));
1618 return;
robertphillipsea461502015-05-26 11:38:03 -07001619 }
robertphillipsb56f9272016-02-25 11:03:52 -08001620
Mike Klein16885072018-12-11 09:54:31 -05001621 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001622 this->drawShapeUsingPathRenderer(
1623 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001624 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001625}
1626
Brian Osman11052242016-10-27 14:47:55 -04001627void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001628 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001629 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001630 const SkMatrix& viewMatrix,
1631 const SkRect& oval,
1632 SkScalar startAngle,
1633 SkScalar sweepAngle,
1634 bool useCenter,
1635 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001636 ASSERT_SINGLE_OWNER
1637 RETURN_IF_ABANDONED
1638 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001639 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001640
1641 AutoCheckFlush acf(this->drawingManager());
1642
Chris Dalton7d6748e2019-03-13 00:34:52 -06001643 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001644 if (GrAAType::kCoverage == aaType) {
1645 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1646 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1647 std::move(paint),
1648 viewMatrix,
1649 oval,
1650 startAngle,
1651 sweepAngle,
1652 useCenter,
1653 style,
1654 shaderCaps);
1655 if (op) {
1656 this->addDrawOp(clip, std::move(op));
1657 return;
1658 }
1659 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001660 }
Brian Salomone4949402018-04-26 15:22:04 -04001661 this->drawShapeUsingPathRenderer(
1662 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001663 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001664}
1665
Brian Osman11052242016-10-27 14:47:55 -04001666void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001667 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001668 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001669 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001670 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001671 sk_sp<GrColorSpaceXform> csxf,
1672 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001673 std::unique_ptr<SkLatticeIter> iter,
1674 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001675 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001676 RETURN_IF_ABANDONED
1677 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001678 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001679
Robert Phillips72152832017-01-25 17:31:35 -05001680 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001681
Brian Salomon2a943df2018-05-04 13:43:19 -04001682 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001683 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001684 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001685 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001686}
1687
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001688void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1689 const SkRect& bounds) {
1690 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1691 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001692 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001693}
1694
Brian Salomon031b0ba2019-05-23 11:05:26 -04001695void GrRenderTargetContext::asyncRescaleAndReadPixels(
1696 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1697 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1698 auto direct = fContext->priv().asDirectContext();
1699 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001700 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001701 return;
1702 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001703 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001704 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001705 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001706 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001707 if (this->asRenderTargetProxy()->framebufferOnly()) {
1708 callback(context, nullptr);
1709 return;
1710 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001711 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001712 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001713 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001714 return;
1715 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001717 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001718 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001719 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001720 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001721 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1722 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001723 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001724 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001725 backendFormatOfFinalContext, dstCT);
1726 // Fail if we can't read from the source surface's color type.
1727 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001728 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001729 return;
1730 }
Brian Salomon624f9062019-07-02 14:23:00 -04001731 // Fail if read color type does not have all of dstCT's color channels and those missing color
1732 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001733 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1734 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1735 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1736 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001737 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001738 return;
1739 }
1740
Brian Salomonbf6b9792019-08-21 09:38:10 -04001741 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001742 int x = srcRect.fLeft;
1743 int y = srcRect.fTop;
1744 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001745 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1746 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001747 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001748 return;
1749 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001750 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001751 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001752 x = y = 0;
1753 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001754 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1755 this->colorInfo().alphaType(),
1756 info.colorSpace(),
1757 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001758 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1759 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001760 // We flip or color convert by drawing and we don't currently support drawing to
1761 // kPremul.
1762 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001763 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001764 return;
1765 }
Greg Daniel40903af2020-01-30 14:55:05 -05001766 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001767 SkRect srcRectToDraw = SkRect::Make(srcRect);
1768 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001769 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001770 texProxyView =
1771 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1772 SkBackingFit::kApprox, SkBudgeted::kNo);
1773 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001774 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001775 return;
1776 }
Brian Salomonc5243782020-04-02 12:50:34 -04001777 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001778 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1779 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001780 tempRTC = GrRenderTargetContext::Make(
1781 direct, this->colorInfo().colorType(), info.refColorSpace(),
1782 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001783 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001784 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001785 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001786 return;
1787 }
Greg Daniel40903af2020-01-30 14:55:05 -05001788 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1789 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1790 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001791 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1792 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1793 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001794 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001795 }
1796 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001797 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001798 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1799 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001800}
1801
Brian Salomon9241a6d2019-10-03 13:26:54 -04001802class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1803public:
1804 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1805 ~AsyncReadResult() override {
1806 for (int i = 0; i < fPlanes.count(); ++i) {
1807 if (!fPlanes[i].fMappedBuffer) {
1808 delete[] static_cast<const char*>(fPlanes[i].fData);
1809 } else {
1810 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1811 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1812 }
1813 }
1814 }
1815
1816 int count() const override { return fPlanes.count(); }
1817 const void* data(int i) const override { return fPlanes[i].fData; }
1818 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1819
1820 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001821 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001822 size_t rowBytes,
1823 GrClientMappedBufferManager* manager) {
1824 SkASSERT(!result.fTransferBuffer->isMapped());
1825 const void* mappedData = result.fTransferBuffer->map();
1826 if (!mappedData) {
1827 return false;
1828 }
1829 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001830 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001831 result.fPixelConverter(convertedData.get(), mappedData);
1832 this->addCpuPlane(std::move(convertedData), rowBytes);
1833 result.fTransferBuffer->unmap();
1834 } else {
1835 manager->insert(result.fTransferBuffer);
1836 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1837 }
1838 return true;
1839 }
1840
1841 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1842 SkASSERT(data);
1843 SkASSERT(rowBytes > 0);
1844 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1845 }
1846
1847private:
1848 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1849 SkASSERT(data);
1850 SkASSERT(rowBytes > 0);
1851 SkASSERT(mappedBuffer);
1852 SkASSERT(mappedBuffer->isMapped());
1853 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1854 }
1855
1856 struct Plane {
1857 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1858 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1859 const void* fData;
1860 size_t fRowBytes;
1861 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1862 sk_sp<GrGpuBuffer> fMappedBuffer;
1863 };
1864 SkSTArray<3, Plane> fPlanes;
1865 uint32_t fInboxID;
1866};
1867
Brian Salomon024bd002019-06-11 11:38:16 -04001868void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1869 ReadPixelsCallback callback,
1870 ReadPixelsContext context) {
1871 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1872 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1873
Greg Danielca9dbe22020-01-02 13:44:30 -05001874 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001875 callback(context, nullptr);
1876 return;
1877 }
1878
Brian Salomon9241a6d2019-10-03 13:26:54 -04001879 auto directContext = fContext->priv().asDirectContext();
1880 SkASSERT(directContext);
1881 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1882
Brian Salomon024bd002019-06-11 11:38:16 -04001883 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1884
1885 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001886 auto ii = SkImageInfo::Make(rect.size(), colorType,
1887 this->colorInfo().alphaType(),
1888 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001889 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001890 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1891 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1892 result->addCpuPlane(std::move(data), pm.rowBytes());
1893
Brian Salomon1d435302019-07-01 13:05:28 -04001894 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001896 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001897 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001898 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001899 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001900 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001901
Brian Salomonab32f652019-05-10 14:24:50 -04001902 struct FinishContext {
1903 ReadPixelsCallback* fClientCallback;
1904 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001905 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001906 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001907 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001908 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001909 };
1910 // Assumption is that the caller would like to flush. We could take a parameter or require an
1911 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1912 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001913 auto* finishContext = new FinishContext{callback,
1914 context,
1915 rect.size(),
1916 colorType,
1917 mappedBufferManager,
1918 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001919 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001920 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001921 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001922 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1923 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1924 context->fMappedBufferManager)) {
1925 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001926 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001927 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001928 delete context;
1929 };
1930 GrFlushInfo flushInfo;
1931 flushInfo.fFinishedContext = finishContext;
1932 flushInfo.fFinishedProc = finishCallback;
1933 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1934}
1935
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1937 sk_sp<SkColorSpace> dstColorSpace,
1938 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001939 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001940 RescaleGamma rescaleGamma,
1941 SkFilterQuality rescaleQuality,
1942 ReadPixelsCallback callback,
1943 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001944 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1945 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001946 SkASSERT(!dstSize.isZero());
1947 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1948
Brian Salomon024bd002019-06-11 11:38:16 -04001949 auto direct = fContext->priv().asDirectContext();
1950 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001951 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001952 return;
1953 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001954 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001955 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001956 return;
1957 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001958 if (this->asRenderTargetProxy()->framebufferOnly()) {
1959 callback(context, nullptr);
1960 return;
1961 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001962 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001963 callback(context, nullptr);
1964 return;
1965 }
Brian Salomon024bd002019-06-11 11:38:16 -04001966 int x = srcRect.fLeft;
1967 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001968 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001969 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001970 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001971 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001972 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001973 dstColorSpace);
1974 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001975 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001976 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001977 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001978 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001979 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001980 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001981 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001982 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001983 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001984 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001985 srcView = this->readSurfaceView();
1986 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001987 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1988 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1989 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001990 // If we can't get a texture copy of the contents then give up.
1991 callback(context, nullptr);
1992 return;
1993 }
Brian Salomonc5243782020-04-02 12:50:34 -04001994 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001995 x = y = 0;
1996 }
Brian Salomon4d036892019-07-02 15:10:58 -04001997 // We assume the caller wants kPremul. There is no way to indicate a preference.
1998 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001999 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
2000 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04002001 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002002 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2003 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002004 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2005 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002006 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002007 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002008 return;
2009 }
Greg Daniel40903af2020-01-30 14:55:05 -05002010 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
2011 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2012 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2013 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2014 SkMatrix::I(), std::move(xform));
2015 srcView = tempRTC->readSurfaceView();
2016 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002017 x = y = 0;
2018 }
2019 }
Greg Danielc594e622019-10-15 14:01:49 -04002020
Greg Daniele20fcad2020-01-08 11:52:34 -05002021 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2022 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2023 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002024 int halfW = dstSize.width()/2;
2025 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002026 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2027 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2028 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2029 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2030 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2031 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002032 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002033 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002034 return;
2035 }
2036
Brian Osman28ba5282019-10-30 14:18:07 -04002037 float baseM[20];
2038 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2039
Brian Salomon024bd002019-06-11 11:38:16 -04002040 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2041
2042 auto texMatrix = SkMatrix::MakeTrans(x, y);
2043
Brian Salomon9241a6d2019-10-03 13:26:54 -04002044 SkRect dstRectY = SkRect::Make(dstSize);
2045 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002046
Brian Salomon6aa65052020-01-28 12:16:53 -05002047 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2048 PixelTransferResult yTransfer, uTransfer, vTransfer;
2049
Brian Salomon024bd002019-06-11 11:38:16 -04002050 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2051 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002052 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002053 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002054 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002055 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002056 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002057 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2058 yPaint.addColorFragmentProcessor(std::move(yFP));
2059 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002060 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2061 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002062 if (!doSynchronousRead) {
2063 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2064 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2065 if (!yTransfer.fTransferBuffer) {
2066 callback(context, nullptr);
2067 return;
2068 }
Brian Salomon024bd002019-06-11 11:38:16 -04002069 }
2070
2071 texMatrix.preScale(2.f, 2.f);
2072 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2073 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002074 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002075 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002076 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002077 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002078 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002079 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2080 uPaint.addColorFragmentProcessor(std::move(uFP));
2081 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002082 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2083 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002084 if (!doSynchronousRead) {
2085 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2086 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2087 if (!uTransfer.fTransferBuffer) {
2088 callback(context, nullptr);
2089 return;
2090 }
Brian Salomon024bd002019-06-11 11:38:16 -04002091 }
2092
2093 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2094 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002095 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002096 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002097 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002098 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2099 this->colorInfo().alphaType(), texMatrix,
2100 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002101 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2102 vPaint.addColorFragmentProcessor(std::move(vFP));
2103 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002104 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2105 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002106 if (!doSynchronousRead) {
2107 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2108 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2109 if (!vTransfer.fTransferBuffer) {
2110 callback(context, nullptr);
2111 return;
2112 }
2113 }
2114
2115 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002116 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2117 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2118 size_t yRB = yInfo.minRowBytes();
2119 size_t uvRB = uvInfo.minRowBytes();
2120 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2121 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2122 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2123 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2124 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2125 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002126 callback(context, nullptr);
2127 return;
2128 }
2129 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002130 result->addCpuPlane(std::move(y), yRB );
2131 result->addCpuPlane(std::move(u), uvRB);
2132 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002133 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002134 return;
2135 }
2136
2137 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002138 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002139 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002140 GrClientMappedBufferManager* fMappedBufferManager;
2141 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002142 PixelTransferResult fYTransfer;
2143 PixelTransferResult fUTransfer;
2144 PixelTransferResult fVTransfer;
2145 };
2146 // Assumption is that the caller would like to flush. We could take a parameter or require an
2147 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2148 // callback to GrGpu until after the next flush that flushes our op list, though.
2149 auto* finishContext = new FinishContext{callback,
2150 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002151 direct->priv().clientMappedBufferManager(),
2152 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002153 std::move(yTransfer),
2154 std::move(uTransfer),
2155 std::move(vTransfer)};
2156 auto finishCallback = [](GrGpuFinishedContext c) {
2157 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002158 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002159 auto manager = context->fMappedBufferManager;
2160 size_t rowBytes = SkToSizeT(context->fSize.width());
2161 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2162 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002163 delete context;
2164 return;
2165 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002166 rowBytes /= 2;
2167 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2168 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2169 (*context->fClientCallback)(context->fClientContext, nullptr);
2170 delete context;
2171 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002172 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002173 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2174 (*context->fClientCallback)(context->fClientContext, nullptr);
2175 delete context;
2176 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002177 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002178 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002179 delete context;
2180 };
2181 GrFlushInfo flushInfo;
2182 flushInfo.fFinishedContext = finishContext;
2183 flushInfo.fFinishedProc = finishCallback;
2184 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002185}
2186
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002187GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2188 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002189 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002190 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002191 return GrSemaphoresSubmitted::kNo;
2192 }
robertphillips8c523e02016-07-26 07:41:00 -07002193 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002194 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002195
Greg Daniel55f040b2020-02-13 15:38:32 +00002196 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002197}
2198
Greg Danielc64ee462017-06-15 16:59:49 -04002199bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002200 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002201 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002202 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002203 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002204 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002205
2206 AutoCheckFlush acf(this->drawingManager());
2207
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002208 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002209 return false;
2210 }
2211
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002212 auto direct = fContext->priv().asDirectContext();
2213 if (!direct) {
2214 return false;
2215 }
2216
2217 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002218
Greg Daniel301015c2019-11-18 14:06:46 -05002219 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2220 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002221 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002222 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002223 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2224 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002225 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002226 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2227 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002228 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002229}
joshualitt33a5fce2015-11-18 13:28:51 -08002230
Brian Osman11052242016-10-27 14:47:55 -04002231void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002232 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002233 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002234 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002235 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002236 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002237 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002238 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002239 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002240 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2241
Michael Ludwig2686d692020-04-17 20:21:37 +00002242 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002243
Robert Phillips27927a52018-08-20 13:18:12 -04002244 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002245}
2246
2247void GrRenderTargetContext::drawShape(const GrClip& clip,
2248 GrPaint&& paint,
2249 GrAA aa,
2250 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002251 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002252 ASSERT_SINGLE_OWNER
2253 RETURN_IF_ABANDONED
2254 SkDEBUGCODE(this->validate();)
2255 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2256
Brian Salomon2fad74a2017-12-20 13:28:55 -05002257 if (shape.isEmpty()) {
2258 if (shape.inverseFilled()) {
2259 this->drawPaint(clip, std::move(paint), viewMatrix);
2260 }
2261 return;
robertphillipsea461502015-05-26 11:38:03 -07002262 }
2263
Robert Phillips72152832017-01-25 17:31:35 -05002264 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002265
Brian Salomon2fad74a2017-12-20 13:28:55 -05002266 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002267 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002268 SkRRect rrect;
2269 // We can ignore the starting point and direction since there is no path effect.
2270 bool inverted;
2271 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2272 if (rrect.isRect()) {
2273 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2274 &shape.style());
2275 return;
2276 } else if (rrect.isOval()) {
2277 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002278 return;
2279 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002280 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2281 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002282 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2283 viewMatrix.rectStaysRect()) {
2284 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2285 // the matrix to all the points individually rather than just to the rect
2286 SkRect rects[2];
2287 if (shape.asNestedRects(rects)) {
2288 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002289 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002290 fContext, std::move(paint), viewMatrix, rects);
2291 if (op) {
2292 this->addDrawOp(clip, std::move(op));
2293 }
2294 // Returning here indicates that there is nothing to draw in this case.
2295 return;
2296 }
robertphillipsea461502015-05-26 11:38:03 -07002297 }
2298 }
robertphillips4bc31812016-03-01 12:22:49 -08002299
Brian Salomon2fad74a2017-12-20 13:28:55 -05002300 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002301}
2302
Chris Daltonbbfd5162017-11-07 13:35:22 -07002303bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002304 const GrUserStencilSettings* ss,
2305 SkRegion::Op op,
2306 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002307 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002308 const SkMatrix& viewMatrix,
2309 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002310 ASSERT_SINGLE_OWNER_PRIV
2311 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002312 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002313 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2314 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002315
2316 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002317 GrPaint paint;
2318 paint.setCoverageSetOpXPFactory(op, invert);
2319 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2320 SkRect::MakeIWH(fRenderTargetContext->width(),
2321 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002322 return true;
2323 }
2324
Robert Phillips72152832017-01-25 17:31:35 -05002325 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002326
2327 // An Assumption here is that path renderer would use some form of tweaking
2328 // the src color (either the input alpha or in the frag shader) to implement
2329 // aa. If we have some future driver-mojo path AA that can do the right
2330 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002331 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002332 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002333
Chris Daltondb91c6e2017-09-08 16:25:08 -06002334 SkIRect clipConservativeBounds;
2335 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2336 &clipConservativeBounds, nullptr);
2337
Michael Ludwig2686d692020-04-17 20:21:37 +00002338 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002339 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002340 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002341 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002342 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002343 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002344 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002345 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002346 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2347 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002348 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002349
2350 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002351 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002352 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002353 if (!pr) {
2354 return false;
2355 }
2356
2357 GrPaint paint;
2358 paint.setCoverageSetOpXPFactory(op, invert);
2359
Brian Salomonf3569f02017-10-24 12:52:33 -04002360 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2361 std::move(paint),
2362 ss,
2363 fRenderTargetContext,
2364 &clip,
2365 &clipConservativeBounds,
2366 &viewMatrix,
2367 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002368 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002369 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002370 pr->drawPath(args);
2371 return true;
2372}
2373
Brian Osman11052242016-10-27 14:47:55 -04002374SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002375 ASSERT_SINGLE_OWNER_PRIV
2376
Robert Phillips6a6de562019-02-15 15:19:15 -05002377 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002378 return SkBudgeted::kNo;
2379 }
2380
Brian Osman11052242016-10-27 14:47:55 -04002381 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002382
Greg Daniel3912a4b2020-01-14 09:56:04 -05002383 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002384}
2385
Brian Salomon2fad74a2017-12-20 13:28:55 -05002386void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2387 GrPaint&& paint,
2388 GrAA aa,
2389 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002390 const GrStyledShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002391 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002392 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002393 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2394
Jim Van Verthf86073a2018-10-02 13:05:38 -04002395 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2396 return;
2397 }
2398
Chris Daltondb91c6e2017-09-08 16:25:08 -06002399 SkIRect clipConservativeBounds;
2400 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2401
Michael Ludwig2686d692020-04-17 20:21:37 +00002402 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002403 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002404
robertphillips68737822015-10-29 12:12:21 -07002405 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002406 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002407 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002408 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002409 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002410 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002411 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002412 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002413
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002414 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002415 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002416 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002417 return;
2418 }
2419
Chris Dalton6ce447a2019-06-23 18:07:38 -06002420 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002421
2422 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2423 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2424 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2425
Brian Salomon2fad74a2017-12-20 13:28:55 -05002426 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002427 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002428 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2429 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002430 return;
2431 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002432 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002433 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002434 }
2435 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002436 if (canDrawArgs.fShape->style().applies()) {
2437 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2438 styleScale);
2439 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002440 return;
2441 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002442 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002443 // This time, allow SW renderer
2444 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2445 } else {
2446 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002447#if GR_PATH_RENDERER_SPEW
2448 SkDebugf("falling back to: %s\n", pr->name());
2449#endif
bsalomon6663acf2016-05-10 09:14:17 -07002450 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002451 }
robertphillipsea461502015-05-26 11:38:03 -07002452
bsalomon8acedde2016-06-24 10:42:16 -07002453 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002454#ifdef SK_DEBUG
2455 SkDebugf("Unable to find path renderer compatible with path.\n");
2456#endif
2457 return;
2458 }
2459
Robert Phillips256c37b2017-03-01 14:32:46 -05002460 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002461 std::move(paint),
2462 &GrUserStencilSettings::kUnused,
2463 this,
2464 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002465 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002466 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002467 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002468 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002469 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002470 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002471}
2472
Brian Salomon467921e2017-03-06 16:17:12 -05002473static void op_bounds(SkRect* bounds, const GrOp* op) {
2474 *bounds = op->bounds();
2475 if (op->hasZeroArea()) {
2476 if (op->hasAABloat()) {
2477 bounds->outset(0.5f, 0.5f);
2478 } else {
2479 // We don't know which way the particular GPU will snap lines or points at integer
2480 // coords. So we ensure that the bounds is large enough for either snap.
2481 SkRect before = *bounds;
2482 bounds->roundOut(bounds);
2483 if (bounds->fLeft == before.fLeft) {
2484 bounds->fLeft -= 1;
2485 }
2486 if (bounds->fTop == before.fTop) {
2487 bounds->fTop -= 1;
2488 }
2489 if (bounds->fRight == before.fRight) {
2490 bounds->fRight += 1;
2491 }
2492 if (bounds->fBottom == before.fBottom) {
2493 bounds->fBottom += 1;
2494 }
2495 }
2496 }
2497}
2498
Chris Dalton08755122019-08-05 16:13:47 -06002499void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002500 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002501 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2502}
2503
Brian Salomon348a0372018-10-31 10:42:18 -04002504void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2505 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002506 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002507 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002508 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002509 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002510 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002511 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002512 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002513 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002514
Brian Salomon467921e2017-03-06 16:17:12 -05002515 // Setup clip
2516 SkRect bounds;
2517 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002518 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002519 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002520 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002521 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002522
Chris Dalton9acf6822019-11-12 11:52:40 -07002523 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002524 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002525 }
2526
Chris Dalton9acf6822019-11-12 11:52:40 -07002527 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002528 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002529 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002530 }
2531
Chris Dalton9acf6822019-11-12 11:52:40 -07002532 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2533 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2534
2535 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2536 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2537 // that uses a stencil test when the stencil buffer is multisampled.)
2538 bool hasMixedSampledCoverage = (
2539 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002540 SkASSERT(!hasMixedSampledCoverage ||
2541 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002542
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002543 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002544 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002545 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2546
Greg Daniel524e28b2019-11-01 11:48:53 -04002547 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002548 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002549 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002550 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002551 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002552 }
2553 }
2554
2555 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002556 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002557 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002558 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002559 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002560 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002561 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002562}
2563
Greg Daniel524e28b2019-11-01 11:48:53 -04002564bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2565 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002566 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2567 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2568 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002569 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002570 return false;
2571 }
2572
Greg Daniel3912a4b2020-01-14 09:56:04 -05002573 if (this->caps()->textureBarrierSupport() &&
2574 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002575 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002576 // The render target is a texture, so we can read from it directly in the shader. The XP
2577 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002578 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002579 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002580 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002581 }
2582 }
2583
Greg Daniel3912a4b2020-01-14 09:56:04 -05002584 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002585
Eric Karl74480882017-04-03 14:49:05 -07002586 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002587 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002588 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002589 SkRect opBounds = op.bounds();
2590 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2591 // 0.5 pixels.
2592 if (op.hasAABloat() || op.hasZeroArea()) {
2593 opBounds.outset(0.5f, 0.5f);
2594 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2595 // performance we may ignore the clip when the draw is entirely inside the clip is float
2596 // space but will hit pixels just outside the clip when actually rasterizing.
2597 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002598 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002599 }
2600 SkIRect opIBounds;
2601 opBounds.roundOut(&opIBounds);
2602 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002603#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002604 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002605#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002606 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002607 }
2608
Greg Daniel3155f7f2020-01-16 16:54:26 -05002609 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002610 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2611 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002612 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002613 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002614
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002615 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002616 copyRect = clippedRect;
2617 }
Brian Salomon467921e2017-03-06 16:17:12 -05002618
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002619 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002620 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002621 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002622 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002623 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002624 } else {
Eric Karl74480882017-04-03 14:49:05 -07002625 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002626 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002627 }
Brian Salomonc5243782020-04-02 12:50:34 -04002628 auto copy =
2629 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2630 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2631 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002632
Brian Salomonc5243782020-04-02 12:50:34 -04002633 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002634 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002635 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002636}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002637
Greg Daniel573312e2020-02-07 17:22:35 -05002638bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002639 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002640 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002641 SkIRect clippedSrcRect;
2642 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002643 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2644 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002645 return false;
2646 }
2647
2648 GrPaint paint;
2649 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002650
Greg Danield2ccbb52020-02-05 10:45:39 -05002651 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002652 if (!fp) {
2653 return false;
2654 }
2655 paint.addColorFragmentProcessor(std::move(fp));
2656
2657 this->fillRectToRect(
2658 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2659 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2660 clippedSrcRect.height()),
2661 SkRect::Make(clippedSrcRect));
2662 return true;
2663}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002664
2665void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2666 SkASSERT(&task == fOpsTask.get());
2667 fOpsTask.reset();
2668}