blob: 4c8ba6a5476f936e04ee880f91c509e95f1a4a20 [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 Salomon8afde5f2020-04-01 16:22:00 -0400384 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
385 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500386
Brian Salomonf18b1d82017-10-27 11:30:49 -0400387 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700388 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700389}
390
robertphillips2e1e51f2015-10-15 08:01:48 -0700391#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500392void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400393 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400394 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700395 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700396}
397#endif
398
Brian Osman11052242016-10-27 14:47:55 -0400399GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800400 ASSERT_SINGLE_OWNER
Brian Salomon198393b2020-04-20 15:38:21 -0400401 if (fOpsTask) {
402 fOpsTask->setClosedObserver(nullptr);
403 }
robertphillipsa106c622015-10-16 09:07:06 -0700404}
405
Chris Dalton7d6748e2019-03-13 00:34:52 -0600406inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600407 if (GrAA::kNo == aa) {
408 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
409 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600410 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600411 return GrAAType::kMSAA;
412 }
413 return GrAAType::kNone;
414 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600415 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600416}
417
Greg Daniele252f082017-10-23 16:05:23 -0400418GrMipMapped GrRenderTargetContext::mipMapped() const {
419 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
420 return proxy->mipMapped();
421 }
422 return GrMipMapped::kNo;
423}
424
Greg Danielf41b2bd2019-08-22 16:19:24 -0400425GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800426 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700427 SkDEBUGCODE(this->validate();)
428
Brian Salomon198393b2020-04-20 15:38:21 -0400429 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600430 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400431 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600432 if (fOpsTask && fNumStencilSamples > 0) {
433 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600434 fOpsTask->setMustPreserveStencil();
435 // Reload the stencil buffer content at the beginning of newOpsTask.
436 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
437 // values?
438 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
439 }
Brian Salomon198393b2020-04-20 15:38:21 -0400440 newOpsTask->setClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600441 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700442 }
Brian Salomon198393b2020-04-20 15:38:21 -0400443 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400444 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700445}
446
Brian Osman449b1152020-04-15 16:43:00 -0400447void GrRenderTargetContext::drawGlyphRunList(const GrClip& clip,
448 const SkMatrixProvider& matrixProvider,
449 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800450 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700451 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700452 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400453 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700454
Greg Danielbe7fc462019-01-03 16:40:42 -0500455 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
456 // secondary command buffers because it would require stopping and starting a render pass which
457 // we don't have access to.
458 if (this->wrapsVkSecondaryCB()) {
459 return;
460 }
461
Herb Derby26cbe512018-05-24 14:39:01 -0400462 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400463 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400464 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700465}
466
Brian Osman11052242016-10-27 14:47:55 -0400467void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800468 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700469 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700470 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400471 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700472
Robert Phillips72152832017-01-25 17:31:35 -0500473 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400474
Greg Danielf41b2bd2019-08-22 16:19:24 -0400475 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700476}
477
Brian Osman11052242016-10-27 14:47:55 -0400478void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500479 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700480 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800481 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700482 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700483 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400484 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700485
Robert Phillips72152832017-01-25 17:31:35 -0500486 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700487 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
488 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700489}
robertphillips9199a9f2016-07-13 07:48:43 -0700490
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500491void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
492 const SkPMColor4f& color,
493 CanClearFullscreen canClearFullscreen) {
494 ASSERT_SINGLE_OWNER_PRIV
495 RETURN_IF_ABANDONED_PRIV
496 SkDEBUGCODE(fRenderTargetContext->validate();)
497 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
498 fRenderTargetContext->fContext);
499
500 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
501 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
502}
503
504static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
505 paint->setColor4f(color);
506 if (color.isOpaque()) {
507 // Can just rely on the src-over blend mode to do the right thing
508 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
509 } else {
510 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
511 // were src blended
512 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
513 }
514}
515
516void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
517 const SkPMColor4f& color,
518 CanClearFullscreen canClearFullscreen) {
519 bool isFull = false;
520 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400521 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
522 // only clear the entire target if we knew it had not been cleared before. As
523 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500524 isFull = !clip.scissorEnabled() ||
525 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400526 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500527 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
528 }
529
530 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400531 GrOpsTask* opsTask = this->getOpsTask();
532 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500533 !this->caps()->performColorClearsAsDraws()) {
534 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400535 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 return;
537 } else {
538 // Will use an op for the clear, reset the load op to discard since the op will
539 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400540 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500541 }
542
543 // Must add an op to the list (either because we couldn't use a load op, or because the
544 // clear load op isn't supported)
545 if (this->caps()->performColorClearsAsDraws()) {
546 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
547 GrPaint paint;
548 clear_to_grpaint(color, &paint);
549 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400550 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
551 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500552 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600553 this->addOp(GrClearOp::Make(
554 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 }
556 } else {
557 if (this->caps()->performPartialClearsAsDraws()) {
558 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500559 GrPaint paint;
560 clear_to_grpaint(color, &paint);
561
Michael Ludwig64b28a72019-05-28 12:02:00 -0400562 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400563 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
564 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500565 } else {
566 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
567 this->asSurfaceProxy()));
568 // This version of the clear op factory can return null if the clip doesn't intersect
569 // with the surface proxy's boundary
570 if (!op) {
571 return;
572 }
Chris Dalton08755122019-08-05 16:13:47 -0600573 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500574 }
575 }
576}
577
Brian Osman11052242016-10-27 14:47:55 -0400578void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500579 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400580 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000581 // Start with the render target, since that is the maximum content we could possibly fill.
582 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500583 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400584 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000585 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
586 // inverse view matrix.
587 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
588 } else {
589 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
590 SkMatrix localMatrix;
591 if (!viewMatrix.invert(&localMatrix)) {
592 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400593 }
Michael Ludwig61328202019-06-19 14:48:58 +0000594 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
595 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700596 }
robertphillipsea461502015-05-26 11:38:03 -0700597}
598
Michael Ludwig61328202019-06-19 14:48:58 +0000599enum class GrRenderTargetContext::QuadOptimization {
600 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
601 kDiscarded,
602 // The rect to draw was converted to some other op and appended to the oplist, so no additional
603 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
604 // a constColor is provided.
605 kSubmitted,
606 // The clip was folded into the device quad, with updated edge flags and local coords, and
607 // caller is responsible for adding an appropriate op.
608 kClipApplied,
609 // No change to clip, but quad updated to better fit clip/render target, and caller is
610 // responsible for adding an appropriate op.
611 kCropped
612};
Michael Ludwig61a16512019-01-15 11:15:13 -0500613
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400614static bool make_vertex_finite(float* value) {
615 if (SkScalarIsNaN(*value)) {
616 return false;
617 }
618
619 if (!SkScalarIsFinite(*value)) {
620 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
621 // left when calculating crops.
622 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
623 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
624 }
625
626 return true;
627}
628
Michael Ludwig61328202019-06-19 14:48:58 +0000629GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400630 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500631 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000632 // Optimization requirements:
633 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500634 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
635 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
636 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
637 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
638 // 5. kCropped in all other scenarios (although a crop may be a no-op)
639
640 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
641 // better to just keep the old flags instead of introducing mixed edge flags.
642 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000643
Michael Ludwige08b4432019-06-19 18:00:48 -0400644 SkRect rtRect;
645 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400646 // Must use size at which the rendertarget will ultimately be allocated so that stencil
647 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500648 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400649 } else {
650 // Use the logical size of the render target, which allows for "fullscreen" clears even if
651 // the render target has an approximate backing fit
652 rtRect = SkRect::MakeWH(this->width(), this->height());
653 }
654
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500655 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000656 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400657 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
658 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500659 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400660 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500661 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
662 !make_vertex_finite(quad->fDevice.ys() + i) ||
663 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400664 // Discard if we see a nan
665 return QuadOptimization::kDiscarded;
666 }
667 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500668 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400669 }
670 } else {
671 // CropToRect requires the quads to be finite. If they are not finite and we have local
672 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500673 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400674 return QuadOptimization::kDiscarded;
675 }
Brian Salomon7694b902019-06-18 21:00:21 +0000676 }
677
Michael Ludwig61328202019-06-19 14:48:58 +0000678 // If the quad is entirely off screen, it doesn't matter what the clip does
679 if (!rtRect.intersects(drawBounds)) {
680 return QuadOptimization::kDiscarded;
681 }
Brian Salomon7694b902019-06-18 21:00:21 +0000682
Michael Ludwig61328202019-06-19 14:48:58 +0000683 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
684 // the render target).
685 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400686 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
687 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
688 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
689 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000690 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000691 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000692 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
693 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000694 }
Brian Salomon7694b902019-06-18 21:00:21 +0000695 }
696
Michael Ludwig61328202019-06-19 14:48:58 +0000697 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
698 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400699 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000700 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
701 // of the render target and the clip rect)
702 SkRect clipBounds = rtRect;
703 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
704 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000705 }
706
Michael Ludwig61328202019-06-19 14:48:58 +0000707 if (clipRRect.isRect()) {
708 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500709 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700710 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500711 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000712 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500713 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000714 if (drawBounds.contains(rtRect)) {
715 // Fullscreen clear
716 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
717 return QuadOptimization::kSubmitted;
718 } else if (GrClip::IsPixelAligned(drawBounds) &&
719 drawBounds.width() > 256 && drawBounds.height() > 256) {
720 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
721 SkIRect scissorRect;
722 drawBounds.round(&scissorRect);
723 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
724 return QuadOptimization::kSubmitted;
725 }
726 }
727
728 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000729 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500730 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000731 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
732 // properly reflect the smooth edge of the clip.
733 *aa = GrAA::kYes;
734 }
735 // We intentionally do not downgrade AA here because we don't know if we need to
736 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
737 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
738
739 // deviceQuad is exactly the intersection of original quad and clip, so it can be
740 // drawn with no clip (submitted by caller)
741 return QuadOptimization::kClipApplied;
742 } else {
743 // The quads have been updated to better fit the clip bounds, but can't get rid of
744 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500745 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000746 return QuadOptimization::kCropped;
747 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700748 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000749 // Rounded corners and constant filled color (limit ourselves to solid colors because
750 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500751 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
752 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
753 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000754 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
755 // we can draw the rrect directly and ignore the edge flags
756 GrPaint paint;
757 clear_to_grpaint(*constColor, &paint);
758 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
759 clipRRect, GrStyle::SimpleFill());
760 return QuadOptimization::kSubmitted;
761 } else {
762 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500763 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000764 return QuadOptimization::kCropped;
765 }
Brian Salomon7694b902019-06-18 21:00:21 +0000766 }
Brian Salomon7694b902019-06-18 21:00:21 +0000767 }
768
Michael Ludwig61328202019-06-19 14:48:58 +0000769 // Crop the quad to the conservative bounds of the clip.
770 SkIRect clipDevBounds;
771 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
772 SkRect clipBounds = SkRect::Make(clipDevBounds);
773
774 // One final check for discarding, since we may have gone here directly due to a complex clip
775 if (!clipBounds.intersects(drawBounds)) {
776 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000777 }
778
Michael Ludwig61328202019-06-19 14:48:58 +0000779 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
780 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500781 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
782 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000783
784 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000785}
786
Michael Ludwig61328202019-06-19 14:48:58 +0000787void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500788 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500789 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500790 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400791 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000792 ASSERT_SINGLE_OWNER
793 RETURN_IF_ABANDONED
794 SkDEBUGCODE(this->validate();)
795 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500796
Michael Ludwig61328202019-06-19 14:48:58 +0000797 AutoCheckFlush acf(this->drawingManager());
798
799 SkPMColor4f* constColor = nullptr;
800 SkPMColor4f paintColor;
801 if (!ss && !paint.numCoverageFragmentProcessors() &&
802 paint.isConstantBlendedColor(&paintColor)) {
803 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
804 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500805 }
806
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500807 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000808 if (opt >= QuadOptimization::kClipApplied) {
809 // These optimizations require caller to add an op themselves
810 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
811 : clip;
812 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
813 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500814 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
815 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700816 }
Michael Ludwig61328202019-06-19 14:48:58 +0000817 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800818}
819
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000820void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400821 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500822 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000823 sk_sp<GrColorSpaceXform> textureXform,
824 GrSamplerState::Filter filter,
825 const SkPMColor4f& color,
826 SkBlendMode blendMode,
827 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500828 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000829 const SkRect* domain) {
830 ASSERT_SINGLE_OWNER
831 RETURN_IF_ABANDONED
832 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400833 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000834 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
835
836 AutoCheckFlush acf(this->drawingManager());
837
838 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
839 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500840 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000841
842 SkASSERT(opt != QuadOptimization::kSubmitted);
843 if (opt != QuadOptimization::kDiscarded) {
844 // And the texture op if not discarded
845 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
846 : clip;
847 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400848 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400849 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
850 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000851 // Use the provided domain, although hypothetically we could detect that the cropped local
852 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500853 this->addDrawOp(finalClip,
854 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
855 std::move(textureXform), filter, color, saturate,
856 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000857 }
858}
859
Brian Osman11052242016-10-27 14:47:55 -0400860void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500861 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500862 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400863 const SkMatrix& viewMatrix,
864 const SkRect& rect,
865 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700866 if (!style) {
867 style = &GrStyle::SimpleFill();
868 }
joshualitt1de610a2016-01-06 08:26:09 -0800869 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700870 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700871 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400872 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700873
bsalomon6663acf2016-05-10 09:14:17 -0700874 // Path effects should've been devolved to a path in SkGpuDevice
875 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700876
Robert Phillips72152832017-01-25 17:31:35 -0500877 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700878
bsalomon6663acf2016-05-10 09:14:17 -0700879 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400880 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000881 // Fills the rect, using rect as its own local coordinates
882 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500883 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700884 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
885 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
886 if ((!rect.width() || !rect.height()) &&
887 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
888 SkScalar r = stroke.getWidth() / 2;
Michael Ludwig2686d692020-04-17 20:21:37 +0000889 // TODO: Move these stroke->fill fallbacks to GrStyledShape?
bsalomona7d85ba2016-07-06 11:54:59 -0700890 switch (stroke.getJoin()) {
891 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500892 this->drawRect(
893 clip, std::move(paint), aa, viewMatrix,
894 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
895 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700896 return;
897 case SkPaint::kRound_Join:
898 // Raster draws nothing when both dimensions are empty.
899 if (rect.width() || rect.height()){
900 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500901 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
902 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700903 return;
904 }
905 case SkPaint::kBevel_Join:
906 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500907 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700908 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
909 &GrStyle::SimpleFill());
910 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500911 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700912 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
913 &GrStyle::SimpleFill());
914 }
915 return;
916 }
917 }
robertphillips44302392016-07-08 14:43:03 -0700918
Brian Salomonbaaf4392017-06-15 09:59:23 -0400919 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700920
Chris Dalton7d6748e2019-03-13 00:34:52 -0600921 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500922 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
923 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
924 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500925 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400926 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700927 return;
robertphillips4bc31812016-03-01 12:22:49 -0800928 }
robertphillips4bc31812016-03-01 12:22:49 -0800929 }
Mike Klein16885072018-12-11 09:54:31 -0500930 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000931 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
932 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700933}
934
Michael Ludwig69858532018-11-28 15:34:34 -0500935void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
936 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
937 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600938 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500939
940 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
941 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500942}
943
Robert Phillipsec2249f2016-11-09 08:54:35 -0500944int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500945 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400946 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500947}
948
Greg Danielf41b2bd2019-08-22 16:19:24 -0400949GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600950 ) const {
951#if GR_TEST_UTILS
952 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400953 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600954 }
955#endif
956 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
957 // would normally be overwritten. The one exception is if the render target context is marked as
958 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
959 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
960 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
961 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400962 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600963}
964
Chris Dalton858cf232019-10-14 16:20:00 -0600965void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600966 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600967 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600968 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600969
Chris Daltoneffee202019-07-01 22:28:03 -0600970 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600971 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500972 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600973 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400974 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600975 }
976 SkASSERT(numRequiredSamples > 0);
977
978 if (numRequiredSamples > fNumStencilSamples) {
979 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500980 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600981 }
Chris Dalton6b982802019-06-27 13:53:46 -0600982
Chris Dalton674f77a2019-09-30 20:49:39 -0600983 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600984 if (this->caps()->performStencilClearsAsDraws()) {
985 // There is a driver bug with clearing stencil. We must use an op to manually clear the
986 // stencil buffer before the op that required 'setNeedsStencil'.
987 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
988 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600989 this->getOpsTask()->setInitialStencilContent(
990 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600991 }
992 }
993}
994
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000995void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700996 ASSERT_SINGLE_OWNER_PRIV
997 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400998 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400999 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
1000 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -07001001
Robert Phillips72152832017-01-25 17:31:35 -05001002 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001003
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001004 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
1005}
1006
1007void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001008 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1009
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001010 if (this->caps()->performStencilClearsAsDraws()) {
1011 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
1012 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
1013
1014 // Configure the paint to have no impact on the color buffer
1015 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001016 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001017 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1018 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001019 } else {
1020 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001021 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001022 if (!op) {
1023 return;
1024 }
Chris Dalton08755122019-08-05 16:13:47 -06001025 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001026 }
robertphillips976f5f02016-06-03 10:59:20 -07001027}
1028
Chris Daltonbbfd5162017-11-07 13:35:22 -07001029void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001030 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001031 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001032 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001033 ASSERT_SINGLE_OWNER_PRIV
1034 RETURN_IF_ABANDONED_PRIV
1035 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001036 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1037 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001038
Brian Salomon467921e2017-03-06 16:17:12 -05001039 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1040 SkASSERT(path);
1041 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1042
1043 // FIXME: Use path bounds instead of this WAR once
1044 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1045 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1046
1047 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001048 GrAppliedHardClip appliedClip;
1049 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1050 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001051 return;
1052 }
1053
Robert Phillips7c525e62018-06-12 10:11:12 -04001054 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1055 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001056 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001057 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001058 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001059 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001060 if (!op) {
1061 return;
1062 }
Brian Salomon97180af2017-03-14 13:42:58 -04001063 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001064
Chris Daltoneffee202019-07-01 22:28:03 -06001065 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001066 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001067}
1068
Michael Ludwig379e4962019-12-06 13:21:26 -05001069void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1070 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001071 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001072 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1073 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001074 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001075 ASSERT_SINGLE_OWNER
1076 RETURN_IF_ABANDONED
1077 SkDEBUGCODE(this->validate();)
1078 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001079
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001080 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1081 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1082 AutoCheckFlush acf(this->drawingManager());
1083 GrAAType aaType = this->chooseAAType(aa);
1084 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1085 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1086 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001087 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1088 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001089}
1090
Brian Osman11052242016-10-27 14:47:55 -04001091void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001092 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001093 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001094 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001095 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001096 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001097 ASSERT_SINGLE_OWNER
1098 RETURN_IF_ABANDONED
1099 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001100 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001101
1102 AutoCheckFlush acf(this->drawingManager());
1103
1104 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001105 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001106 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001107 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001108 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001109 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001110 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001111}
1112
1113///////////////////////////////////////////////////////////////////////////////
1114
Brian Osman4d92b892019-03-24 00:53:23 +00001115void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1116 GrPaint&& paint,
1117 const SkMatrix& viewMatrix,
1118 int spriteCount,
1119 const SkRSXform xform[],
1120 const SkRect texRect[],
1121 const SkColor colors[]) {
1122 ASSERT_SINGLE_OWNER
1123 RETURN_IF_ABANDONED
1124 SkDEBUGCODE(this->validate();)
1125 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1126
1127 AutoCheckFlush acf(this->drawingManager());
1128
1129 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1130 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1131 aaType, spriteCount, xform, texRect, colors);
1132 this->addDrawOp(clip, std::move(op));
1133}
1134
1135///////////////////////////////////////////////////////////////////////////////
1136
Brian Osman11052242016-10-27 14:47:55 -04001137void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001138 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001139 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001140 const SkMatrix& viewMatrix,
1141 const SkRRect& rrect,
1142 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001143 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001144 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001145 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001146 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001147
1148 const SkStrokeRec& stroke = style.strokeRec();
1149 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001150 return;
1151 }
1152
bsalomon7f0d9f32016-08-15 14:49:10 -07001153 GrNoClip noclip;
1154 const GrClip* clip = &origClip;
1155#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1156 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001157 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001158 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1159 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001160 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1161 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001162 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001163 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1164 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001165 clip = &noclip;
1166 }
1167#endif
bsalomon6663acf2016-05-10 09:14:17 -07001168 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001169
Robert Phillips72152832017-01-25 17:31:35 -05001170 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001171
Chris Dalton7d6748e2019-03-13 00:34:52 -06001172 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001173
Chris Dalton0dffbab2019-03-27 13:08:50 -06001174 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001175 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1176 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1177 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1178 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1179 // to avoid perf regressions on some platforms.
1180 assert_alive(paint);
1181 op = GrOvalOpFactory::MakeCircularRRectOp(
1182 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1183 }
1184 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001185 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001186 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001187 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001188 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001189 assert_alive(paint);
1190 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001191 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001192 }
1193 if (op) {
1194 this->addDrawOp(*clip, std::move(op));
1195 return;
robertphillipsea461502015-05-26 11:38:03 -07001196 }
robertphillipsb56f9272016-02-25 11:03:52 -08001197
Mike Klein16885072018-12-11 09:54:31 -05001198 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001199 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001200 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001201}
1202
Jim Van Verthc5903412016-11-17 15:27:09 -05001203///////////////////////////////////////////////////////////////////////////////
1204
Jim Van Verth3af1af92017-05-18 15:06:54 -04001205static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1206 SkPoint3 result;
1207 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1208 result.fZ = pt.fZ;
1209 return result;
1210}
1211
1212bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001213 const SkMatrix& viewMatrix,
1214 const SkPath& path,
1215 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001216 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001217 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001218 return true;
1219 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001220 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001221 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001222
1223 // check z plane
1224 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1225 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1226 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1227 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1228 return false;
1229 }
1230
1231 SkRRect rrect;
1232 SkRect rect;
1233 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001234 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001235 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1236 if (!isRRect &&
1237 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1238 rect.width() > SK_ScalarNearlyZero) {
1239 rrect.setOval(rect);
1240 isRRect = true;
1241 }
1242 if (!isRRect && path.isRect(&rect)) {
1243 rrect.setRect(rect);
1244 isRRect = true;
1245 }
1246
1247 if (!isRRect) {
1248 return false;
1249 }
1250
Jim Van Verthc5903412016-11-17 15:27:09 -05001251 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001252 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001253 }
1254
Robert Phillips72152832017-01-25 17:31:35 -05001255 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001256
Jim Van Verth3af1af92017-05-18 15:06:54 -04001257 // transform light
1258 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1259
1260 // 1/scale
1261 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001262 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001263 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1264 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1265
1266 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001267 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1268
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001269 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001270 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1271 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1272 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001273
1274 // Outset the shadow rrect to the border of the penumbra
1275 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1276 SkRRect ambientRRect;
1277 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1278 // If the rrect was an oval then its outset will also be one.
1279 // We set it explicitly to avoid errors.
1280 if (rrect.isOval()) {
1281 ambientRRect = SkRRect::MakeOval(outsetRect);
1282 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001283 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001284 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1285 }
1286
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001287 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001288 if (transparent) {
1289 // set a large inset to force a fill
1290 devSpaceInsetWidth = ambientRRect.width();
1291 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001292
Robert Phillips7c525e62018-06-12 10:11:12 -04001293 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1294 ambientColor,
1295 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001296 ambientRRect,
1297 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001298 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001299 if (op) {
1300 this->addDrawOp(clip, std::move(op));
1301 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001302 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001303
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001304 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001305 SkScalar devSpaceSpotBlur;
1306 SkScalar spotScale;
1307 SkVector spotOffset;
1308 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1309 devLightPos.fZ, rec.fLightRadius,
1310 &devSpaceSpotBlur, &spotScale, &spotOffset);
1311 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001312 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1313
Jim Van Verth3af1af92017-05-18 15:06:54 -04001314 // Adjust translate for the effect of the scale.
1315 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1316 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1317 // This offset is in dev space, need to transform it into source space.
1318 SkMatrix ctmInverse;
1319 if (viewMatrix.invert(&ctmInverse)) {
1320 ctmInverse.mapPoints(&spotOffset, 1);
1321 } else {
1322 // Since the matrix is a similarity, this should never happen, but just in case...
1323 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1324 SkASSERT(false);
1325 }
1326
1327 // Compute the transformed shadow rrect
1328 SkRRect spotShadowRRect;
1329 SkMatrix shadowTransform;
1330 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1331 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001332 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001333
1334 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001335 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001336 SkScalar insetWidth = blurOutset;
1337 if (transparent) {
1338 // If transparent, just do a fill
1339 insetWidth += spotShadowRRect.width();
1340 } else {
1341 // For shadows, instead of using a stroke we specify an inset from the penumbra
1342 // border. We want to extend this inset area so that it meets up with the caster
1343 // geometry. The inset geometry will by default already be inset by the blur width.
1344 //
1345 // We compare the min and max corners inset by the radius between the original
1346 // rrect and the shadow rrect. The distance between the two plus the difference
1347 // between the scaled radius and the original radius gives the distance from the
1348 // transformed shadow shape to the original shape in that corner. The max
1349 // of these gives the maximum distance we need to cover.
1350 //
1351 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1352 // that to get the full insetWidth.
1353 SkScalar maxOffset;
1354 if (rrect.isRect()) {
1355 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001356 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001357 rrect.rect().fLeft),
1358 SkTAbs(spotShadowRRect.rect().fTop -
1359 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001360 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001361 rrect.rect().fRight),
1362 SkTAbs(spotShadowRRect.rect().fBottom -
1363 rrect.rect().fBottom)));
1364 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001365 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1367 rrect.rect().fLeft + dr,
1368 spotShadowRRect.rect().fTop -
1369 rrect.rect().fTop + dr);
1370 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1371 rrect.rect().fRight - dr,
1372 spotShadowRRect.rect().fBottom -
1373 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001374 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001375 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001376 }
Brian Osman788b9162020-02-07 10:36:46 -05001377 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001378 }
1379
1380 // Outset the shadow rrect to the border of the penumbra
1381 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1382 if (spotShadowRRect.isOval()) {
1383 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1384 } else {
1385 SkScalar outsetRad = spotRadius + blurOutset;
1386 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1387 }
1388
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001389 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001390
Robert Phillips7c525e62018-06-12 10:11:12 -04001391 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1392 spotColor,
1393 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001394 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001395 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001396 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001397 if (op) {
1398 this->addDrawOp(clip, std::move(op));
1399 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001400 }
1401
1402 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001403}
1404
1405///////////////////////////////////////////////////////////////////////////////
1406
Brian Osman11052242016-10-27 14:47:55 -04001407bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001408 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001409 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001410 const SkMatrix& viewMatrix,
1411 const SkRRect& origOuter,
1412 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001413 SkASSERT(!origInner.isEmpty());
1414 SkASSERT(!origOuter.isEmpty());
1415
Brian Salomon65749212017-12-01 16:01:47 -05001416 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1417
Chris Dalton7d6748e2019-03-13 00:34:52 -06001418 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001419
1420 if (GrAAType::kMSAA == aaType) {
1421 return false;
1422 }
1423
Greg Daniel2655ede2019-04-10 00:49:28 +00001424 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1425 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001426 auto outerR = outer->width() / 2.f;
1427 auto innerR = inner->width() / 2.f;
1428 auto cx = outer->getBounds().fLeft + outerR;
1429 auto cy = outer->getBounds().fTop + outerR;
1430 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1431 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1432 auto avgR = (innerR + outerR) / 2.f;
1433 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1434 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1435 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001436 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001437 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001438 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001439 if (op) {
1440 this->addDrawOp(clip, std::move(op));
1441 return true;
1442 }
Mike Klein16885072018-12-11 09:54:31 -05001443 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001444 }
1445 }
1446
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001447 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001448 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001449 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1450 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001451 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001452 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1453 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001454 }
robertphillips00095892016-02-29 13:50:40 -08001455
robertphillips00095892016-02-29 13:50:40 -08001456 SkMatrix inverseVM;
1457 if (!viewMatrix.isIdentity()) {
1458 if (!origInner.transform(viewMatrix, inner.writable())) {
1459 return false;
1460 }
1461 if (!origOuter.transform(viewMatrix, outer.writable())) {
1462 return false;
1463 }
1464 if (!viewMatrix.invert(&inverseVM)) {
1465 return false;
1466 }
1467 } else {
1468 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001469 }
robertphillips00095892016-02-29 13:50:40 -08001470
Ethan Nicholaseace9352018-10-15 20:09:54 +00001471 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001472 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001473 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001474 if (!innerEffect) {
1475 return false;
1476 }
1477
Ethan Nicholaseace9352018-10-15 20:09:54 +00001478 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001479 if (!outerEffect) {
1480 return false;
1481 }
1482
Brian Salomon82f44312017-01-11 13:42:54 -05001483 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1484 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001485
1486 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001487 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001488 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1489 }
halcanary9d524f22016-03-29 09:03:52 -07001490
Brian Salomon82f44312017-01-11 13:42:54 -05001491 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1492 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001493 return true;
1494}
1495
Brian Osman11052242016-10-27 14:47:55 -04001496void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001497 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001498 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001499 const SkMatrix& viewMatrix,
1500 const SkRRect& outer,
1501 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001502 ASSERT_SINGLE_OWNER
1503 RETURN_IF_ABANDONED
1504 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001505 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001506
1507 SkASSERT(!outer.isEmpty());
1508 SkASSERT(!inner.isEmpty());
1509
Robert Phillips72152832017-01-25 17:31:35 -05001510 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001511
Brian Salomon82f44312017-01-11 13:42:54 -05001512 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001513 return;
1514 }
Mike Klein16885072018-12-11 09:54:31 -05001515 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001516
1517 SkPath path;
1518 path.setIsVolatile(true);
1519 path.addRRect(inner);
1520 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001521 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001522 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001523}
1524
robertphillipsea461502015-05-26 11:38:03 -07001525///////////////////////////////////////////////////////////////////////////////
1526
Brian Osman11052242016-10-27 14:47:55 -04001527void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001528 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001529 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001530 const SkMatrix& viewMatrix,
1531 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001532 const GrStyle& style,
1533 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001534 ASSERT_SINGLE_OWNER
1535 RETURN_IF_ABANDONED
1536 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001537 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001538
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001539 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001540 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001541 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001542 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001543 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1544 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001545 aa = GrAA::kNo;
1546 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001547 }
msarettcc319b92016-08-25 18:07:18 -07001548 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001549 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001550 SkPath path;
1551 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001552 path.setIsVolatile(true);
1553
Brian Salomon82f44312017-01-11 13:42:54 -05001554 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001555 }
1556
Chris Dalton7d6748e2019-03-13 00:34:52 -06001557 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001558 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1559 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001560 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001561}
1562
Brian Osman11052242016-10-27 14:47:55 -04001563void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001564 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001565 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001566 const SkMatrix& viewMatrix,
1567 const SkRect& oval,
1568 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001569 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001570 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001571 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001572 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001573
Robert Phillips7484d202018-07-03 09:09:08 -04001574 const SkStrokeRec& stroke = style.strokeRec();
1575
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001576 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001577 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1578 return;
1579 }
1580
1581 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001582 return;
robertphillipsea461502015-05-26 11:38:03 -07001583 }
1584
Robert Phillips72152832017-01-25 17:31:35 -05001585 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001586
Chris Dalton7d6748e2019-03-13 00:34:52 -06001587 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001588
1589 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001590 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1591 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001592 // We don't draw true circles as round rects in coverage mode, because it can
1593 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1594 assert_alive(paint);
1595 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1596 this->caps()->shaderCaps());
1597 }
1598 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001599 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1600 // the arc equation. This same special geometry and fragment branch also turn out to be a
1601 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1602 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1603 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001604 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001605 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1606 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001607 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001608 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001609 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001610 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1611 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001612 }
1613 if (op) {
1614 this->addDrawOp(clip, std::move(op));
1615 return;
robertphillipsea461502015-05-26 11:38:03 -07001616 }
robertphillipsb56f9272016-02-25 11:03:52 -08001617
Mike Klein16885072018-12-11 09:54:31 -05001618 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001619 this->drawShapeUsingPathRenderer(
1620 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001621 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001622}
1623
Brian Osman11052242016-10-27 14:47:55 -04001624void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001625 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001626 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001627 const SkMatrix& viewMatrix,
1628 const SkRect& oval,
1629 SkScalar startAngle,
1630 SkScalar sweepAngle,
1631 bool useCenter,
1632 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001633 ASSERT_SINGLE_OWNER
1634 RETURN_IF_ABANDONED
1635 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001636 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001637
1638 AutoCheckFlush acf(this->drawingManager());
1639
Chris Dalton7d6748e2019-03-13 00:34:52 -06001640 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001641 if (GrAAType::kCoverage == aaType) {
1642 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1643 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1644 std::move(paint),
1645 viewMatrix,
1646 oval,
1647 startAngle,
1648 sweepAngle,
1649 useCenter,
1650 style,
1651 shaderCaps);
1652 if (op) {
1653 this->addDrawOp(clip, std::move(op));
1654 return;
1655 }
1656 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001657 }
Brian Salomone4949402018-04-26 15:22:04 -04001658 this->drawShapeUsingPathRenderer(
1659 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001660 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001661}
1662
Brian Osman11052242016-10-27 14:47:55 -04001663void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001664 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001665 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001666 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001667 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001668 sk_sp<GrColorSpaceXform> csxf,
1669 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001670 std::unique_ptr<SkLatticeIter> iter,
1671 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001672 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001673 RETURN_IF_ABANDONED
1674 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001675 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001676
Robert Phillips72152832017-01-25 17:31:35 -05001677 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001678
Brian Salomon2a943df2018-05-04 13:43:19 -04001679 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001680 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001681 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001682 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001683}
1684
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001685void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1686 const SkRect& bounds) {
1687 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1688 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001689 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001690}
1691
Brian Salomon031b0ba2019-05-23 11:05:26 -04001692void GrRenderTargetContext::asyncRescaleAndReadPixels(
1693 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1694 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1695 auto direct = fContext->priv().asDirectContext();
1696 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001697 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001698 return;
1699 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001700 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001701 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001702 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001703 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001704 if (this->asRenderTargetProxy()->framebufferOnly()) {
1705 callback(context, nullptr);
1706 return;
1707 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001709 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001710 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001711 return;
1712 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001713 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001714 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001715 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001717 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001718 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1719 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001720 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001721 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001722 backendFormatOfFinalContext, dstCT);
1723 // Fail if we can't read from the source surface's color type.
1724 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001725 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001726 return;
1727 }
Brian Salomon624f9062019-07-02 14:23:00 -04001728 // Fail if read color type does not have all of dstCT's color channels and those missing color
1729 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001730 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1731 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1732 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1733 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001734 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 return;
1736 }
1737
Brian Salomonbf6b9792019-08-21 09:38:10 -04001738 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001739 int x = srcRect.fLeft;
1740 int y = srcRect.fTop;
1741 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001742 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1743 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001744 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001745 return;
1746 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001747 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001748 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001749 x = y = 0;
1750 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001751 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1752 this->colorInfo().alphaType(),
1753 info.colorSpace(),
1754 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001755 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1756 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001757 // We flip or color convert by drawing and we don't currently support drawing to
1758 // kPremul.
1759 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001760 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001761 return;
1762 }
Greg Daniel40903af2020-01-30 14:55:05 -05001763 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001764 SkRect srcRectToDraw = SkRect::Make(srcRect);
1765 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001766 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001767 texProxyView =
1768 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1769 SkBackingFit::kApprox, SkBudgeted::kNo);
1770 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001771 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001772 return;
1773 }
Brian Salomonc5243782020-04-02 12:50:34 -04001774 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001775 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1776 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001777 tempRTC = GrRenderTargetContext::Make(
1778 direct, this->colorInfo().colorType(), info.refColorSpace(),
1779 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001780 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001781 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001782 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001783 return;
1784 }
Greg Daniel40903af2020-01-30 14:55:05 -05001785 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1786 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1787 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001788 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1789 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1790 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001791 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001792 }
1793 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001794 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001795 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1796 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001797}
1798
Brian Salomon9241a6d2019-10-03 13:26:54 -04001799class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1800public:
1801 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1802 ~AsyncReadResult() override {
1803 for (int i = 0; i < fPlanes.count(); ++i) {
1804 if (!fPlanes[i].fMappedBuffer) {
1805 delete[] static_cast<const char*>(fPlanes[i].fData);
1806 } else {
1807 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1808 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1809 }
1810 }
1811 }
1812
1813 int count() const override { return fPlanes.count(); }
1814 const void* data(int i) const override { return fPlanes[i].fData; }
1815 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1816
1817 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001818 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001819 size_t rowBytes,
1820 GrClientMappedBufferManager* manager) {
1821 SkASSERT(!result.fTransferBuffer->isMapped());
1822 const void* mappedData = result.fTransferBuffer->map();
1823 if (!mappedData) {
1824 return false;
1825 }
1826 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001827 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001828 result.fPixelConverter(convertedData.get(), mappedData);
1829 this->addCpuPlane(std::move(convertedData), rowBytes);
1830 result.fTransferBuffer->unmap();
1831 } else {
1832 manager->insert(result.fTransferBuffer);
1833 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1834 }
1835 return true;
1836 }
1837
1838 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1839 SkASSERT(data);
1840 SkASSERT(rowBytes > 0);
1841 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1842 }
1843
1844private:
1845 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1846 SkASSERT(data);
1847 SkASSERT(rowBytes > 0);
1848 SkASSERT(mappedBuffer);
1849 SkASSERT(mappedBuffer->isMapped());
1850 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1851 }
1852
1853 struct Plane {
1854 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1855 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1856 const void* fData;
1857 size_t fRowBytes;
1858 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1859 sk_sp<GrGpuBuffer> fMappedBuffer;
1860 };
1861 SkSTArray<3, Plane> fPlanes;
1862 uint32_t fInboxID;
1863};
1864
Brian Salomon024bd002019-06-11 11:38:16 -04001865void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1866 ReadPixelsCallback callback,
1867 ReadPixelsContext context) {
1868 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1869 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1870
Greg Danielca9dbe22020-01-02 13:44:30 -05001871 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001872 callback(context, nullptr);
1873 return;
1874 }
1875
Brian Salomon9241a6d2019-10-03 13:26:54 -04001876 auto directContext = fContext->priv().asDirectContext();
1877 SkASSERT(directContext);
1878 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1879
Brian Salomon024bd002019-06-11 11:38:16 -04001880 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1881
1882 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001883 auto ii = SkImageInfo::Make(rect.size(), colorType,
1884 this->colorInfo().alphaType(),
1885 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001886 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001887 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1888 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1889 result->addCpuPlane(std::move(data), pm.rowBytes());
1890
Brian Salomon1d435302019-07-01 13:05:28 -04001891 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001892 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001893 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001894 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001896 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001897 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001898
Brian Salomonab32f652019-05-10 14:24:50 -04001899 struct FinishContext {
1900 ReadPixelsCallback* fClientCallback;
1901 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001902 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001903 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001904 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001905 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001906 };
1907 // Assumption is that the caller would like to flush. We could take a parameter or require an
1908 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1909 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001910 auto* finishContext = new FinishContext{callback,
1911 context,
1912 rect.size(),
1913 colorType,
1914 mappedBufferManager,
1915 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001916 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001917 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001918 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1920 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1921 context->fMappedBufferManager)) {
1922 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001923 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001924 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001925 delete context;
1926 };
1927 GrFlushInfo flushInfo;
1928 flushInfo.fFinishedContext = finishContext;
1929 flushInfo.fFinishedProc = finishCallback;
1930 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1931}
1932
Brian Salomon9241a6d2019-10-03 13:26:54 -04001933void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1934 sk_sp<SkColorSpace> dstColorSpace,
1935 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001936 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001937 RescaleGamma rescaleGamma,
1938 SkFilterQuality rescaleQuality,
1939 ReadPixelsCallback callback,
1940 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001941 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1942 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001943 SkASSERT(!dstSize.isZero());
1944 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1945
Brian Salomon024bd002019-06-11 11:38:16 -04001946 auto direct = fContext->priv().asDirectContext();
1947 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001948 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001949 return;
1950 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001951 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001952 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001953 return;
1954 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001955 if (this->asRenderTargetProxy()->framebufferOnly()) {
1956 callback(context, nullptr);
1957 return;
1958 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001959 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001960 callback(context, nullptr);
1961 return;
1962 }
Brian Salomon024bd002019-06-11 11:38:16 -04001963 int x = srcRect.fLeft;
1964 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001965 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001966 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001967 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001968 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001969 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001970 dstColorSpace);
1971 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001972 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001973 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001974 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001975 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001976 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001977 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001978 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001979 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001980 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001981 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001982 srcView = this->readSurfaceView();
1983 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001984 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1985 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1986 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001987 // If we can't get a texture copy of the contents then give up.
1988 callback(context, nullptr);
1989 return;
1990 }
Brian Salomonc5243782020-04-02 12:50:34 -04001991 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001992 x = y = 0;
1993 }
Brian Salomon4d036892019-07-02 15:10:58 -04001994 // We assume the caller wants kPremul. There is no way to indicate a preference.
1995 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001996 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1997 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001998 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001999 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2000 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002001 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2002 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002003 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002004 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002005 return;
2006 }
Greg Daniel40903af2020-01-30 14:55:05 -05002007 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
2008 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2009 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2010 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2011 SkMatrix::I(), std::move(xform));
2012 srcView = tempRTC->readSurfaceView();
2013 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002014 x = y = 0;
2015 }
2016 }
Greg Danielc594e622019-10-15 14:01:49 -04002017
Greg Daniele20fcad2020-01-08 11:52:34 -05002018 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2019 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2020 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002021 int halfW = dstSize.width()/2;
2022 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002023 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2024 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2025 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2026 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2027 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2028 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002029 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002030 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002031 return;
2032 }
2033
Brian Osman28ba5282019-10-30 14:18:07 -04002034 float baseM[20];
2035 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2036
Brian Salomon024bd002019-06-11 11:38:16 -04002037 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2038
2039 auto texMatrix = SkMatrix::MakeTrans(x, y);
2040
Brian Salomon9241a6d2019-10-03 13:26:54 -04002041 SkRect dstRectY = SkRect::Make(dstSize);
2042 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002043
Brian Salomon6aa65052020-01-28 12:16:53 -05002044 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2045 PixelTransferResult yTransfer, uTransfer, vTransfer;
2046
Brian Salomon024bd002019-06-11 11:38:16 -04002047 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2048 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002049 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002050 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002051 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002052 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002053 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002054 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2055 yPaint.addColorFragmentProcessor(std::move(yFP));
2056 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002057 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2058 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002059 if (!doSynchronousRead) {
2060 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2061 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2062 if (!yTransfer.fTransferBuffer) {
2063 callback(context, nullptr);
2064 return;
2065 }
Brian Salomon024bd002019-06-11 11:38:16 -04002066 }
2067
2068 texMatrix.preScale(2.f, 2.f);
2069 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2070 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002071 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002072 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002073 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002074 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002075 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002076 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2077 uPaint.addColorFragmentProcessor(std::move(uFP));
2078 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002079 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2080 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002081 if (!doSynchronousRead) {
2082 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2083 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2084 if (!uTransfer.fTransferBuffer) {
2085 callback(context, nullptr);
2086 return;
2087 }
Brian Salomon024bd002019-06-11 11:38:16 -04002088 }
2089
2090 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2091 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002092 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002093 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002094 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002095 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2096 this->colorInfo().alphaType(), texMatrix,
2097 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002098 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2099 vPaint.addColorFragmentProcessor(std::move(vFP));
2100 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002101 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2102 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002103 if (!doSynchronousRead) {
2104 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2105 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2106 if (!vTransfer.fTransferBuffer) {
2107 callback(context, nullptr);
2108 return;
2109 }
2110 }
2111
2112 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002113 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2114 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2115 size_t yRB = yInfo.minRowBytes();
2116 size_t uvRB = uvInfo.minRowBytes();
2117 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2118 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2119 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2120 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2121 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2122 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002123 callback(context, nullptr);
2124 return;
2125 }
2126 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002127 result->addCpuPlane(std::move(y), yRB );
2128 result->addCpuPlane(std::move(u), uvRB);
2129 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002130 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002131 return;
2132 }
2133
2134 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002135 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002136 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002137 GrClientMappedBufferManager* fMappedBufferManager;
2138 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002139 PixelTransferResult fYTransfer;
2140 PixelTransferResult fUTransfer;
2141 PixelTransferResult fVTransfer;
2142 };
2143 // Assumption is that the caller would like to flush. We could take a parameter or require an
2144 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2145 // callback to GrGpu until after the next flush that flushes our op list, though.
2146 auto* finishContext = new FinishContext{callback,
2147 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002148 direct->priv().clientMappedBufferManager(),
2149 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002150 std::move(yTransfer),
2151 std::move(uTransfer),
2152 std::move(vTransfer)};
2153 auto finishCallback = [](GrGpuFinishedContext c) {
2154 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002155 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002156 auto manager = context->fMappedBufferManager;
2157 size_t rowBytes = SkToSizeT(context->fSize.width());
2158 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2159 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002160 delete context;
2161 return;
2162 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002163 rowBytes /= 2;
2164 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2165 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2166 (*context->fClientCallback)(context->fClientContext, nullptr);
2167 delete context;
2168 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002169 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002170 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2171 (*context->fClientCallback)(context->fClientContext, nullptr);
2172 delete context;
2173 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002174 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002175 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002176 delete context;
2177 };
2178 GrFlushInfo flushInfo;
2179 flushInfo.fFinishedContext = finishContext;
2180 flushInfo.fFinishedProc = finishCallback;
2181 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002182}
2183
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002184GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2185 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002186 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002187 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002188 return GrSemaphoresSubmitted::kNo;
2189 }
robertphillips8c523e02016-07-26 07:41:00 -07002190 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002191 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002192
Greg Daniel55f040b2020-02-13 15:38:32 +00002193 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002194}
2195
Greg Danielc64ee462017-06-15 16:59:49 -04002196bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002197 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002198 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002199 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002200 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002201 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002202
2203 AutoCheckFlush acf(this->drawingManager());
2204
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002205 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002206 return false;
2207 }
2208
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002209 auto direct = fContext->priv().asDirectContext();
2210 if (!direct) {
2211 return false;
2212 }
2213
2214 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002215
Greg Daniel301015c2019-11-18 14:06:46 -05002216 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2217 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002218 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002219 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002220 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2221 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002222 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002223 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2224 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002225 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002226}
joshualitt33a5fce2015-11-18 13:28:51 -08002227
Brian Osman11052242016-10-27 14:47:55 -04002228void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002229 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002230 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002231 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002232 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002233 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002234 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002235 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002236 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002237 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2238
Michael Ludwig2686d692020-04-17 20:21:37 +00002239 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002240
Robert Phillips27927a52018-08-20 13:18:12 -04002241 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002242}
2243
2244void GrRenderTargetContext::drawShape(const GrClip& clip,
2245 GrPaint&& paint,
2246 GrAA aa,
2247 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002248 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002249 ASSERT_SINGLE_OWNER
2250 RETURN_IF_ABANDONED
2251 SkDEBUGCODE(this->validate();)
2252 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2253
Brian Salomon2fad74a2017-12-20 13:28:55 -05002254 if (shape.isEmpty()) {
2255 if (shape.inverseFilled()) {
2256 this->drawPaint(clip, std::move(paint), viewMatrix);
2257 }
2258 return;
robertphillipsea461502015-05-26 11:38:03 -07002259 }
2260
Robert Phillips72152832017-01-25 17:31:35 -05002261 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002262
Brian Salomon2fad74a2017-12-20 13:28:55 -05002263 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002264 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002265 SkRRect rrect;
2266 // We can ignore the starting point and direction since there is no path effect.
2267 bool inverted;
2268 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2269 if (rrect.isRect()) {
2270 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2271 &shape.style());
2272 return;
2273 } else if (rrect.isOval()) {
2274 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002275 return;
2276 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002277 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2278 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002279 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2280 viewMatrix.rectStaysRect()) {
2281 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2282 // the matrix to all the points individually rather than just to the rect
2283 SkRect rects[2];
2284 if (shape.asNestedRects(rects)) {
2285 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002286 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002287 fContext, std::move(paint), viewMatrix, rects);
2288 if (op) {
2289 this->addDrawOp(clip, std::move(op));
2290 }
2291 // Returning here indicates that there is nothing to draw in this case.
2292 return;
2293 }
robertphillipsea461502015-05-26 11:38:03 -07002294 }
2295 }
robertphillips4bc31812016-03-01 12:22:49 -08002296
Brian Salomon2fad74a2017-12-20 13:28:55 -05002297 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002298}
2299
Chris Daltonbbfd5162017-11-07 13:35:22 -07002300bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002301 const GrUserStencilSettings* ss,
2302 SkRegion::Op op,
2303 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002304 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002305 const SkMatrix& viewMatrix,
2306 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002307 ASSERT_SINGLE_OWNER_PRIV
2308 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002309 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002310 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2311 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002312
2313 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002314 GrPaint paint;
2315 paint.setCoverageSetOpXPFactory(op, invert);
2316 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2317 SkRect::MakeIWH(fRenderTargetContext->width(),
2318 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002319 return true;
2320 }
2321
Robert Phillips72152832017-01-25 17:31:35 -05002322 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002323
2324 // An Assumption here is that path renderer would use some form of tweaking
2325 // the src color (either the input alpha or in the frag shader) to implement
2326 // aa. If we have some future driver-mojo path AA that can do the right
2327 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002328 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002329 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002330
Chris Daltondb91c6e2017-09-08 16:25:08 -06002331 SkIRect clipConservativeBounds;
2332 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2333 &clipConservativeBounds, nullptr);
2334
Michael Ludwig2686d692020-04-17 20:21:37 +00002335 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002336 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002337 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002338 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002339 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002340 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002341 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002342 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002343 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2344 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002345 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002346
2347 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002348 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002349 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002350 if (!pr) {
2351 return false;
2352 }
2353
2354 GrPaint paint;
2355 paint.setCoverageSetOpXPFactory(op, invert);
2356
Brian Salomonf3569f02017-10-24 12:52:33 -04002357 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2358 std::move(paint),
2359 ss,
2360 fRenderTargetContext,
2361 &clip,
2362 &clipConservativeBounds,
2363 &viewMatrix,
2364 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002365 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002366 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002367 pr->drawPath(args);
2368 return true;
2369}
2370
Brian Osman11052242016-10-27 14:47:55 -04002371SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002372 ASSERT_SINGLE_OWNER_PRIV
2373
Robert Phillips6a6de562019-02-15 15:19:15 -05002374 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002375 return SkBudgeted::kNo;
2376 }
2377
Brian Osman11052242016-10-27 14:47:55 -04002378 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002379
Greg Daniel3912a4b2020-01-14 09:56:04 -05002380 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002381}
2382
Brian Salomon2fad74a2017-12-20 13:28:55 -05002383void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2384 GrPaint&& paint,
2385 GrAA aa,
2386 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002387 const GrStyledShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002388 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002389 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002390 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2391
Jim Van Verthf86073a2018-10-02 13:05:38 -04002392 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2393 return;
2394 }
2395
Chris Daltondb91c6e2017-09-08 16:25:08 -06002396 SkIRect clipConservativeBounds;
2397 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2398
Michael Ludwig2686d692020-04-17 20:21:37 +00002399 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002400 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002401
robertphillips68737822015-10-29 12:12:21 -07002402 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002403 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002404 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002405 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002406 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002407 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002408 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002409 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002410
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002411 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002412 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002413 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002414 return;
2415 }
2416
Chris Dalton6ce447a2019-06-23 18:07:38 -06002417 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002418
2419 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2420 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2421 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2422
Brian Salomon2fad74a2017-12-20 13:28:55 -05002423 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002424 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002425 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2426 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002427 return;
2428 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002429 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002430 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002431 }
2432 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002433 if (canDrawArgs.fShape->style().applies()) {
2434 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2435 styleScale);
2436 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002437 return;
2438 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002439 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002440 // This time, allow SW renderer
2441 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2442 } else {
2443 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002444#if GR_PATH_RENDERER_SPEW
2445 SkDebugf("falling back to: %s\n", pr->name());
2446#endif
bsalomon6663acf2016-05-10 09:14:17 -07002447 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002448 }
robertphillipsea461502015-05-26 11:38:03 -07002449
bsalomon8acedde2016-06-24 10:42:16 -07002450 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002451#ifdef SK_DEBUG
2452 SkDebugf("Unable to find path renderer compatible with path.\n");
2453#endif
2454 return;
2455 }
2456
Robert Phillips256c37b2017-03-01 14:32:46 -05002457 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002458 std::move(paint),
2459 &GrUserStencilSettings::kUnused,
2460 this,
2461 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002462 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002463 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002464 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002465 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002466 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002467 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002468}
2469
Brian Salomon467921e2017-03-06 16:17:12 -05002470static void op_bounds(SkRect* bounds, const GrOp* op) {
2471 *bounds = op->bounds();
2472 if (op->hasZeroArea()) {
2473 if (op->hasAABloat()) {
2474 bounds->outset(0.5f, 0.5f);
2475 } else {
2476 // We don't know which way the particular GPU will snap lines or points at integer
2477 // coords. So we ensure that the bounds is large enough for either snap.
2478 SkRect before = *bounds;
2479 bounds->roundOut(bounds);
2480 if (bounds->fLeft == before.fLeft) {
2481 bounds->fLeft -= 1;
2482 }
2483 if (bounds->fTop == before.fTop) {
2484 bounds->fTop -= 1;
2485 }
2486 if (bounds->fRight == before.fRight) {
2487 bounds->fRight += 1;
2488 }
2489 if (bounds->fBottom == before.fBottom) {
2490 bounds->fBottom += 1;
2491 }
2492 }
2493 }
2494}
2495
Chris Dalton08755122019-08-05 16:13:47 -06002496void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002497 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002498 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2499}
2500
Brian Salomon348a0372018-10-31 10:42:18 -04002501void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2502 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002503 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002504 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002505 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002506 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002507 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002508 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002509 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002510 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002511
Brian Salomon467921e2017-03-06 16:17:12 -05002512 // Setup clip
2513 SkRect bounds;
2514 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002515 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002516 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002517 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002518 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002519
Chris Dalton9acf6822019-11-12 11:52:40 -07002520 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002521 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002522 }
2523
Chris Dalton9acf6822019-11-12 11:52:40 -07002524 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002525 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002526 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002527 }
2528
Chris Dalton9acf6822019-11-12 11:52:40 -07002529 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2530 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2531
2532 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2533 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2534 // that uses a stencil test when the stencil buffer is multisampled.)
2535 bool hasMixedSampledCoverage = (
2536 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002537 SkASSERT(!hasMixedSampledCoverage ||
2538 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002539
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002540 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002541 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002542 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2543
Greg Daniel524e28b2019-11-01 11:48:53 -04002544 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002545 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002546 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002547 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002548 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002549 }
2550 }
2551
2552 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002553 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002554 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002555 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002556 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002557 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002558 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002559}
2560
Greg Daniel524e28b2019-11-01 11:48:53 -04002561bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2562 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002563 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2564 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2565 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002566 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002567 return false;
2568 }
2569
Greg Daniel3912a4b2020-01-14 09:56:04 -05002570 if (this->caps()->textureBarrierSupport() &&
2571 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002572 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002573 // The render target is a texture, so we can read from it directly in the shader. The XP
2574 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002575 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002576 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002577 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002578 }
2579 }
2580
Greg Daniel3912a4b2020-01-14 09:56:04 -05002581 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002582
Eric Karl74480882017-04-03 14:49:05 -07002583 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002584 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002585 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002586 SkRect opBounds = op.bounds();
2587 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2588 // 0.5 pixels.
2589 if (op.hasAABloat() || op.hasZeroArea()) {
2590 opBounds.outset(0.5f, 0.5f);
2591 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2592 // performance we may ignore the clip when the draw is entirely inside the clip is float
2593 // space but will hit pixels just outside the clip when actually rasterizing.
2594 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002595 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002596 }
2597 SkIRect opIBounds;
2598 opBounds.roundOut(&opIBounds);
2599 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002600#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002601 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002602#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002603 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002604 }
2605
Greg Daniel3155f7f2020-01-16 16:54:26 -05002606 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002607 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2608 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002609 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002610 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002611
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002612 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002613 copyRect = clippedRect;
2614 }
Brian Salomon467921e2017-03-06 16:17:12 -05002615
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002616 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002617 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002618 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002619 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002620 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002621 } else {
Eric Karl74480882017-04-03 14:49:05 -07002622 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002623 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002624 }
Brian Salomonc5243782020-04-02 12:50:34 -04002625 auto copy =
2626 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2627 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2628 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002629
Brian Salomonc5243782020-04-02 12:50:34 -04002630 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002631 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002632 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002633}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002634
Greg Daniel573312e2020-02-07 17:22:35 -05002635bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002636 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002637 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002638 SkIRect clippedSrcRect;
2639 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002640 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2641 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002642 return false;
2643 }
2644
2645 GrPaint paint;
2646 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002647
Greg Danield2ccbb52020-02-05 10:45:39 -05002648 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002649 if (!fp) {
2650 return false;
2651 }
2652 paint.addColorFragmentProcessor(std::move(fp));
2653
2654 this->fillRectToRect(
2655 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2656 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2657 clippedSrcRect.height()),
2658 SkRect::Make(clippedSrcRect));
2659 return true;
2660}
Brian Salomon198393b2020-04-20 15:38:21 -04002661
2662void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2663 SkASSERT(&task == fOpsTask.get());
2664 fOpsTask.reset();
2665}