blob: 0210d5f56638dc884e64edf68c49240bdd855354 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050058#include "src/gpu/ops/GrDrawAtlasOp.h"
59#include "src/gpu/ops/GrDrawOp.h"
60#include "src/gpu/ops/GrDrawVerticesOp.h"
61#include "src/gpu/ops/GrDrawableOp.h"
62#include "src/gpu/ops/GrFillRRectOp.h"
63#include "src/gpu/ops/GrFillRectOp.h"
64#include "src/gpu/ops/GrLatticeOp.h"
65#include "src/gpu/ops/GrOp.h"
66#include "src/gpu/ops/GrOvalOpFactory.h"
67#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068#include "src/gpu/ops/GrShadowRRectOp.h"
69#include "src/gpu/ops/GrStencilPathOp.h"
70#include "src/gpu/ops/GrStrokeRectOp.h"
71#include "src/gpu/ops/GrTextureOp.h"
72#include "src/gpu/text/GrTextContext.h"
73#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040074
Herb Derbyc1b482c2018-08-09 15:02:27 -040075class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040076public:
77 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040078 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040079 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040080 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040081 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040082
Michael Ludwig7c12e282020-05-29 09:54:07 -040083 void addDrawOp(const GrClip* clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040084 fRenderTargetContext->addDrawOp(clip, std::move(op));
85 }
86
Michael Ludwig7c12e282020-05-29 09:54:07 -040087 void drawShape(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -040088 const SkPaint& paint,
89 const SkMatrixProvider& matrixProvider,
90 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040091 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040092 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040093 }
94
Brian Osman449b1152020-04-15 16:43:00 -040095 void makeGrPaint(GrMaskFormat maskFormat,
96 const SkPaint& skPaint,
97 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040098 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050099 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400100 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400101 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400102 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
103 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400104 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400105 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400106 }
107 }
108
Robert Phillips69893702019-02-22 11:16:30 -0500109 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400110 return fRenderTargetContext->fContext;
111 }
112
Herb Derby65956872018-08-21 16:55:04 -0400113 SkGlyphRunListPainter* glyphPainter() override {
114 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400115 }
116
Brian Salomonf18b1d82017-10-27 11:30:49 -0400117private:
118 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400119 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400120
Brian Salomonf18b1d82017-10-27 11:30:49 -0400121};
joshualittbc907352016-01-13 06:45:40 -0800122
Robert Phillips72152832017-01-25 17:31:35 -0500123#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -0400124#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
125#define ASSERT_SINGLE_OWNER_PRIV GR_ASSERT_SINGLE_OWNER(fRenderTargetContext->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -0500126#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
127#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
128#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
129#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
130#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700131
Brian Salomone225b562017-06-14 13:00:03 -0400132//////////////////////////////////////////////////////////////////////////////
133
robertphillipsea461502015-05-26 11:38:03 -0700134class AutoCheckFlush {
135public:
halcanary9d524f22016-03-29 09:03:52 -0700136 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700137 SkASSERT(fDrawingManager);
138 }
bsalomonb77a9072016-09-07 10:02:04 -0700139 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700140
141private:
robertphillips77a2e522015-10-17 07:43:27 -0700142 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700143};
144
Greg Daniele20fcad2020-01-08 11:52:34 -0500145std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
146 GrRecordingContext* context,
147 GrColorType colorType,
148 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500149 sk_sp<GrSurfaceProxy> proxy,
150 GrSurfaceOrigin origin,
151 const SkSurfaceProps* surfaceProps,
152 bool managedOps) {
153 if (!proxy) {
154 return nullptr;
155 }
156
157 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400158 GrSwizzle readSwizzle, writeSwizzle;
159 if (colorType != GrColorType::kUnknown) {
160 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
161 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
162 }
Greg Danielba0ff782020-01-07 15:42:57 -0500163
Greg Daniel3912a4b2020-01-14 09:56:04 -0500164 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400165 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500166
167 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400168 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500169 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500170}
171
172std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
173 GrRecordingContext* context,
174 GrColorType colorType,
175 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500176 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500177 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500178 const GrBackendFormat& format,
179 int sampleCnt,
180 GrMipMapped mipMapped,
181 GrProtected isProtected,
182 GrSurfaceOrigin origin,
183 SkBudgeted budgeted,
184 const SkSurfaceProps* surfaceProps) {
185 // It is probably not necessary to check if the context is abandoned here since uses of the
186 // GrRenderTargetContext which need the context will mostly likely fail later on without an
187 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
188 // an abandoned context correctly. It also lets us early out of some extra work.
189 if (context->priv().abandoned()) {
190 return nullptr;
191 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500192
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400194 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500195 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500196 if (!proxy) {
197 return nullptr;
198 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500199
Greg Danielba0ff782020-01-07 15:42:57 -0500200 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
201 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500202 if (!rtc) {
203 return nullptr;
204 }
205 rtc->discard();
206 return rtc;
207}
208
209std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
210 GrRecordingContext* context,
211 GrColorType colorType,
212 sk_sp<SkColorSpace> colorSpace,
213 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500214 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500215 int sampleCnt,
216 GrMipMapped mipMapped,
217 GrProtected isProtected,
218 GrSurfaceOrigin origin,
219 SkBudgeted budgeted,
220 const SkSurfaceProps* surfaceProps) {
221 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
222 if (!format.isValid()) {
223 return nullptr;
224 }
225
226 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
227 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
228 surfaceProps);
229}
230
231static inline GrColorType color_type_fallback(GrColorType ct) {
232 switch (ct) {
233 // kRGBA_8888 is our default fallback for many color types that may not have renderable
234 // backend formats.
235 case GrColorType::kAlpha_8:
236 case GrColorType::kBGR_565:
237 case GrColorType::kABGR_4444:
238 case GrColorType::kBGRA_8888:
239 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400240 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500241 case GrColorType::kRGBA_F16:
242 case GrColorType::kRGBA_F16_Clamped:
243 return GrColorType::kRGBA_8888;
244 case GrColorType::kAlpha_F16:
245 return GrColorType::kRGBA_F16;
246 case GrColorType::kGray_8:
247 return GrColorType::kRGB_888x;
248 default:
249 return GrColorType::kUnknown;
250 }
251}
252
Brian Salomond005b692020-04-01 15:47:05 -0400253std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400254 GrImageContext* context, GrColorType colorType, int sampleCnt) {
255 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400256 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400257 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
258 // We continue to the fallback color type if there no default renderable format or we
259 // requested msaa and the format doesn't support msaa.
260 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400261 return {colorType, format};
262 }
263 colorType = color_type_fallback(colorType);
264 } while (colorType != GrColorType::kUnknown);
265 return {GrColorType::kUnknown, {}};
266}
267
Greg Daniele20fcad2020-01-08 11:52:34 -0500268std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
269 GrRecordingContext* context,
270 GrColorType colorType,
271 sk_sp<SkColorSpace> colorSpace,
272 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500273 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500274 int sampleCnt,
275 GrMipMapped mipMapped,
276 GrProtected isProtected,
277 GrSurfaceOrigin origin,
278 SkBudgeted budgeted,
279 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400280 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400281 if (ct == GrColorType::kUnknown) {
282 return nullptr;
283 }
284 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
285 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500286}
287
Greg Danielba0ff782020-01-07 15:42:57 -0500288std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
289 GrRecordingContext* context,
290 GrColorType colorType,
291 sk_sp<SkColorSpace> colorSpace,
292 const GrBackendTexture& tex,
293 int sampleCnt,
294 GrSurfaceOrigin origin,
295 const SkSurfaceProps* surfaceProps,
296 ReleaseProc releaseProc,
297 ReleaseContext releaseCtx) {
298 SkASSERT(sampleCnt > 0);
299 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400300 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500301 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500302 if (!proxy) {
303 return nullptr;
304 }
305
306 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
307 origin, surfaceProps);
308}
309
310std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
311 GrRecordingContext* context,
312 GrColorType colorType,
313 sk_sp<SkColorSpace> colorSpace,
314 const GrBackendTexture& tex,
315 int sampleCnt,
316 GrSurfaceOrigin origin,
317 const SkSurfaceProps* surfaceProps) {
318 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400319 sk_sp<GrSurfaceProxy> proxy(
320 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500321 if (!proxy) {
322 return nullptr;
323 }
324
325 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
326 origin, surfaceProps);
327}
328
329std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
330 GrRecordingContext* context,
331 GrColorType colorType,
332 sk_sp<SkColorSpace> colorSpace,
333 const GrBackendRenderTarget& rt,
334 GrSurfaceOrigin origin,
335 const SkSurfaceProps* surfaceProps,
336 ReleaseProc releaseProc,
337 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400338 sk_sp<GrSurfaceProxy> proxy(
339 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500340 if (!proxy) {
341 return nullptr;
342 }
343
344 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
345 origin, surfaceProps);
346}
347
348std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
349 GrRecordingContext* context,
350 const SkImageInfo& imageInfo,
351 const GrVkDrawableInfo& vkInfo,
352 const SkSurfaceProps* props) {
353 sk_sp<GrSurfaceProxy> proxy(
354 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
355 vkInfo));
356 if (!proxy) {
357 return nullptr;
358 }
359
Greg Danielba0ff782020-01-07 15:42:57 -0500360 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
361 imageInfo.refColorSpace(), std::move(proxy),
362 kTopLeft_GrSurfaceOrigin, props);
363}
364
Greg Danielf41b2bd2019-08-22 16:19:24 -0400365// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
366// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
367// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
368// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500369GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500370 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400371 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400372 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400373 sk_sp<SkColorSpace> colorSpace,
374 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400375 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500376 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
377 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400378 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500379 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400380 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400381 , fManagedOpsTask(managedOpsTask) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400382 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400383 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400384 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400385 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
386 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500387
Brian Salomonf18b1d82017-10-27 11:30:49 -0400388 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700389 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700390}
391
robertphillips2e1e51f2015-10-15 08:01:48 -0700392#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500393void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400394 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400395 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700396 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700397}
398#endif
399
Brian Osman11052242016-10-27 14:47:55 -0400400GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800401 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400402 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400403 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400404 }
robertphillipsa106c622015-10-16 09:07:06 -0700405}
406
Chris Dalton7d6748e2019-03-13 00:34:52 -0600407inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408 if (GrAA::kNo == aa) {
409 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
410 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600411 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600412 return GrAAType::kMSAA;
413 }
414 return GrAAType::kNone;
415 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600416 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600417}
418
Greg Daniele252f082017-10-23 16:05:23 -0400419GrMipMapped GrRenderTargetContext::mipMapped() const {
420 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
421 return proxy->mipMapped();
422 }
423 return GrMipMapped::kNo;
424}
425
Greg Danielf41b2bd2019-08-22 16:19:24 -0400426GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800427 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700428 SkDEBUGCODE(this->validate();)
429
Brian Salomon3b8486a2020-04-21 12:43:26 -0400430 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600431 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400432 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600433 if (fOpsTask && fNumStencilSamples > 0) {
434 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600435 fOpsTask->setMustPreserveStencil();
436 // Reload the stencil buffer content at the beginning of newOpsTask.
437 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
438 // values?
439 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
440 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400441 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600442 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700443 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400444 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400445 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700446}
447
Michael Ludwig7c12e282020-05-29 09:54:07 -0400448void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400449 const SkMatrixProvider& matrixProvider,
450 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800451 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700452 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700453 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400454 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700455
Greg Danielbe7fc462019-01-03 16:40:42 -0500456 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
457 // secondary command buffers because it would require stopping and starting a render pass which
458 // we don't have access to.
459 if (this->wrapsVkSecondaryCB()) {
460 return;
461 }
462
Herb Derby26cbe512018-05-24 14:39:01 -0400463 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400464 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400465 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700466}
467
Brian Osman11052242016-10-27 14:47:55 -0400468void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800469 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700470 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700471 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400472 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700473
Robert Phillips72152832017-01-25 17:31:35 -0500474 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400475
Greg Danielf41b2bd2019-08-22 16:19:24 -0400476 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700477}
478
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500479static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
480 paint->setColor4f(color);
481 if (color.isOpaque()) {
482 // Can just rely on the src-over blend mode to do the right thing
483 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
484 } else {
485 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
486 // were src blended
487 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
488 }
489}
490
Michael Ludwig81d41722020-05-26 16:57:38 -0400491// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
492// premul alpha type. If we ever support different alpha type render targets, this function should
493// transform the color as appropriate.
494void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500495 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400496 bool upgradePartialToFull) {
497 ASSERT_SINGLE_OWNER
498 RETURN_IF_ABANDONED
499 SkDEBUGCODE(this->validate();)
500 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
501
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400502 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
503 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
504 // supports then. Drawing an axis-aligned rect is the fallback path.
505 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
506 if (scissor && !scissorState.set(*scissor)) {
507 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
508 // except clear ops are not draw ops).
509 return;
510 }
511
512 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
513 // the test. We only do this when the clear would be handled by a load op or natively.
514 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
515 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
516 this->caps()->shouldInitializeTextures())) {
517 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
518 // only clear the entire target if we knew it had not been cleared before. As
519 // is this could end up doing a lot of redundant clears.
520 scissorState.setDisabled();
521 } else {
522 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
523 // render target to overflow into any approx-fit padding of the backing store dimensions
524 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400525 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500526 }
527
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400528 if (!scissorState.enabled()) {
529 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
530 // discard all prior ops in the current task since the color buffer will be overwritten.
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 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400542 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500543
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400544 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
545 // as a load op.
546 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
547 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
548 if (clearAsDraw) {
549 GrPaint paint;
550 clear_to_grpaint(color, &paint);
551 this->addDrawOp(nullptr,
552 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
553 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500554 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400555 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500556 }
557}
558
Michael Ludwig7c12e282020-05-29 09:54:07 -0400559void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500560 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400561 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000562 // Start with the render target, since that is the maximum content we could possibly fill.
563 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500564 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400565 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000566 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
567 // inverse view matrix.
568 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
569 } else {
570 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
571 SkMatrix localMatrix;
572 if (!viewMatrix.invert(&localMatrix)) {
573 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400574 }
Michael Ludwig61328202019-06-19 14:48:58 +0000575 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
576 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700577 }
robertphillipsea461502015-05-26 11:38:03 -0700578}
579
Michael Ludwig61328202019-06-19 14:48:58 +0000580enum class GrRenderTargetContext::QuadOptimization {
581 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
582 kDiscarded,
583 // The rect to draw was converted to some other op and appended to the oplist, so no additional
584 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
585 // a constColor is provided.
586 kSubmitted,
587 // The clip was folded into the device quad, with updated edge flags and local coords, and
588 // caller is responsible for adding an appropriate op.
589 kClipApplied,
590 // No change to clip, but quad updated to better fit clip/render target, and caller is
591 // responsible for adding an appropriate op.
592 kCropped
593};
Michael Ludwig61a16512019-01-15 11:15:13 -0500594
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400595static bool make_vertex_finite(float* value) {
596 if (SkScalarIsNaN(*value)) {
597 return false;
598 }
599
600 if (!SkScalarIsFinite(*value)) {
601 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
602 // left when calculating crops.
603 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
604 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
605 }
606
607 return true;
608}
609
Michael Ludwig7c12e282020-05-29 09:54:07 -0400610static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
611 return clip ? clip->getConservativeBounds(rtc->width(), rtc->height())
612 : SkIRect::MakeWH(rtc->width(), rtc->height());
613}
614
Michael Ludwig61328202019-06-19 14:48:58 +0000615GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400616 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500617 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000618 // Optimization requirements:
619 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500620 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
621 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
622 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
623 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
624 // 5. kCropped in all other scenarios (although a crop may be a no-op)
625
626 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
627 // better to just keep the old flags instead of introducing mixed edge flags.
628 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000629
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400630 // Use the logical size of the render target, which allows for "fullscreen" clears even if
631 // the render target has an approximate backing fit
632 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400633
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500634 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000635 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400636 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
637 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500638 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400639 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500640 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
641 !make_vertex_finite(quad->fDevice.ys() + i) ||
642 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400643 // Discard if we see a nan
644 return QuadOptimization::kDiscarded;
645 }
646 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500647 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400648 }
649 } else {
650 // CropToRect requires the quads to be finite. If they are not finite and we have local
651 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500652 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400653 return QuadOptimization::kDiscarded;
654 }
Brian Salomon7694b902019-06-18 21:00:21 +0000655 }
656
Michael Ludwig61328202019-06-19 14:48:58 +0000657 // If the quad is entirely off screen, it doesn't matter what the clip does
658 if (!rtRect.intersects(drawBounds)) {
659 return QuadOptimization::kDiscarded;
660 }
Brian Salomon7694b902019-06-18 21:00:21 +0000661
Michael Ludwig61328202019-06-19 14:48:58 +0000662 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
663 // the render target).
664 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400665 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
666 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
667 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
668 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000669 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400670 if (clip && !clip->quickContains(rtRect)) {
671 if (!clip->isRRect(rtRect, &clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000672 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000673 }
Brian Salomon7694b902019-06-18 21:00:21 +0000674 }
675
Michael Ludwig61328202019-06-19 14:48:58 +0000676 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
677 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400678 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000679 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
680 // of the render target and the clip rect)
681 SkRect clipBounds = rtRect;
682 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
683 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000684 }
685
Michael Ludwig61328202019-06-19 14:48:58 +0000686 if (clipRRect.isRect()) {
687 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500688 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700689 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500690 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000691 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500692 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000693 if (drawBounds.contains(rtRect)) {
694 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400695 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000696 return QuadOptimization::kSubmitted;
697 } else if (GrClip::IsPixelAligned(drawBounds) &&
698 drawBounds.width() > 256 && drawBounds.height() > 256) {
699 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
700 SkIRect scissorRect;
701 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400702 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000703 return QuadOptimization::kSubmitted;
704 }
705 }
706
707 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000708 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500709 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000710 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
711 // properly reflect the smooth edge of the clip.
712 *aa = GrAA::kYes;
713 }
714 // We intentionally do not downgrade AA here because we don't know if we need to
715 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
716 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
717
718 // deviceQuad is exactly the intersection of original quad and clip, so it can be
719 // drawn with no clip (submitted by caller)
720 return QuadOptimization::kClipApplied;
721 } else {
722 // The quads have been updated to better fit the clip bounds, but can't get rid of
723 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500724 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000725 return QuadOptimization::kCropped;
726 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700727 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000728 // Rounded corners and constant filled color (limit ourselves to solid colors because
729 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500730 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
731 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
732 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000733 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
734 // we can draw the rrect directly and ignore the edge flags
735 GrPaint paint;
736 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400737 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000738 clipRRect, GrStyle::SimpleFill());
739 return QuadOptimization::kSubmitted;
740 } else {
741 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500742 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000743 return QuadOptimization::kCropped;
744 }
Brian Salomon7694b902019-06-18 21:00:21 +0000745 }
Brian Salomon7694b902019-06-18 21:00:21 +0000746 }
747
Michael Ludwig61328202019-06-19 14:48:58 +0000748 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400749 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000750
751 // One final check for discarding, since we may have gone here directly due to a complex clip
752 if (!clipBounds.intersects(drawBounds)) {
753 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000754 }
755
Michael Ludwig61328202019-06-19 14:48:58 +0000756 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
757 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500758 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
759 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000760
761 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000762}
763
Michael Ludwig7c12e282020-05-29 09:54:07 -0400764void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500765 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500766 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500767 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400768 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000769 ASSERT_SINGLE_OWNER
770 RETURN_IF_ABANDONED
771 SkDEBUGCODE(this->validate();)
772 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500773
Michael Ludwig61328202019-06-19 14:48:58 +0000774 AutoCheckFlush acf(this->drawingManager());
775
776 SkPMColor4f* constColor = nullptr;
777 SkPMColor4f paintColor;
778 if (!ss && !paint.numCoverageFragmentProcessors() &&
779 paint.isConstantBlendedColor(&paintColor)) {
780 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
781 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500782 }
783
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500784 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000785 if (opt >= QuadOptimization::kClipApplied) {
786 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400787 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000788 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
789 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500790 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
791 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700792 }
Michael Ludwig61328202019-06-19 14:48:58 +0000793 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800794}
795
Michael Ludwig7c12e282020-05-29 09:54:07 -0400796void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400797 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500798 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000799 sk_sp<GrColorSpaceXform> textureXform,
800 GrSamplerState::Filter filter,
801 const SkPMColor4f& color,
802 SkBlendMode blendMode,
803 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500804 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400805 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000806 ASSERT_SINGLE_OWNER
807 RETURN_IF_ABANDONED
808 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400809 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000810 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
811
812 AutoCheckFlush acf(this->drawingManager());
813
814 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
815 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500816 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000817
818 SkASSERT(opt != QuadOptimization::kSubmitted);
819 if (opt != QuadOptimization::kDiscarded) {
820 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400821 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000822 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400823 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400824 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
825 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400826 // Use the provided subset, although hypothetically we could detect that the cropped local
827 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500828 this->addDrawOp(finalClip,
829 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
830 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400831 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000832 }
833}
834
Michael Ludwig7c12e282020-05-29 09:54:07 -0400835void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500836 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500837 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400838 const SkMatrix& viewMatrix,
839 const SkRect& rect,
840 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700841 if (!style) {
842 style = &GrStyle::SimpleFill();
843 }
joshualitt1de610a2016-01-06 08:26:09 -0800844 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700845 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700846 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400847 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700848
bsalomon6663acf2016-05-10 09:14:17 -0700849 // Path effects should've been devolved to a path in SkGpuDevice
850 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700851
Robert Phillips72152832017-01-25 17:31:35 -0500852 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700853
bsalomon6663acf2016-05-10 09:14:17 -0700854 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400855 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000856 // Fills the rect, using rect as its own local coordinates
857 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500858 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400859 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
860 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
861 (rect.width() && rect.height())) {
862 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
863 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400864 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700865
Chris Dalton7d6748e2019-03-13 00:34:52 -0600866 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500867 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
868 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
869 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500870 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400871 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700872 return;
robertphillips4bc31812016-03-01 12:22:49 -0800873 }
robertphillips4bc31812016-03-01 12:22:49 -0800874 }
Mike Klein16885072018-12-11 09:54:31 -0500875 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000876 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
877 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700878}
879
Michael Ludwig7c12e282020-05-29 09:54:07 -0400880void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500881 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
882 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600883 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500884
885 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
886 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500887}
888
Robert Phillipsec2249f2016-11-09 08:54:35 -0500889int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500890 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400891 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500892}
893
Greg Danielf41b2bd2019-08-22 16:19:24 -0400894GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600895 ) const {
896#if GR_TEST_UTILS
897 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400898 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600899 }
900#endif
901 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
902 // would normally be overwritten. The one exception is if the render target context is marked as
903 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
904 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
905 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
906 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400907 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600908}
909
Chris Dalton858cf232019-10-14 16:20:00 -0600910void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600911 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600912 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600913 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600914
Chris Daltoneffee202019-07-01 22:28:03 -0600915 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600916 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500917 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600918 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400919 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600920 }
921 SkASSERT(numRequiredSamples > 0);
922
923 if (numRequiredSamples > fNumStencilSamples) {
924 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500925 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600926 }
Chris Dalton6b982802019-06-27 13:53:46 -0600927
Chris Dalton674f77a2019-09-30 20:49:39 -0600928 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600929 if (this->caps()->performStencilClearsAsDraws()) {
930 // There is a driver bug with clearing stencil. We must use an op to manually clear the
931 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400932 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600933 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600934 this->getOpsTask()->setInitialStencilContent(
935 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600936 }
937 }
938}
939
Michael Ludwig81d41722020-05-26 16:57:38 -0400940void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600941 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
942
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400943 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
944 if (scissor && !scissorState.set(*scissor)) {
945 // The requested clear region is off screen, so nothing to do.
946 return;
947 }
948
Michael Ludwig1e632792020-05-21 12:45:31 -0400949 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400950 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400951 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500952 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500953
954 // Configure the paint to have no impact on the color buffer
955 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400956 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400957 this->addDrawOp(nullptr,
958 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
959 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500960 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400961 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500962 }
robertphillips976f5f02016-06-03 10:59:20 -0700963}
964
Michael Ludwig7c12e282020-05-29 09:54:07 -0400965void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600966 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400967 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400968 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500969 ASSERT_SINGLE_OWNER_PRIV
970 RETURN_IF_ABANDONED_PRIV
971 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400972 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
973 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500974
Brian Salomon467921e2017-03-06 16:17:12 -0500975 // TODO: extract portions of checkDraw that are relevant to path stenciling.
976 SkASSERT(path);
977 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
978
979 // FIXME: Use path bounds instead of this WAR once
980 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
981 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
982
Michael Ludwig7c12e282020-05-29 09:54:07 -0400983 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
984 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
985 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400986 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
987 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
988
Michael Ludwig7c12e282020-05-29 09:54:07 -0400989 if (clip && !clip->apply(fRenderTargetContext->width(), fRenderTargetContext->height(),
990 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500991 return;
992 }
Michael Ludwig7c12e282020-05-29 09:54:07 -0400993 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
994 // but as it is, we're just using the full render target so intersecting the two bounds would
995 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -0500996
Robert Phillips7c525e62018-06-12 10:11:12 -0400997 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
998 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600999 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001000 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001001 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001002 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001003 if (!op) {
1004 return;
1005 }
Brian Salomon97180af2017-03-14 13:42:58 -04001006 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001007
Chris Daltoneffee202019-07-01 22:28:03 -06001008 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001009 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001010}
1011
Michael Ludwig7c12e282020-05-29 09:54:07 -04001012void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001013 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001014 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001015 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1016 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001017 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001018 ASSERT_SINGLE_OWNER
1019 RETURN_IF_ABANDONED
1020 SkDEBUGCODE(this->validate();)
1021 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001022
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001023 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1024 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1025 AutoCheckFlush acf(this->drawingManager());
1026 GrAAType aaType = this->chooseAAType(aa);
1027 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1028 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1029 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001030 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1031 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001032}
1033
Michael Ludwig7c12e282020-05-29 09:54:07 -04001034void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001035 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001036 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001037 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001038 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001039 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001040 ASSERT_SINGLE_OWNER
1041 RETURN_IF_ABANDONED
1042 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001043 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001044
1045 AutoCheckFlush acf(this->drawingManager());
1046
1047 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001048 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001049 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001050 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001051 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001052 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001053 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001054}
1055
1056///////////////////////////////////////////////////////////////////////////////
1057
Michael Ludwig7c12e282020-05-29 09:54:07 -04001058void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001059 GrPaint&& paint,
1060 const SkMatrix& viewMatrix,
1061 int spriteCount,
1062 const SkRSXform xform[],
1063 const SkRect texRect[],
1064 const SkColor colors[]) {
1065 ASSERT_SINGLE_OWNER
1066 RETURN_IF_ABANDONED
1067 SkDEBUGCODE(this->validate();)
1068 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1069
1070 AutoCheckFlush acf(this->drawingManager());
1071
1072 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1073 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1074 aaType, spriteCount, xform, texRect, colors);
1075 this->addDrawOp(clip, std::move(op));
1076}
1077
1078///////////////////////////////////////////////////////////////////////////////
1079
Michael Ludwig7c12e282020-05-29 09:54:07 -04001080void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001081 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001082 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001083 const SkMatrix& viewMatrix,
1084 const SkRRect& rrect,
1085 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001086 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001087 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001088 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001089 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001090
1091 const SkStrokeRec& stroke = style.strokeRec();
1092 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001093 return;
1094 }
1095
Michael Ludwig7c12e282020-05-29 09:54:07 -04001096 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001097#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1098 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001099 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001100 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1101 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001102 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1103 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001104 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001105 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1106 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1107 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001108 }
1109#endif
bsalomon6663acf2016-05-10 09:14:17 -07001110 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001111
Robert Phillips72152832017-01-25 17:31:35 -05001112 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001113
Chris Dalton7d6748e2019-03-13 00:34:52 -06001114 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001115
Chris Dalton0dffbab2019-03-27 13:08:50 -06001116 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001117 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1118 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1119 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1120 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1121 // to avoid perf regressions on some platforms.
1122 assert_alive(paint);
1123 op = GrOvalOpFactory::MakeCircularRRectOp(
1124 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1125 }
1126 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001127 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001128 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001129 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001130 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001131 assert_alive(paint);
1132 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001133 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001134 }
1135 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001136 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001137 return;
robertphillipsea461502015-05-26 11:38:03 -07001138 }
robertphillipsb56f9272016-02-25 11:03:52 -08001139
Mike Klein16885072018-12-11 09:54:31 -05001140 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001141 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001142 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001143}
1144
Jim Van Verthc5903412016-11-17 15:27:09 -05001145///////////////////////////////////////////////////////////////////////////////
1146
Jim Van Verth3af1af92017-05-18 15:06:54 -04001147static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1148 SkPoint3 result;
1149 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1150 result.fZ = pt.fZ;
1151 return result;
1152}
1153
Michael Ludwig7c12e282020-05-29 09:54:07 -04001154bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001155 const SkMatrix& viewMatrix,
1156 const SkPath& path,
1157 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001158 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001159 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001160 return true;
1161 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001162 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001163 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001164
1165 // check z plane
1166 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1167 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1168 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1169 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1170 return false;
1171 }
1172
1173 SkRRect rrect;
1174 SkRect rect;
1175 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001176 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001177 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1178 if (!isRRect &&
1179 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1180 rect.width() > SK_ScalarNearlyZero) {
1181 rrect.setOval(rect);
1182 isRRect = true;
1183 }
1184 if (!isRRect && path.isRect(&rect)) {
1185 rrect.setRect(rect);
1186 isRRect = true;
1187 }
1188
1189 if (!isRRect) {
1190 return false;
1191 }
1192
Jim Van Verthc5903412016-11-17 15:27:09 -05001193 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001194 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001195 }
1196
Robert Phillips72152832017-01-25 17:31:35 -05001197 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001198
Jim Van Verth3af1af92017-05-18 15:06:54 -04001199 // transform light
1200 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1201
1202 // 1/scale
1203 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001204 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001205 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1206 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1207
1208 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001209 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1210
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001211 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001212 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1213 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1214 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001215
1216 // Outset the shadow rrect to the border of the penumbra
1217 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1218 SkRRect ambientRRect;
1219 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1220 // If the rrect was an oval then its outset will also be one.
1221 // We set it explicitly to avoid errors.
1222 if (rrect.isOval()) {
1223 ambientRRect = SkRRect::MakeOval(outsetRect);
1224 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001225 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001226 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1227 }
1228
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001229 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001230 if (transparent) {
1231 // set a large inset to force a fill
1232 devSpaceInsetWidth = ambientRRect.width();
1233 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001234
Robert Phillips7c525e62018-06-12 10:11:12 -04001235 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1236 ambientColor,
1237 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001238 ambientRRect,
1239 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001240 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001241 if (op) {
1242 this->addDrawOp(clip, std::move(op));
1243 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001244 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001245
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001246 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001247 SkScalar devSpaceSpotBlur;
1248 SkScalar spotScale;
1249 SkVector spotOffset;
1250 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1251 devLightPos.fZ, rec.fLightRadius,
1252 &devSpaceSpotBlur, &spotScale, &spotOffset);
1253 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1255
Jim Van Verth3af1af92017-05-18 15:06:54 -04001256 // Adjust translate for the effect of the scale.
1257 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1258 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1259 // This offset is in dev space, need to transform it into source space.
1260 SkMatrix ctmInverse;
1261 if (viewMatrix.invert(&ctmInverse)) {
1262 ctmInverse.mapPoints(&spotOffset, 1);
1263 } else {
1264 // Since the matrix is a similarity, this should never happen, but just in case...
1265 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1266 SkASSERT(false);
1267 }
1268
1269 // Compute the transformed shadow rrect
1270 SkRRect spotShadowRRect;
1271 SkMatrix shadowTransform;
1272 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1273 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001274 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001275
1276 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001277 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001278 SkScalar insetWidth = blurOutset;
1279 if (transparent) {
1280 // If transparent, just do a fill
1281 insetWidth += spotShadowRRect.width();
1282 } else {
1283 // For shadows, instead of using a stroke we specify an inset from the penumbra
1284 // border. We want to extend this inset area so that it meets up with the caster
1285 // geometry. The inset geometry will by default already be inset by the blur width.
1286 //
1287 // We compare the min and max corners inset by the radius between the original
1288 // rrect and the shadow rrect. The distance between the two plus the difference
1289 // between the scaled radius and the original radius gives the distance from the
1290 // transformed shadow shape to the original shape in that corner. The max
1291 // of these gives the maximum distance we need to cover.
1292 //
1293 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1294 // that to get the full insetWidth.
1295 SkScalar maxOffset;
1296 if (rrect.isRect()) {
1297 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001298 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001299 rrect.rect().fLeft),
1300 SkTAbs(spotShadowRRect.rect().fTop -
1301 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001302 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001303 rrect.rect().fRight),
1304 SkTAbs(spotShadowRRect.rect().fBottom -
1305 rrect.rect().fBottom)));
1306 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001307 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001308 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1309 rrect.rect().fLeft + dr,
1310 spotShadowRRect.rect().fTop -
1311 rrect.rect().fTop + dr);
1312 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1313 rrect.rect().fRight - dr,
1314 spotShadowRRect.rect().fBottom -
1315 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001316 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001317 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001318 }
Brian Osman788b9162020-02-07 10:36:46 -05001319 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001320 }
1321
1322 // Outset the shadow rrect to the border of the penumbra
1323 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1324 if (spotShadowRRect.isOval()) {
1325 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1326 } else {
1327 SkScalar outsetRad = spotRadius + blurOutset;
1328 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1329 }
1330
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001331 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001332
Robert Phillips7c525e62018-06-12 10:11:12 -04001333 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1334 spotColor,
1335 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001336 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001337 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001338 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001339 if (op) {
1340 this->addDrawOp(clip, std::move(op));
1341 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 }
1343
1344 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001345}
1346
1347///////////////////////////////////////////////////////////////////////////////
1348
Michael Ludwig7c12e282020-05-29 09:54:07 -04001349bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001350 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001351 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001352 const SkMatrix& viewMatrix,
1353 const SkRRect& origOuter,
1354 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001355 SkASSERT(!origInner.isEmpty());
1356 SkASSERT(!origOuter.isEmpty());
1357
Brian Salomon65749212017-12-01 16:01:47 -05001358 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1359
Chris Dalton7d6748e2019-03-13 00:34:52 -06001360 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001361
1362 if (GrAAType::kMSAA == aaType) {
1363 return false;
1364 }
1365
Greg Daniel2655ede2019-04-10 00:49:28 +00001366 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1367 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001368 auto outerR = outer->width() / 2.f;
1369 auto innerR = inner->width() / 2.f;
1370 auto cx = outer->getBounds().fLeft + outerR;
1371 auto cy = outer->getBounds().fTop + outerR;
1372 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1373 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1374 auto avgR = (innerR + outerR) / 2.f;
1375 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1376 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1377 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001378 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001379 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001380 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001381 if (op) {
1382 this->addDrawOp(clip, std::move(op));
1383 return true;
1384 }
Mike Klein16885072018-12-11 09:54:31 -05001385 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001386 }
1387 }
1388
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001389 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001390 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001391 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1392 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001393 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001394 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1395 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001396 }
robertphillips00095892016-02-29 13:50:40 -08001397
robertphillips00095892016-02-29 13:50:40 -08001398 SkMatrix inverseVM;
1399 if (!viewMatrix.isIdentity()) {
1400 if (!origInner.transform(viewMatrix, inner.writable())) {
1401 return false;
1402 }
1403 if (!origOuter.transform(viewMatrix, outer.writable())) {
1404 return false;
1405 }
1406 if (!viewMatrix.invert(&inverseVM)) {
1407 return false;
1408 }
1409 } else {
1410 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001411 }
robertphillips00095892016-02-29 13:50:40 -08001412
Ethan Nicholaseace9352018-10-15 20:09:54 +00001413 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001414 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001415 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001416 if (!innerEffect) {
1417 return false;
1418 }
1419
Ethan Nicholaseace9352018-10-15 20:09:54 +00001420 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001421 if (!outerEffect) {
1422 return false;
1423 }
1424
Brian Salomon82f44312017-01-11 13:42:54 -05001425 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1426 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001427
1428 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001429 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001430 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1431 }
halcanary9d524f22016-03-29 09:03:52 -07001432
Brian Salomon82f44312017-01-11 13:42:54 -05001433 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1434 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001435 return true;
1436}
1437
Michael Ludwig7c12e282020-05-29 09:54:07 -04001438void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001439 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001440 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001441 const SkMatrix& viewMatrix,
1442 const SkRRect& outer,
1443 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001444 ASSERT_SINGLE_OWNER
1445 RETURN_IF_ABANDONED
1446 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001447 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001448
1449 SkASSERT(!outer.isEmpty());
1450 SkASSERT(!inner.isEmpty());
1451
Robert Phillips72152832017-01-25 17:31:35 -05001452 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001453
Brian Salomon82f44312017-01-11 13:42:54 -05001454 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001455 return;
1456 }
Mike Klein16885072018-12-11 09:54:31 -05001457 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001458
1459 SkPath path;
1460 path.setIsVolatile(true);
1461 path.addRRect(inner);
1462 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001463 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001464 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001465}
1466
robertphillipsea461502015-05-26 11:38:03 -07001467///////////////////////////////////////////////////////////////////////////////
1468
Michael Ludwig7c12e282020-05-29 09:54:07 -04001469void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001470 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001471 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001472 const SkMatrix& viewMatrix,
1473 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001474 const GrStyle& style,
1475 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001476 ASSERT_SINGLE_OWNER
1477 RETURN_IF_ABANDONED
1478 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001479 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001480
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001481 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001482 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001483 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001484 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001485 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1486 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001487 aa = GrAA::kNo;
1488 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001489 }
msarettcc319b92016-08-25 18:07:18 -07001490 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001491 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001492 SkPath path;
1493 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001494 path.setIsVolatile(true);
1495
Brian Salomon82f44312017-01-11 13:42:54 -05001496 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001497 }
1498
Chris Dalton7d6748e2019-03-13 00:34:52 -06001499 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001500 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1501 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001502 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001503}
1504
Michael Ludwig7c12e282020-05-29 09:54:07 -04001505void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001506 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001507 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001508 const SkMatrix& viewMatrix,
1509 const SkRect& oval,
1510 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001511 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001512 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001513 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001514 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001515
Robert Phillips7484d202018-07-03 09:09:08 -04001516 const SkStrokeRec& stroke = style.strokeRec();
1517
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001518 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001519 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1520 return;
1521 }
1522
1523 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001524 return;
robertphillipsea461502015-05-26 11:38:03 -07001525 }
1526
Robert Phillips72152832017-01-25 17:31:35 -05001527 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001528
Chris Dalton7d6748e2019-03-13 00:34:52 -06001529 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001530
1531 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001532 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1533 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001534 // We don't draw true circles as round rects in coverage mode, because it can
1535 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1536 assert_alive(paint);
1537 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1538 this->caps()->shaderCaps());
1539 }
1540 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001541 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1542 // the arc equation. This same special geometry and fragment branch also turn out to be a
1543 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1544 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1545 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001546 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001547 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1548 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001549 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001550 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001551 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001552 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1553 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001554 }
1555 if (op) {
1556 this->addDrawOp(clip, std::move(op));
1557 return;
robertphillipsea461502015-05-26 11:38:03 -07001558 }
robertphillipsb56f9272016-02-25 11:03:52 -08001559
Mike Klein16885072018-12-11 09:54:31 -05001560 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001561 this->drawShapeUsingPathRenderer(
1562 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001563 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001564}
1565
Michael Ludwig7c12e282020-05-29 09:54:07 -04001566void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001567 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001568 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001569 const SkMatrix& viewMatrix,
1570 const SkRect& oval,
1571 SkScalar startAngle,
1572 SkScalar sweepAngle,
1573 bool useCenter,
1574 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001575 ASSERT_SINGLE_OWNER
1576 RETURN_IF_ABANDONED
1577 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001578 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001579
1580 AutoCheckFlush acf(this->drawingManager());
1581
Chris Dalton7d6748e2019-03-13 00:34:52 -06001582 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001583 if (GrAAType::kCoverage == aaType) {
1584 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1585 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1586 std::move(paint),
1587 viewMatrix,
1588 oval,
1589 startAngle,
1590 sweepAngle,
1591 useCenter,
1592 style,
1593 shaderCaps);
1594 if (op) {
1595 this->addDrawOp(clip, std::move(op));
1596 return;
1597 }
1598 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001599 }
Brian Salomone4949402018-04-26 15:22:04 -04001600 this->drawShapeUsingPathRenderer(
1601 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001602 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001603}
1604
Michael Ludwig7c12e282020-05-29 09:54:07 -04001605void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001606 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001607 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001608 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001609 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001610 sk_sp<GrColorSpaceXform> csxf,
1611 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001612 std::unique_ptr<SkLatticeIter> iter,
1613 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001614 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001615 RETURN_IF_ABANDONED
1616 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001617 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001618
Robert Phillips72152832017-01-25 17:31:35 -05001619 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001620
Brian Salomon2a943df2018-05-04 13:43:19 -04001621 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001622 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001623 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001624 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001625}
1626
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001627void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1628 const SkRect& bounds) {
1629 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1630 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001631 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001632}
1633
Brian Salomon031b0ba2019-05-23 11:05:26 -04001634void GrRenderTargetContext::asyncRescaleAndReadPixels(
1635 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1636 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1637 auto direct = fContext->priv().asDirectContext();
1638 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001639 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001640 return;
1641 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001642 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001643 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001644 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001645 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001646 if (this->asRenderTargetProxy()->framebufferOnly()) {
1647 callback(context, nullptr);
1648 return;
1649 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001650 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001651 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001652 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001653 return;
1654 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001655 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001656 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001657 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001658 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001659 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001660 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1661 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001662 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001663 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001664 backendFormatOfFinalContext, dstCT);
1665 // Fail if we can't read from the source surface's color type.
1666 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001667 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001668 return;
1669 }
Brian Salomon624f9062019-07-02 14:23:00 -04001670 // Fail if read color type does not have all of dstCT's color channels and those missing color
1671 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001672 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1673 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1674 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1675 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001676 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001677 return;
1678 }
1679
Brian Salomonbf6b9792019-08-21 09:38:10 -04001680 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001681 int x = srcRect.fLeft;
1682 int y = srcRect.fTop;
1683 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001684 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1685 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001686 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001687 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001688 return;
1689 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001690 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001691 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001692 x = y = 0;
1693 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001694 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1695 this->colorInfo().alphaType(),
1696 info.colorSpace(),
1697 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001698 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1699 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001700 // We flip or color convert by drawing and we don't currently support drawing to
1701 // kPremul.
1702 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001703 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001704 return;
1705 }
Greg Daniel40903af2020-01-30 14:55:05 -05001706 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001707 SkRect srcRectToDraw = SkRect::Make(srcRect);
1708 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001709 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001710 texProxyView =
1711 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1712 SkBackingFit::kApprox, SkBudgeted::kNo);
1713 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001714 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001715 return;
1716 }
Brian Salomonc5243782020-04-02 12:50:34 -04001717 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001718 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1719 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001720 tempRTC = GrRenderTargetContext::Make(
1721 direct, this->colorInfo().colorType(), info.refColorSpace(),
1722 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001723 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001724 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001725 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001726 return;
1727 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001728 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001729 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1730 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001731 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1732 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1733 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001734 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 }
1736 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001737 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001738 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1739 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001740}
1741
Brian Salomon9241a6d2019-10-03 13:26:54 -04001742class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1743public:
1744 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1745 ~AsyncReadResult() override {
1746 for (int i = 0; i < fPlanes.count(); ++i) {
1747 if (!fPlanes[i].fMappedBuffer) {
1748 delete[] static_cast<const char*>(fPlanes[i].fData);
1749 } else {
1750 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1751 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1752 }
1753 }
1754 }
1755
1756 int count() const override { return fPlanes.count(); }
1757 const void* data(int i) const override { return fPlanes[i].fData; }
1758 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1759
1760 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001761 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001762 size_t rowBytes,
1763 GrClientMappedBufferManager* manager) {
1764 SkASSERT(!result.fTransferBuffer->isMapped());
1765 const void* mappedData = result.fTransferBuffer->map();
1766 if (!mappedData) {
1767 return false;
1768 }
1769 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001770 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001771 result.fPixelConverter(convertedData.get(), mappedData);
1772 this->addCpuPlane(std::move(convertedData), rowBytes);
1773 result.fTransferBuffer->unmap();
1774 } else {
1775 manager->insert(result.fTransferBuffer);
1776 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1777 }
1778 return true;
1779 }
1780
1781 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1782 SkASSERT(data);
1783 SkASSERT(rowBytes > 0);
1784 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1785 }
1786
1787private:
1788 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1789 SkASSERT(data);
1790 SkASSERT(rowBytes > 0);
1791 SkASSERT(mappedBuffer);
1792 SkASSERT(mappedBuffer->isMapped());
1793 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1794 }
1795
1796 struct Plane {
1797 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1798 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1799 const void* fData;
1800 size_t fRowBytes;
1801 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1802 sk_sp<GrGpuBuffer> fMappedBuffer;
1803 };
1804 SkSTArray<3, Plane> fPlanes;
1805 uint32_t fInboxID;
1806};
1807
Brian Salomon024bd002019-06-11 11:38:16 -04001808void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1809 ReadPixelsCallback callback,
1810 ReadPixelsContext context) {
1811 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1812 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1813
Greg Danielca9dbe22020-01-02 13:44:30 -05001814 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001815 callback(context, nullptr);
1816 return;
1817 }
1818
Brian Salomon9241a6d2019-10-03 13:26:54 -04001819 auto directContext = fContext->priv().asDirectContext();
1820 SkASSERT(directContext);
1821 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1822
Brian Salomon024bd002019-06-11 11:38:16 -04001823 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1824
1825 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001826 auto ii = SkImageInfo::Make(rect.size(), colorType,
1827 this->colorInfo().alphaType(),
1828 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001829 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001830 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1831 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1832 result->addCpuPlane(std::move(data), pm.rowBytes());
1833
Brian Salomon1d435302019-07-01 13:05:28 -04001834 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001835 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001836 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001837 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001838 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001839 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001840 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001841
Brian Salomonab32f652019-05-10 14:24:50 -04001842 struct FinishContext {
1843 ReadPixelsCallback* fClientCallback;
1844 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001845 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001846 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001847 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001848 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001849 };
1850 // Assumption is that the caller would like to flush. We could take a parameter or require an
1851 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1852 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001853 auto* finishContext = new FinishContext{callback,
1854 context,
1855 rect.size(),
1856 colorType,
1857 mappedBufferManager,
1858 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001859 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001860 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001861 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001862 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1863 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1864 context->fMappedBufferManager)) {
1865 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001866 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001867 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001868 delete context;
1869 };
1870 GrFlushInfo flushInfo;
1871 flushInfo.fFinishedContext = finishContext;
1872 flushInfo.fFinishedProc = finishCallback;
1873 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1874}
1875
Brian Salomon9241a6d2019-10-03 13:26:54 -04001876void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1877 sk_sp<SkColorSpace> dstColorSpace,
1878 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001879 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001880 RescaleGamma rescaleGamma,
1881 SkFilterQuality rescaleQuality,
1882 ReadPixelsCallback callback,
1883 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001884 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1885 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001886 SkASSERT(!dstSize.isZero());
1887 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1888
Brian Salomon024bd002019-06-11 11:38:16 -04001889 auto direct = fContext->priv().asDirectContext();
1890 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001891 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001892 return;
1893 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001894 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001896 return;
1897 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001898 if (this->asRenderTargetProxy()->framebufferOnly()) {
1899 callback(context, nullptr);
1900 return;
1901 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001902 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001903 callback(context, nullptr);
1904 return;
1905 }
Brian Salomon024bd002019-06-11 11:38:16 -04001906 int x = srcRect.fLeft;
1907 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001908 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001909 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001910 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001911 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001912 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001913 dstColorSpace);
1914 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001915 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1916 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001917 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001918 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001919 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001920 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001921 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001922 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001923 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001924 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001925 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001926 srcView = this->readSurfaceView();
1927 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001928 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1929 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1930 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001931 // If we can't get a texture copy of the contents then give up.
1932 callback(context, nullptr);
1933 return;
1934 }
Brian Salomonc5243782020-04-02 12:50:34 -04001935 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001936 x = y = 0;
1937 }
Brian Salomon4d036892019-07-02 15:10:58 -04001938 // We assume the caller wants kPremul. There is no way to indicate a preference.
1939 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001940 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1941 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001942 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001943 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1944 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001945 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1946 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001947 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001948 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001949 return;
1950 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001951 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001952 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1953 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1954 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1955 SkMatrix::I(), std::move(xform));
1956 srcView = tempRTC->readSurfaceView();
1957 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001958 x = y = 0;
1959 }
1960 }
Greg Danielc594e622019-10-15 14:01:49 -04001961
Greg Daniele20fcad2020-01-08 11:52:34 -05001962 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1963 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1964 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001965 int halfW = dstSize.width()/2;
1966 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001967 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1968 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1969 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1970 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1971 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1972 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001973 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001974 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001975 return;
1976 }
1977
Brian Osman28ba5282019-10-30 14:18:07 -04001978 float baseM[20];
1979 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1980
Brian Salomon024bd002019-06-11 11:38:16 -04001981 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1982
Mike Reed1f607332020-05-21 12:11:27 -04001983 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04001984
Brian Salomon9241a6d2019-10-03 13:26:54 -04001985 SkRect dstRectY = SkRect::Make(dstSize);
1986 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00001987
Brian Salomon6aa65052020-01-28 12:16:53 -05001988 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1989 PixelTransferResult yTransfer, uTransfer, vTransfer;
1990
Brian Salomon024bd002019-06-11 11:38:16 -04001991 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1992 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001993 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001994 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001995 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04001996 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
1997 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
1998 /*unpremulInput=*/false,
1999 /*clampRGBOutput=*/true,
2000 /*premulOutput=*/false);
2001 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002002 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002003 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002004 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002005 if (!doSynchronousRead) {
2006 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2007 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2008 if (!yTransfer.fTransferBuffer) {
2009 callback(context, nullptr);
2010 return;
2011 }
Brian Salomon024bd002019-06-11 11:38:16 -04002012 }
2013
2014 texMatrix.preScale(2.f, 2.f);
2015 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2016 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002017 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002018 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002019 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002020 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2021 GrSamplerState::Filter::kBilerp);
2022 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2023 /*unpremulInput=*/false,
2024 /*clampRGBOutput=*/true,
2025 /*premulOutput=*/false);
2026 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002027 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002028 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002029 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002030 if (!doSynchronousRead) {
2031 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2032 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2033 if (!uTransfer.fTransferBuffer) {
2034 callback(context, nullptr);
2035 return;
2036 }
Brian Salomon024bd002019-06-11 11:38:16 -04002037 }
2038
2039 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2040 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002041 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002042 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002043 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002044 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2045 texMatrix, GrSamplerState::Filter::kBilerp);
2046 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2047 /*unpremulInput=*/false,
2048 /*clampRGBOutput=*/true,
2049 /*premulOutput=*/false);
2050 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002051 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002052 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002053 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002054 if (!doSynchronousRead) {
2055 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2056 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2057 if (!vTransfer.fTransferBuffer) {
2058 callback(context, nullptr);
2059 return;
2060 }
2061 }
2062
2063 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002064 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2065 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2066 size_t yRB = yInfo.minRowBytes();
2067 size_t uvRB = uvInfo.minRowBytes();
2068 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2069 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2070 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2071 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2072 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2073 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002074 callback(context, nullptr);
2075 return;
2076 }
2077 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002078 result->addCpuPlane(std::move(y), yRB );
2079 result->addCpuPlane(std::move(u), uvRB);
2080 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002081 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002082 return;
2083 }
2084
2085 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002086 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002087 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002088 GrClientMappedBufferManager* fMappedBufferManager;
2089 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002090 PixelTransferResult fYTransfer;
2091 PixelTransferResult fUTransfer;
2092 PixelTransferResult fVTransfer;
2093 };
2094 // Assumption is that the caller would like to flush. We could take a parameter or require an
2095 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2096 // callback to GrGpu until after the next flush that flushes our op list, though.
2097 auto* finishContext = new FinishContext{callback,
2098 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002099 direct->priv().clientMappedBufferManager(),
2100 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002101 std::move(yTransfer),
2102 std::move(uTransfer),
2103 std::move(vTransfer)};
2104 auto finishCallback = [](GrGpuFinishedContext c) {
2105 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002106 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002107 auto manager = context->fMappedBufferManager;
2108 size_t rowBytes = SkToSizeT(context->fSize.width());
2109 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2110 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002111 delete context;
2112 return;
2113 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002114 rowBytes /= 2;
2115 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2116 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2117 (*context->fClientCallback)(context->fClientContext, nullptr);
2118 delete context;
2119 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002120 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002121 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2122 (*context->fClientCallback)(context->fClientContext, nullptr);
2123 delete context;
2124 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002125 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002126 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002127 delete context;
2128 };
2129 GrFlushInfo flushInfo;
2130 flushInfo.fFinishedContext = finishContext;
2131 flushInfo.fFinishedProc = finishCallback;
2132 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002133}
2134
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002135GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2136 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002137 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002138 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002139 if (info.fSubmittedProc) {
2140 info.fSubmittedProc(info.fSubmittedContext, false);
2141 }
2142 if (info.fFinishedProc) {
2143 info.fFinishedProc(info.fFinishedContext);
2144 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002145 return GrSemaphoresSubmitted::kNo;
2146 }
robertphillips8c523e02016-07-26 07:41:00 -07002147 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002148 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002149
Greg Daniel55f040b2020-02-13 15:38:32 +00002150 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002151}
2152
Greg Danielc64ee462017-06-15 16:59:49 -04002153bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002154 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002155 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002156 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002157 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002158 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002159
2160 AutoCheckFlush acf(this->drawingManager());
2161
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002162 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002163 return false;
2164 }
2165
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002166 auto direct = fContext->priv().asDirectContext();
2167 if (!direct) {
2168 return false;
2169 }
2170
2171 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002172
Greg Daniel301015c2019-11-18 14:06:46 -05002173 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2174 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002175 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002176 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002177 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2178 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002179 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002180 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2181 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002182 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002183}
joshualitt33a5fce2015-11-18 13:28:51 -08002184
Michael Ludwig7c12e282020-05-29 09:54:07 -04002185void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002186 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002187 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002188 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002189 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002190 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002191 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002192 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002193 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002194 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2195
Michael Ludwig2686d692020-04-17 20:21:37 +00002196 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002197
Robert Phillips27927a52018-08-20 13:18:12 -04002198 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002199}
2200
Michael Ludwig7c12e282020-05-29 09:54:07 -04002201void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002202 GrPaint&& paint,
2203 GrAA aa,
2204 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002205 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002206 ASSERT_SINGLE_OWNER
2207 RETURN_IF_ABANDONED
2208 SkDEBUGCODE(this->validate();)
2209 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2210
Brian Salomon2fad74a2017-12-20 13:28:55 -05002211 if (shape.isEmpty()) {
2212 if (shape.inverseFilled()) {
2213 this->drawPaint(clip, std::move(paint), viewMatrix);
2214 }
2215 return;
robertphillipsea461502015-05-26 11:38:03 -07002216 }
2217
Robert Phillips72152832017-01-25 17:31:35 -05002218 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002219
Brian Salomon2fad74a2017-12-20 13:28:55 -05002220 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002221 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002222 SkRRect rrect;
2223 // We can ignore the starting point and direction since there is no path effect.
2224 bool inverted;
2225 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2226 if (rrect.isRect()) {
2227 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2228 &shape.style());
2229 return;
2230 } else if (rrect.isOval()) {
2231 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002232 return;
2233 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002234 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2235 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002236 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2237 viewMatrix.rectStaysRect()) {
2238 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2239 // the matrix to all the points individually rather than just to the rect
2240 SkRect rects[2];
2241 if (shape.asNestedRects(rects)) {
2242 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002243 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002244 fContext, std::move(paint), viewMatrix, rects);
2245 if (op) {
2246 this->addDrawOp(clip, std::move(op));
2247 }
2248 // Returning here indicates that there is nothing to draw in this case.
2249 return;
2250 }
robertphillipsea461502015-05-26 11:38:03 -07002251 }
2252 }
robertphillips4bc31812016-03-01 12:22:49 -08002253
Michael Ludwig6a6de652020-04-30 20:16:36 -04002254 // If we get here in drawShape(), we definitely need to use path rendering
2255 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2256 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002257}
2258
Michael Ludwig7c12e282020-05-29 09:54:07 -04002259bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002260 const GrUserStencilSettings* ss,
2261 SkRegion::Op op,
2262 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002263 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002264 const SkMatrix& viewMatrix,
2265 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002266 ASSERT_SINGLE_OWNER_PRIV
2267 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002268 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002269 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2270 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002271
2272 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002273 GrPaint paint;
2274 paint.setCoverageSetOpXPFactory(op, invert);
2275 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2276 SkRect::MakeIWH(fRenderTargetContext->width(),
2277 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002278 return true;
2279 }
2280
Robert Phillips72152832017-01-25 17:31:35 -05002281 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002282
2283 // An Assumption here is that path renderer would use some form of tweaking
2284 // the src color (either the input alpha or in the frag shader) to implement
2285 // aa. If we have some future driver-mojo path AA that can do the right
2286 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002287 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002288 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002289
Michael Ludwig7c12e282020-05-29 09:54:07 -04002290 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002291
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002292 GrPaint paint;
2293 paint.setCoverageSetOpXPFactory(op, invert);
2294
Michael Ludwig2686d692020-04-17 20:21:37 +00002295 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002296 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002297 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002298 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002299 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002300 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002301 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002302 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002303 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002304 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2305 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002306 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002307
2308 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002309 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002310 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002311 if (!pr) {
2312 return false;
2313 }
2314
Brian Salomonf3569f02017-10-24 12:52:33 -04002315 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2316 std::move(paint),
2317 ss,
2318 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002319 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002320 &clipConservativeBounds,
2321 &viewMatrix,
2322 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002323 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002324 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002325 pr->drawPath(args);
2326 return true;
2327}
2328
Brian Osman11052242016-10-27 14:47:55 -04002329SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002330 ASSERT_SINGLE_OWNER_PRIV
2331
Robert Phillips6a6de562019-02-15 15:19:15 -05002332 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002333 return SkBudgeted::kNo;
2334 }
2335
Brian Osman11052242016-10-27 14:47:55 -04002336 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002337
Greg Daniel3912a4b2020-01-14 09:56:04 -05002338 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002339}
2340
Michael Ludwig7c12e282020-05-29 09:54:07 -04002341void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002342 GrPaint&& paint,
2343 GrAA aa,
2344 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002345 const GrStyledShape& originalShape,
2346 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002347 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002348 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002349 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2350
Jim Van Verthf86073a2018-10-02 13:05:38 -04002351 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2352 return;
2353 }
2354
Michael Ludwig6a6de652020-04-30 20:16:36 -04002355 if (attemptShapeFallback && originalShape.simplified()) {
2356 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2357 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2358 // we ought to try again instead of going right to path rendering.
2359 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2360 return;
2361 }
2362
Michael Ludwig7c12e282020-05-29 09:54:07 -04002363 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002364
Michael Ludwig2686d692020-04-17 20:21:37 +00002365 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002366 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002367
robertphillips68737822015-10-29 12:12:21 -07002368 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002369 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002370 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002371 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002372 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002373 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002374 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002375 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002376 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002377
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002378 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002379 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002380 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002381 return;
2382 }
2383
Chris Dalton6ce447a2019-06-23 18:07:38 -06002384 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002385
2386 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2387 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2388 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2389
Brian Salomon2fad74a2017-12-20 13:28:55 -05002390 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002391 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002392 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2393 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002394 return;
2395 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002396 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002397 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002398 }
2399 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002400 if (canDrawArgs.fShape->style().applies()) {
2401 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2402 styleScale);
2403 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002404 return;
2405 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002406 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002407 // This time, allow SW renderer
2408 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2409 } else {
2410 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002411#if GR_PATH_RENDERER_SPEW
2412 SkDebugf("falling back to: %s\n", pr->name());
2413#endif
bsalomon6663acf2016-05-10 09:14:17 -07002414 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002415 }
robertphillipsea461502015-05-26 11:38:03 -07002416
bsalomon8acedde2016-06-24 10:42:16 -07002417 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002418#ifdef SK_DEBUG
2419 SkDebugf("Unable to find path renderer compatible with path.\n");
2420#endif
2421 return;
2422 }
2423
Robert Phillips256c37b2017-03-01 14:32:46 -05002424 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002425 std::move(paint),
2426 &GrUserStencilSettings::kUnused,
2427 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002428 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002429 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002430 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002431 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002432 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002433 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002434 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002435}
2436
Brian Salomon467921e2017-03-06 16:17:12 -05002437static void op_bounds(SkRect* bounds, const GrOp* op) {
2438 *bounds = op->bounds();
2439 if (op->hasZeroArea()) {
2440 if (op->hasAABloat()) {
2441 bounds->outset(0.5f, 0.5f);
2442 } else {
2443 // We don't know which way the particular GPU will snap lines or points at integer
2444 // coords. So we ensure that the bounds is large enough for either snap.
2445 SkRect before = *bounds;
2446 bounds->roundOut(bounds);
2447 if (bounds->fLeft == before.fLeft) {
2448 bounds->fLeft -= 1;
2449 }
2450 if (bounds->fTop == before.fTop) {
2451 bounds->fTop -= 1;
2452 }
2453 if (bounds->fRight == before.fRight) {
2454 bounds->fRight += 1;
2455 }
2456 if (bounds->fBottom == before.fBottom) {
2457 bounds->fBottom += 1;
2458 }
2459 }
2460 }
2461}
2462
Chris Dalton08755122019-08-05 16:13:47 -06002463void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002464 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002465 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2466}
2467
Michael Ludwig7c12e282020-05-29 09:54:07 -04002468void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002469 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002470 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002471 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002472 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002473 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002474 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002475 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002476 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002477 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002478
Brian Salomon467921e2017-03-06 16:17:12 -05002479 // Setup clip
2480 SkRect bounds;
2481 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002482 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002483 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002484 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002485 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002486
Chris Dalton9acf6822019-11-12 11:52:40 -07002487 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002488 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002489 }
2490
Michael Ludwig7c12e282020-05-29 09:54:07 -04002491 bool skipDraw = false;
2492 if (clip) {
2493 // Have a complex clip, so defer to its early clip culling
2494 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2495 skipDraw = true;
2496 }
2497 } else {
2498 // No clipping, so just clip the bounds against the logical render target dimensions
2499 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2500 skipDraw = true;
2501 }
2502 }
2503
2504 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002505 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002506 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002507 }
2508
Chris Dalton9acf6822019-11-12 11:52:40 -07002509 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2510 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2511
2512 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2513 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2514 // that uses a stencil test when the stencil buffer is multisampled.)
2515 bool hasMixedSampledCoverage = (
2516 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002517 SkASSERT(!hasMixedSampledCoverage ||
2518 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002519
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002520 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002521 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002522 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2523
Greg Daniel524e28b2019-11-01 11:48:53 -04002524 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002525 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002526 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002527 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002528 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002529 }
2530 }
2531
2532 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002533 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002534 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002535 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002536 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002537 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002538 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002539}
2540
Michael Ludwig7c12e282020-05-29 09:54:07 -04002541bool GrRenderTargetContext::setupDstProxyView(const GrClip* clip, const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002542 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002543 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2544 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2545 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002546 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002547 return false;
2548 }
2549
Greg Daniel3912a4b2020-01-14 09:56:04 -05002550 if (this->caps()->textureBarrierSupport() &&
2551 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002552 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002553 // The render target is a texture, so we can read from it directly in the shader. The XP
2554 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002555 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002556 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002557 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002558 }
2559 }
2560
Greg Daniel3912a4b2020-01-14 09:56:04 -05002561 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002562
Michael Ludwig7c12e282020-05-29 09:54:07 -04002563 SkIRect clippedRect = get_clip_bounds(this, clip);
Brian Salomon09181ef2018-11-14 13:39:51 -05002564 SkRect opBounds = op.bounds();
2565 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2566 // 0.5 pixels.
2567 if (op.hasAABloat() || op.hasZeroArea()) {
2568 opBounds.outset(0.5f, 0.5f);
2569 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2570 // performance we may ignore the clip when the draw is entirely inside the clip is float
2571 // space but will hit pixels just outside the clip when actually rasterizing.
2572 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002573 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002574 }
2575 SkIRect opIBounds;
2576 opBounds.roundOut(&opIBounds);
2577 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002578#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002579 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002580#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002581 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002582 }
2583
Greg Daniel3155f7f2020-01-16 16:54:26 -05002584 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002585 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2586 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002587 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002588 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002589
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002590 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002591 copyRect = clippedRect;
2592 }
Brian Salomon467921e2017-03-06 16:17:12 -05002593
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002594 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002595 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002596 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002597 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002598 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002599 } else {
Eric Karl74480882017-04-03 14:49:05 -07002600 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002601 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002602 }
Brian Salomonc5243782020-04-02 12:50:34 -04002603 auto copy =
2604 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2605 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2606 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002607
Brian Salomonc5243782020-04-02 12:50:34 -04002608 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002609 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002610 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002611}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002612
Greg Daniel573312e2020-02-07 17:22:35 -05002613bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002614 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002615 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002616 SkIRect clippedSrcRect;
2617 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002618 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2619 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002620 return false;
2621 }
2622
2623 GrPaint paint;
2624 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002625
Greg Danield2ccbb52020-02-05 10:45:39 -05002626 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002627 if (!fp) {
2628 return false;
2629 }
2630 paint.addColorFragmentProcessor(std::move(fp));
2631
2632 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002633 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002634 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2635 clippedSrcRect.height()),
2636 SkRect::Make(clippedSrcRect));
2637 return true;
2638}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002639
2640void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2641 SkASSERT(&task == fOpsTask.get());
2642 fOpsTask.reset();
2643}