blob: 2a70205db91aaa2828fbb3dd835a5fde4020070f [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 Derbyd29207a2020-06-08 13:50:19 -040080 , fRenderTargetContext(renderTargetContext) {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040081
Michael Ludwig7c12e282020-05-29 09:54:07 -040082 void addDrawOp(const GrClip* clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040083 fRenderTargetContext->addDrawOp(clip, std::move(op));
84 }
85
Michael Ludwig7c12e282020-05-29 09:54:07 -040086 void drawShape(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -040087 const SkPaint& paint,
88 const SkMatrixProvider& matrixProvider,
89 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040090 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040091 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040092 }
93
Brian Osman449b1152020-04-15 16:43:00 -040094 void makeGrPaint(GrMaskFormat maskFormat,
95 const SkPaint& skPaint,
96 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040097 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050098 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040099 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400100 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400101 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
102 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400103 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400104 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400105 }
106 }
107
Robert Phillips69893702019-02-22 11:16:30 -0500108 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400109 return fRenderTargetContext->fContext;
110 }
111
Herb Derby65956872018-08-21 16:55:04 -0400112 SkGlyphRunListPainter* glyphPainter() override {
Herb Derbyd29207a2020-06-08 13:50:19 -0400113 return fRenderTargetContext->glyphPainter();
Herb Derby74c6ed32018-07-28 18:07:54 -0400114 }
115
Brian Salomonf18b1d82017-10-27 11:30:49 -0400116private:
117 GrRenderTargetContext* fRenderTargetContext;
118};
joshualittbc907352016-01-13 06:45:40 -0800119
Robert Phillips72152832017-01-25 17:31:35 -0500120#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -0400121#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
122#define ASSERT_SINGLE_OWNER_PRIV GR_ASSERT_SINGLE_OWNER(fRenderTargetContext->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -0500123#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
124#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
125#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
126#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
127#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700128
Brian Salomone225b562017-06-14 13:00:03 -0400129//////////////////////////////////////////////////////////////////////////////
130
robertphillipsea461502015-05-26 11:38:03 -0700131class AutoCheckFlush {
132public:
halcanary9d524f22016-03-29 09:03:52 -0700133 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700134 SkASSERT(fDrawingManager);
135 }
bsalomonb77a9072016-09-07 10:02:04 -0700136 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700137
138private:
robertphillips77a2e522015-10-17 07:43:27 -0700139 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700140};
141
Greg Daniele20fcad2020-01-08 11:52:34 -0500142std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
143 GrRecordingContext* context,
144 GrColorType colorType,
145 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500146 sk_sp<GrSurfaceProxy> proxy,
147 GrSurfaceOrigin origin,
148 const SkSurfaceProps* surfaceProps,
149 bool managedOps) {
150 if (!proxy) {
151 return nullptr;
152 }
153
154 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400155 GrSwizzle readSwizzle, writeSwizzle;
156 if (colorType != GrColorType::kUnknown) {
157 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
158 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
159 }
Greg Danielba0ff782020-01-07 15:42:57 -0500160
Greg Daniel3912a4b2020-01-14 09:56:04 -0500161 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400162 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500163
164 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400165 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500166 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500167}
168
169std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
170 GrRecordingContext* context,
171 GrColorType colorType,
172 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500173 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500174 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500175 const GrBackendFormat& format,
176 int sampleCnt,
177 GrMipMapped mipMapped,
178 GrProtected isProtected,
179 GrSurfaceOrigin origin,
180 SkBudgeted budgeted,
181 const SkSurfaceProps* surfaceProps) {
182 // It is probably not necessary to check if the context is abandoned here since uses of the
183 // GrRenderTargetContext which need the context will mostly likely fail later on without an
184 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
185 // an abandoned context correctly. It also lets us early out of some extra work.
186 if (context->priv().abandoned()) {
187 return nullptr;
188 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500189
Greg Daniele20fcad2020-01-08 11:52:34 -0500190 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400191 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500192 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 if (!proxy) {
194 return nullptr;
195 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500196
Greg Danielba0ff782020-01-07 15:42:57 -0500197 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
198 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500199 if (!rtc) {
200 return nullptr;
201 }
202 rtc->discard();
203 return rtc;
204}
205
206std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
207 GrRecordingContext* context,
208 GrColorType colorType,
209 sk_sp<SkColorSpace> colorSpace,
210 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500211 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500212 int sampleCnt,
213 GrMipMapped mipMapped,
214 GrProtected isProtected,
215 GrSurfaceOrigin origin,
216 SkBudgeted budgeted,
217 const SkSurfaceProps* surfaceProps) {
218 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
219 if (!format.isValid()) {
220 return nullptr;
221 }
222
223 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
224 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
225 surfaceProps);
226}
227
228static inline GrColorType color_type_fallback(GrColorType ct) {
229 switch (ct) {
230 // kRGBA_8888 is our default fallback for many color types that may not have renderable
231 // backend formats.
232 case GrColorType::kAlpha_8:
233 case GrColorType::kBGR_565:
234 case GrColorType::kABGR_4444:
235 case GrColorType::kBGRA_8888:
236 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400237 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500238 case GrColorType::kRGBA_F16:
239 case GrColorType::kRGBA_F16_Clamped:
240 return GrColorType::kRGBA_8888;
241 case GrColorType::kAlpha_F16:
242 return GrColorType::kRGBA_F16;
243 case GrColorType::kGray_8:
244 return GrColorType::kRGB_888x;
245 default:
246 return GrColorType::kUnknown;
247 }
248}
249
Brian Salomond005b692020-04-01 15:47:05 -0400250std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400251 GrImageContext* context, GrColorType colorType, int sampleCnt) {
252 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400253 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400254 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
255 // We continue to the fallback color type if there no default renderable format or we
256 // requested msaa and the format doesn't support msaa.
257 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400258 return {colorType, format};
259 }
260 colorType = color_type_fallback(colorType);
261 } while (colorType != GrColorType::kUnknown);
262 return {GrColorType::kUnknown, {}};
263}
264
Greg Daniele20fcad2020-01-08 11:52:34 -0500265std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
266 GrRecordingContext* context,
267 GrColorType colorType,
268 sk_sp<SkColorSpace> colorSpace,
269 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500270 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500271 int sampleCnt,
272 GrMipMapped mipMapped,
273 GrProtected isProtected,
274 GrSurfaceOrigin origin,
275 SkBudgeted budgeted,
276 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400277 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400278 if (ct == GrColorType::kUnknown) {
279 return nullptr;
280 }
281 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
282 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500283}
284
Greg Danielba0ff782020-01-07 15:42:57 -0500285std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
286 GrRecordingContext* context,
287 GrColorType colorType,
288 sk_sp<SkColorSpace> colorSpace,
289 const GrBackendTexture& tex,
290 int sampleCnt,
291 GrSurfaceOrigin origin,
292 const SkSurfaceProps* surfaceProps,
293 ReleaseProc releaseProc,
294 ReleaseContext releaseCtx) {
295 SkASSERT(sampleCnt > 0);
296 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400297 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500298 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500299 if (!proxy) {
300 return nullptr;
301 }
302
303 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
304 origin, surfaceProps);
305}
306
307std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
308 GrRecordingContext* context,
309 GrColorType colorType,
310 sk_sp<SkColorSpace> colorSpace,
311 const GrBackendTexture& tex,
312 int sampleCnt,
313 GrSurfaceOrigin origin,
314 const SkSurfaceProps* surfaceProps) {
315 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400316 sk_sp<GrSurfaceProxy> proxy(
317 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500318 if (!proxy) {
319 return nullptr;
320 }
321
322 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
323 origin, surfaceProps);
324}
325
326std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
327 GrRecordingContext* context,
328 GrColorType colorType,
329 sk_sp<SkColorSpace> colorSpace,
330 const GrBackendRenderTarget& rt,
331 GrSurfaceOrigin origin,
332 const SkSurfaceProps* surfaceProps,
333 ReleaseProc releaseProc,
334 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400335 sk_sp<GrSurfaceProxy> proxy(
336 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500337 if (!proxy) {
338 return nullptr;
339 }
340
341 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
342 origin, surfaceProps);
343}
344
345std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
346 GrRecordingContext* context,
347 const SkImageInfo& imageInfo,
348 const GrVkDrawableInfo& vkInfo,
349 const SkSurfaceProps* props) {
350 sk_sp<GrSurfaceProxy> proxy(
351 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
352 vkInfo));
353 if (!proxy) {
354 return nullptr;
355 }
356
Greg Danielba0ff782020-01-07 15:42:57 -0500357 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
358 imageInfo.refColorSpace(), std::move(proxy),
359 kTopLeft_GrSurfaceOrigin, props);
360}
361
Greg Danielf41b2bd2019-08-22 16:19:24 -0400362// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
363// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
364// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
365// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500366GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500367 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400368 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400369 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400370 sk_sp<SkColorSpace> colorSpace,
371 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400372 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500373 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
374 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400375 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500376 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400377 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400378 , fManagedOpsTask(managedOpsTask)
379 , fGlyphPainter(*this) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400380 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400381 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400382 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400383 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
384 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500385
Brian Salomonf18b1d82017-10-27 11:30:49 -0400386 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700387 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700388}
389
robertphillips2e1e51f2015-10-15 08:01:48 -0700390#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500391void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400392 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400393 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700394 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700395}
396#endif
397
Brian Osman11052242016-10-27 14:47:55 -0400398GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800399 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400400 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400401 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400402 }
robertphillipsa106c622015-10-16 09:07:06 -0700403}
404
Chris Dalton7d6748e2019-03-13 00:34:52 -0600405inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600406 if (GrAA::kNo == aa) {
407 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
408 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600409 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600410 return GrAAType::kMSAA;
411 }
412 return GrAAType::kNone;
413 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600414 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600415}
416
Greg Daniele252f082017-10-23 16:05:23 -0400417GrMipMapped GrRenderTargetContext::mipMapped() const {
418 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
419 return proxy->mipMapped();
420 }
421 return GrMipMapped::kNo;
422}
423
Greg Danielf41b2bd2019-08-22 16:19:24 -0400424GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800425 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700426 SkDEBUGCODE(this->validate();)
427
Brian Salomon3b8486a2020-04-21 12:43:26 -0400428 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600429 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400430 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600431 if (fOpsTask && fNumStencilSamples > 0) {
432 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600433 fOpsTask->setMustPreserveStencil();
434 // Reload the stencil buffer content at the beginning of newOpsTask.
435 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
436 // values?
437 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
438 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400439 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600440 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700441 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400442 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400443 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700444}
445
Michael Ludwig7c12e282020-05-29 09:54:07 -0400446void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400447 const SkMatrixProvider& matrixProvider,
448 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800449 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700450 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700451 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400452 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700453
Greg Danielbe7fc462019-01-03 16:40:42 -0500454 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
455 // secondary command buffers because it would require stopping and starting a render pass which
456 // we don't have access to.
457 if (this->wrapsVkSecondaryCB()) {
458 return;
459 }
460
Herb Derby26cbe512018-05-24 14:39:01 -0400461 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400462 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400463 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700464}
465
Brian Osman11052242016-10-27 14:47:55 -0400466void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800467 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700468 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700469 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400470 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700471
Robert Phillips72152832017-01-25 17:31:35 -0500472 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400473
Greg Danielf41b2bd2019-08-22 16:19:24 -0400474 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700475}
476
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500477static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
478 paint->setColor4f(color);
479 if (color.isOpaque()) {
480 // Can just rely on the src-over blend mode to do the right thing
481 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
482 } else {
483 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
484 // were src blended
485 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
486 }
487}
488
Michael Ludwig81d41722020-05-26 16:57:38 -0400489// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
490// premul alpha type. If we ever support different alpha type render targets, this function should
491// transform the color as appropriate.
492void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500493 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400494 bool upgradePartialToFull) {
495 ASSERT_SINGLE_OWNER
496 RETURN_IF_ABANDONED
497 SkDEBUGCODE(this->validate();)
498 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
499
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400500 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
501 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
502 // supports then. Drawing an axis-aligned rect is the fallback path.
503 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
504 if (scissor && !scissorState.set(*scissor)) {
505 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
506 // except clear ops are not draw ops).
507 return;
508 }
509
510 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
511 // the test. We only do this when the clear would be handled by a load op or natively.
512 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
513 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
514 this->caps()->shouldInitializeTextures())) {
515 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
516 // only clear the entire target if we knew it had not been cleared before. As
517 // is this could end up doing a lot of redundant clears.
518 scissorState.setDisabled();
519 } else {
520 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
521 // render target to overflow into any approx-fit padding of the backing store dimensions
522 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400523 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500524 }
525
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400526 if (!scissorState.enabled()) {
527 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
528 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400529 GrOpsTask* opsTask = this->getOpsTask();
530 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500531 !this->caps()->performColorClearsAsDraws()) {
532 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400533 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500534 return;
535 } else {
536 // Will use an op for the clear, reset the load op to discard since the op will
537 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400538 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500539 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400540 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500541
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400542 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
543 // as a load op.
544 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
545 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
546 if (clearAsDraw) {
547 GrPaint paint;
548 clear_to_grpaint(color, &paint);
549 this->addDrawOp(nullptr,
550 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
551 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500552 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400553 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500554 }
555}
556
Michael Ludwig7c12e282020-05-29 09:54:07 -0400557void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500558 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400559 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000560 // Start with the render target, since that is the maximum content we could possibly fill.
561 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500562 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400563 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000564 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
565 // inverse view matrix.
566 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
567 } else {
568 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
569 SkMatrix localMatrix;
570 if (!viewMatrix.invert(&localMatrix)) {
571 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400572 }
Michael Ludwig61328202019-06-19 14:48:58 +0000573 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
574 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700575 }
robertphillipsea461502015-05-26 11:38:03 -0700576}
577
Michael Ludwig61328202019-06-19 14:48:58 +0000578enum class GrRenderTargetContext::QuadOptimization {
579 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
580 kDiscarded,
581 // The rect to draw was converted to some other op and appended to the oplist, so no additional
582 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
583 // a constColor is provided.
584 kSubmitted,
585 // The clip was folded into the device quad, with updated edge flags and local coords, and
586 // caller is responsible for adding an appropriate op.
587 kClipApplied,
588 // No change to clip, but quad updated to better fit clip/render target, and caller is
589 // responsible for adding an appropriate op.
590 kCropped
591};
Michael Ludwig61a16512019-01-15 11:15:13 -0500592
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400593static bool make_vertex_finite(float* value) {
594 if (SkScalarIsNaN(*value)) {
595 return false;
596 }
597
598 if (!SkScalarIsFinite(*value)) {
599 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
600 // left when calculating crops.
601 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
602 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
603 }
604
605 return true;
606}
607
Michael Ludwig7c12e282020-05-29 09:54:07 -0400608static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
609 return clip ? clip->getConservativeBounds(rtc->width(), rtc->height())
610 : SkIRect::MakeWH(rtc->width(), rtc->height());
611}
612
Michael Ludwig61328202019-06-19 14:48:58 +0000613GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400614 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500615 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000616 // Optimization requirements:
617 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500618 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
619 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
620 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
621 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
622 // 5. kCropped in all other scenarios (although a crop may be a no-op)
623
624 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
625 // better to just keep the old flags instead of introducing mixed edge flags.
626 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000627
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400628 // Use the logical size of the render target, which allows for "fullscreen" clears even if
629 // the render target has an approximate backing fit
630 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400631
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500632 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000633 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400634 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
635 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500636 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400637 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500638 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
639 !make_vertex_finite(quad->fDevice.ys() + i) ||
640 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400641 // Discard if we see a nan
642 return QuadOptimization::kDiscarded;
643 }
644 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500645 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400646 }
647 } else {
648 // CropToRect requires the quads to be finite. If they are not finite and we have local
649 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500650 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400651 return QuadOptimization::kDiscarded;
652 }
Brian Salomon7694b902019-06-18 21:00:21 +0000653 }
654
Michael Ludwig61328202019-06-19 14:48:58 +0000655 // If the quad is entirely off screen, it doesn't matter what the clip does
656 if (!rtRect.intersects(drawBounds)) {
657 return QuadOptimization::kDiscarded;
658 }
Brian Salomon7694b902019-06-18 21:00:21 +0000659
Michael Ludwig61328202019-06-19 14:48:58 +0000660 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
661 // the render target).
662 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400663 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
664 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
665 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
666 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000667 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400668 if (clip && !clip->quickContains(rtRect)) {
669 if (!clip->isRRect(rtRect, &clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000670 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000671 }
Brian Salomon7694b902019-06-18 21:00:21 +0000672 }
673
Michael Ludwig61328202019-06-19 14:48:58 +0000674 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
675 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400676 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000677 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
678 // of the render target and the clip rect)
679 SkRect clipBounds = rtRect;
680 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
681 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000682 }
683
Michael Ludwig61328202019-06-19 14:48:58 +0000684 if (clipRRect.isRect()) {
685 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500686 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700687 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500688 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000689 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500690 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000691 if (drawBounds.contains(rtRect)) {
692 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400693 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000694 return QuadOptimization::kSubmitted;
695 } else if (GrClip::IsPixelAligned(drawBounds) &&
696 drawBounds.width() > 256 && drawBounds.height() > 256) {
697 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
698 SkIRect scissorRect;
699 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400700 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000701 return QuadOptimization::kSubmitted;
702 }
703 }
704
705 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000706 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500707 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000708 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
709 // properly reflect the smooth edge of the clip.
710 *aa = GrAA::kYes;
711 }
712 // We intentionally do not downgrade AA here because we don't know if we need to
713 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
714 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
715
716 // deviceQuad is exactly the intersection of original quad and clip, so it can be
717 // drawn with no clip (submitted by caller)
718 return QuadOptimization::kClipApplied;
719 } else {
720 // The quads have been updated to better fit the clip bounds, but can't get rid of
721 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500722 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000723 return QuadOptimization::kCropped;
724 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700725 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000726 // Rounded corners and constant filled color (limit ourselves to solid colors because
727 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500728 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
729 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
730 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000731 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
732 // we can draw the rrect directly and ignore the edge flags
733 GrPaint paint;
734 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400735 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000736 clipRRect, GrStyle::SimpleFill());
737 return QuadOptimization::kSubmitted;
738 } else {
739 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500740 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000741 return QuadOptimization::kCropped;
742 }
Brian Salomon7694b902019-06-18 21:00:21 +0000743 }
Brian Salomon7694b902019-06-18 21:00:21 +0000744 }
745
Michael Ludwig61328202019-06-19 14:48:58 +0000746 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400747 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000748
749 // One final check for discarding, since we may have gone here directly due to a complex clip
750 if (!clipBounds.intersects(drawBounds)) {
751 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000752 }
753
Michael Ludwig61328202019-06-19 14:48:58 +0000754 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
755 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500756 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
757 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000758
759 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000760}
761
Michael Ludwig7c12e282020-05-29 09:54:07 -0400762void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500763 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500764 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500765 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400766 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000767 ASSERT_SINGLE_OWNER
768 RETURN_IF_ABANDONED
769 SkDEBUGCODE(this->validate();)
770 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500771
Michael Ludwig61328202019-06-19 14:48:58 +0000772 AutoCheckFlush acf(this->drawingManager());
773
774 SkPMColor4f* constColor = nullptr;
775 SkPMColor4f paintColor;
776 if (!ss && !paint.numCoverageFragmentProcessors() &&
777 paint.isConstantBlendedColor(&paintColor)) {
778 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
779 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500780 }
781
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500782 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000783 if (opt >= QuadOptimization::kClipApplied) {
784 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400785 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000786 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
787 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500788 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
789 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700790 }
Michael Ludwig61328202019-06-19 14:48:58 +0000791 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800792}
793
Michael Ludwig7c12e282020-05-29 09:54:07 -0400794void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400795 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500796 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000797 sk_sp<GrColorSpaceXform> textureXform,
798 GrSamplerState::Filter filter,
799 const SkPMColor4f& color,
800 SkBlendMode blendMode,
801 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500802 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400803 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000804 ASSERT_SINGLE_OWNER
805 RETURN_IF_ABANDONED
806 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400807 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000808 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
809
810 AutoCheckFlush acf(this->drawingManager());
811
812 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
813 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500814 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000815
816 SkASSERT(opt != QuadOptimization::kSubmitted);
817 if (opt != QuadOptimization::kDiscarded) {
818 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400819 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000820 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400821 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400822 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
823 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400824 // Use the provided subset, although hypothetically we could detect that the cropped local
825 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500826 this->addDrawOp(finalClip,
827 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
828 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400829 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000830 }
831}
832
Michael Ludwig7c12e282020-05-29 09:54:07 -0400833void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500834 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500835 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400836 const SkMatrix& viewMatrix,
837 const SkRect& rect,
838 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700839 if (!style) {
840 style = &GrStyle::SimpleFill();
841 }
joshualitt1de610a2016-01-06 08:26:09 -0800842 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700843 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700844 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400845 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700846
bsalomon6663acf2016-05-10 09:14:17 -0700847 // Path effects should've been devolved to a path in SkGpuDevice
848 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700849
Robert Phillips72152832017-01-25 17:31:35 -0500850 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700851
bsalomon6663acf2016-05-10 09:14:17 -0700852 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400853 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000854 // Fills the rect, using rect as its own local coordinates
855 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500856 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400857 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
858 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
859 (rect.width() && rect.height())) {
860 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
861 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400862 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700863
Chris Dalton7d6748e2019-03-13 00:34:52 -0600864 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500865 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
866 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
867 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500868 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400869 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700870 return;
robertphillips4bc31812016-03-01 12:22:49 -0800871 }
robertphillips4bc31812016-03-01 12:22:49 -0800872 }
Mike Klein16885072018-12-11 09:54:31 -0500873 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000874 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
875 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700876}
877
Michael Ludwig7c12e282020-05-29 09:54:07 -0400878void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500879 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
880 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600881 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500882
883 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
884 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500885}
886
Robert Phillipsec2249f2016-11-09 08:54:35 -0500887int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500888 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400889 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500890}
891
Greg Danielf41b2bd2019-08-22 16:19:24 -0400892GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600893 ) const {
894#if GR_TEST_UTILS
895 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400896 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600897 }
898#endif
899 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
900 // would normally be overwritten. The one exception is if the render target context is marked as
901 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
902 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
903 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
904 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400905 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600906}
907
Chris Dalton858cf232019-10-14 16:20:00 -0600908void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600909 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600910 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600911 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600912
Chris Daltoneffee202019-07-01 22:28:03 -0600913 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600914 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500915 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600916 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400917 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600918 }
919 SkASSERT(numRequiredSamples > 0);
920
921 if (numRequiredSamples > fNumStencilSamples) {
922 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500923 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600924 }
Chris Dalton6b982802019-06-27 13:53:46 -0600925
Chris Dalton674f77a2019-09-30 20:49:39 -0600926 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600927 if (this->caps()->performStencilClearsAsDraws()) {
928 // There is a driver bug with clearing stencil. We must use an op to manually clear the
929 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400930 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600931 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600932 this->getOpsTask()->setInitialStencilContent(
933 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600934 }
935 }
936}
937
Michael Ludwig81d41722020-05-26 16:57:38 -0400938void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600939 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
940
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400941 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
942 if (scissor && !scissorState.set(*scissor)) {
943 // The requested clear region is off screen, so nothing to do.
944 return;
945 }
946
Michael Ludwig1e632792020-05-21 12:45:31 -0400947 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400948 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400949 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500950 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500951
952 // Configure the paint to have no impact on the color buffer
953 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400954 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400955 this->addDrawOp(nullptr,
956 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
957 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500958 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400959 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500960 }
robertphillips976f5f02016-06-03 10:59:20 -0700961}
962
Michael Ludwig7c12e282020-05-29 09:54:07 -0400963void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600964 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400965 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400966 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500967 ASSERT_SINGLE_OWNER_PRIV
968 RETURN_IF_ABANDONED_PRIV
969 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400970 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
971 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500972
Brian Salomon467921e2017-03-06 16:17:12 -0500973 // TODO: extract portions of checkDraw that are relevant to path stenciling.
974 SkASSERT(path);
975 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
976
977 // FIXME: Use path bounds instead of this WAR once
978 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
979 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
980
Michael Ludwig7c12e282020-05-29 09:54:07 -0400981 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
982 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
983 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400984 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
985 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
986
Michael Ludwig7c12e282020-05-29 09:54:07 -0400987 if (clip && !clip->apply(fRenderTargetContext->width(), fRenderTargetContext->height(),
988 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500989 return;
990 }
Michael Ludwig7c12e282020-05-29 09:54:07 -0400991 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
992 // but as it is, we're just using the full render target so intersecting the two bounds would
993 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -0500994
Robert Phillips7c525e62018-06-12 10:11:12 -0400995 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
996 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600997 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500998 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500999 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001000 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001001 if (!op) {
1002 return;
1003 }
Brian Salomon97180af2017-03-14 13:42:58 -04001004 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001005
Chris Daltoneffee202019-07-01 22:28:03 -06001006 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001007 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001008}
1009
Michael Ludwig7c12e282020-05-29 09:54:07 -04001010void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001011 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001012 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001013 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1014 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001015 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001016 ASSERT_SINGLE_OWNER
1017 RETURN_IF_ABANDONED
1018 SkDEBUGCODE(this->validate();)
1019 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001020
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001021 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1022 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1023 AutoCheckFlush acf(this->drawingManager());
1024 GrAAType aaType = this->chooseAAType(aa);
1025 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1026 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1027 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001028 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1029 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001030}
1031
Michael Ludwig7c12e282020-05-29 09:54:07 -04001032void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001033 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001034 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001035 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001036 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001037 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001038 ASSERT_SINGLE_OWNER
1039 RETURN_IF_ABANDONED
1040 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001041 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001042
1043 AutoCheckFlush acf(this->drawingManager());
1044
1045 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001046 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001047 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001048 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001049 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001050 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001051 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001052}
1053
1054///////////////////////////////////////////////////////////////////////////////
1055
Michael Ludwig7c12e282020-05-29 09:54:07 -04001056void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001057 GrPaint&& paint,
1058 const SkMatrix& viewMatrix,
1059 int spriteCount,
1060 const SkRSXform xform[],
1061 const SkRect texRect[],
1062 const SkColor colors[]) {
1063 ASSERT_SINGLE_OWNER
1064 RETURN_IF_ABANDONED
1065 SkDEBUGCODE(this->validate();)
1066 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1067
1068 AutoCheckFlush acf(this->drawingManager());
1069
1070 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1071 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1072 aaType, spriteCount, xform, texRect, colors);
1073 this->addDrawOp(clip, std::move(op));
1074}
1075
1076///////////////////////////////////////////////////////////////////////////////
1077
Michael Ludwig7c12e282020-05-29 09:54:07 -04001078void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001079 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001080 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001081 const SkMatrix& viewMatrix,
1082 const SkRRect& rrect,
1083 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001084 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001085 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001086 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001087 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001088
1089 const SkStrokeRec& stroke = style.strokeRec();
1090 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001091 return;
1092 }
1093
Michael Ludwig7c12e282020-05-29 09:54:07 -04001094 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001095#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1096 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001097 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001098 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1099 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001100 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1101 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001102 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001103 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1104 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1105 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001106 }
1107#endif
bsalomon6663acf2016-05-10 09:14:17 -07001108 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001109
Robert Phillips72152832017-01-25 17:31:35 -05001110 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001111
Chris Dalton7d6748e2019-03-13 00:34:52 -06001112 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001113
Chris Dalton0dffbab2019-03-27 13:08:50 -06001114 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001115 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1116 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1117 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1118 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1119 // to avoid perf regressions on some platforms.
1120 assert_alive(paint);
1121 op = GrOvalOpFactory::MakeCircularRRectOp(
1122 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1123 }
1124 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001125 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001126 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001127 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001128 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001129 assert_alive(paint);
1130 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001131 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001132 }
1133 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001134 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001135 return;
robertphillipsea461502015-05-26 11:38:03 -07001136 }
robertphillipsb56f9272016-02-25 11:03:52 -08001137
Mike Klein16885072018-12-11 09:54:31 -05001138 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001139 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001140 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001141}
1142
Jim Van Verthc5903412016-11-17 15:27:09 -05001143///////////////////////////////////////////////////////////////////////////////
1144
Jim Van Verth3af1af92017-05-18 15:06:54 -04001145static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1146 SkPoint3 result;
1147 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1148 result.fZ = pt.fZ;
1149 return result;
1150}
1151
Michael Ludwig7c12e282020-05-29 09:54:07 -04001152bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001153 const SkMatrix& viewMatrix,
1154 const SkPath& path,
1155 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001156 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001157 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001158 return true;
1159 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001160 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001161 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001162
1163 // check z plane
1164 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1165 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1166 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1167 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1168 return false;
1169 }
1170
1171 SkRRect rrect;
1172 SkRect rect;
1173 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001174 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001175 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1176 if (!isRRect &&
1177 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1178 rect.width() > SK_ScalarNearlyZero) {
1179 rrect.setOval(rect);
1180 isRRect = true;
1181 }
1182 if (!isRRect && path.isRect(&rect)) {
1183 rrect.setRect(rect);
1184 isRRect = true;
1185 }
1186
1187 if (!isRRect) {
1188 return false;
1189 }
1190
Jim Van Verthc5903412016-11-17 15:27:09 -05001191 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001192 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001193 }
1194
Robert Phillips72152832017-01-25 17:31:35 -05001195 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001196
Jim Van Verth3af1af92017-05-18 15:06:54 -04001197 // transform light
1198 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1199
1200 // 1/scale
1201 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001202 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1204 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1205
1206 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001207 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1208
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001209 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001210 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1211 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1212 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001213
1214 // Outset the shadow rrect to the border of the penumbra
1215 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1216 SkRRect ambientRRect;
1217 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1218 // If the rrect was an oval then its outset will also be one.
1219 // We set it explicitly to avoid errors.
1220 if (rrect.isOval()) {
1221 ambientRRect = SkRRect::MakeOval(outsetRect);
1222 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001223 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001224 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1225 }
1226
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001227 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001228 if (transparent) {
1229 // set a large inset to force a fill
1230 devSpaceInsetWidth = ambientRRect.width();
1231 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001232
Robert Phillips7c525e62018-06-12 10:11:12 -04001233 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1234 ambientColor,
1235 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001236 ambientRRect,
1237 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001238 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001239 if (op) {
1240 this->addDrawOp(clip, std::move(op));
1241 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001242 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001243
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001244 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001245 SkScalar devSpaceSpotBlur;
1246 SkScalar spotScale;
1247 SkVector spotOffset;
1248 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1249 devLightPos.fZ, rec.fLightRadius,
1250 &devSpaceSpotBlur, &spotScale, &spotOffset);
1251 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001252 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1253
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 // Adjust translate for the effect of the scale.
1255 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1256 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1257 // This offset is in dev space, need to transform it into source space.
1258 SkMatrix ctmInverse;
1259 if (viewMatrix.invert(&ctmInverse)) {
1260 ctmInverse.mapPoints(&spotOffset, 1);
1261 } else {
1262 // Since the matrix is a similarity, this should never happen, but just in case...
1263 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1264 SkASSERT(false);
1265 }
1266
1267 // Compute the transformed shadow rrect
1268 SkRRect spotShadowRRect;
1269 SkMatrix shadowTransform;
1270 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1271 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001272 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001273
1274 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001275 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001276 SkScalar insetWidth = blurOutset;
1277 if (transparent) {
1278 // If transparent, just do a fill
1279 insetWidth += spotShadowRRect.width();
1280 } else {
1281 // For shadows, instead of using a stroke we specify an inset from the penumbra
1282 // border. We want to extend this inset area so that it meets up with the caster
1283 // geometry. The inset geometry will by default already be inset by the blur width.
1284 //
1285 // We compare the min and max corners inset by the radius between the original
1286 // rrect and the shadow rrect. The distance between the two plus the difference
1287 // between the scaled radius and the original radius gives the distance from the
1288 // transformed shadow shape to the original shape in that corner. The max
1289 // of these gives the maximum distance we need to cover.
1290 //
1291 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1292 // that to get the full insetWidth.
1293 SkScalar maxOffset;
1294 if (rrect.isRect()) {
1295 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001296 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001297 rrect.rect().fLeft),
1298 SkTAbs(spotShadowRRect.rect().fTop -
1299 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001300 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001301 rrect.rect().fRight),
1302 SkTAbs(spotShadowRRect.rect().fBottom -
1303 rrect.rect().fBottom)));
1304 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001305 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001306 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1307 rrect.rect().fLeft + dr,
1308 spotShadowRRect.rect().fTop -
1309 rrect.rect().fTop + dr);
1310 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1311 rrect.rect().fRight - dr,
1312 spotShadowRRect.rect().fBottom -
1313 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001314 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001315 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001316 }
Brian Osman788b9162020-02-07 10:36:46 -05001317 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001318 }
1319
1320 // Outset the shadow rrect to the border of the penumbra
1321 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1322 if (spotShadowRRect.isOval()) {
1323 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1324 } else {
1325 SkScalar outsetRad = spotRadius + blurOutset;
1326 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1327 }
1328
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001329 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001330
Robert Phillips7c525e62018-06-12 10:11:12 -04001331 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1332 spotColor,
1333 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001334 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001335 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001336 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001337 if (op) {
1338 this->addDrawOp(clip, std::move(op));
1339 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001340 }
1341
1342 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001343}
1344
1345///////////////////////////////////////////////////////////////////////////////
1346
Michael Ludwig7c12e282020-05-29 09:54:07 -04001347bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001348 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001349 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001350 const SkMatrix& viewMatrix,
1351 const SkRRect& origOuter,
1352 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001353 SkASSERT(!origInner.isEmpty());
1354 SkASSERT(!origOuter.isEmpty());
1355
Brian Salomon65749212017-12-01 16:01:47 -05001356 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1357
Chris Dalton7d6748e2019-03-13 00:34:52 -06001358 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001359
1360 if (GrAAType::kMSAA == aaType) {
1361 return false;
1362 }
1363
Greg Daniel2655ede2019-04-10 00:49:28 +00001364 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1365 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001366 auto outerR = outer->width() / 2.f;
1367 auto innerR = inner->width() / 2.f;
1368 auto cx = outer->getBounds().fLeft + outerR;
1369 auto cy = outer->getBounds().fTop + outerR;
1370 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1371 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1372 auto avgR = (innerR + outerR) / 2.f;
1373 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1374 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1375 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001376 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001377 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001378 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001379 if (op) {
1380 this->addDrawOp(clip, std::move(op));
1381 return true;
1382 }
Mike Klein16885072018-12-11 09:54:31 -05001383 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001384 }
1385 }
1386
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001387 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001388 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001389 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1390 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001391 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001392 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1393 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001394 }
robertphillips00095892016-02-29 13:50:40 -08001395
robertphillips00095892016-02-29 13:50:40 -08001396 SkMatrix inverseVM;
1397 if (!viewMatrix.isIdentity()) {
1398 if (!origInner.transform(viewMatrix, inner.writable())) {
1399 return false;
1400 }
1401 if (!origOuter.transform(viewMatrix, outer.writable())) {
1402 return false;
1403 }
1404 if (!viewMatrix.invert(&inverseVM)) {
1405 return false;
1406 }
1407 } else {
1408 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001409 }
robertphillips00095892016-02-29 13:50:40 -08001410
Ethan Nicholaseace9352018-10-15 20:09:54 +00001411 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001412 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001413 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001414 if (!innerEffect) {
1415 return false;
1416 }
1417
Ethan Nicholaseace9352018-10-15 20:09:54 +00001418 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001419 if (!outerEffect) {
1420 return false;
1421 }
1422
Brian Salomon82f44312017-01-11 13:42:54 -05001423 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1424 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001425
1426 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001427 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001428 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1429 }
halcanary9d524f22016-03-29 09:03:52 -07001430
Brian Salomon82f44312017-01-11 13:42:54 -05001431 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1432 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001433 return true;
1434}
1435
Michael Ludwig7c12e282020-05-29 09:54:07 -04001436void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001437 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001438 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001439 const SkMatrix& viewMatrix,
1440 const SkRRect& outer,
1441 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001442 ASSERT_SINGLE_OWNER
1443 RETURN_IF_ABANDONED
1444 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001445 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001446
1447 SkASSERT(!outer.isEmpty());
1448 SkASSERT(!inner.isEmpty());
1449
Robert Phillips72152832017-01-25 17:31:35 -05001450 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001451
Brian Salomon82f44312017-01-11 13:42:54 -05001452 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001453 return;
1454 }
Mike Klein16885072018-12-11 09:54:31 -05001455 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001456
1457 SkPath path;
1458 path.setIsVolatile(true);
1459 path.addRRect(inner);
1460 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001461 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001462 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001463}
1464
robertphillipsea461502015-05-26 11:38:03 -07001465///////////////////////////////////////////////////////////////////////////////
1466
Michael Ludwig7c12e282020-05-29 09:54:07 -04001467void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001468 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001469 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001470 const SkMatrix& viewMatrix,
1471 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001472 const GrStyle& style,
1473 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001474 ASSERT_SINGLE_OWNER
1475 RETURN_IF_ABANDONED
1476 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001477 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001478
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001479 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001480 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001481 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001482 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001483 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1484 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001485 aa = GrAA::kNo;
1486 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001487 }
msarettcc319b92016-08-25 18:07:18 -07001488 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001489 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001490 SkPath path;
1491 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001492 path.setIsVolatile(true);
1493
Brian Salomon82f44312017-01-11 13:42:54 -05001494 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001495 }
1496
Chris Dalton7d6748e2019-03-13 00:34:52 -06001497 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001498 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1499 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001500 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001501}
1502
Michael Ludwig7c12e282020-05-29 09:54:07 -04001503void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001504 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001505 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001506 const SkMatrix& viewMatrix,
1507 const SkRect& oval,
1508 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001509 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001510 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001511 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001512 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001513
Robert Phillips7484d202018-07-03 09:09:08 -04001514 const SkStrokeRec& stroke = style.strokeRec();
1515
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001516 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001517 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1518 return;
1519 }
1520
1521 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001522 return;
robertphillipsea461502015-05-26 11:38:03 -07001523 }
1524
Robert Phillips72152832017-01-25 17:31:35 -05001525 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001526
Chris Dalton7d6748e2019-03-13 00:34:52 -06001527 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001528
1529 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001530 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1531 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001532 // We don't draw true circles as round rects in coverage mode, because it can
1533 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1534 assert_alive(paint);
1535 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1536 this->caps()->shaderCaps());
1537 }
1538 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001539 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1540 // the arc equation. This same special geometry and fragment branch also turn out to be a
1541 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1542 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1543 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001544 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001545 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1546 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001547 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001548 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001549 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001550 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1551 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001552 }
1553 if (op) {
1554 this->addDrawOp(clip, std::move(op));
1555 return;
robertphillipsea461502015-05-26 11:38:03 -07001556 }
robertphillipsb56f9272016-02-25 11:03:52 -08001557
Mike Klein16885072018-12-11 09:54:31 -05001558 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001559 this->drawShapeUsingPathRenderer(
1560 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001561 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001562}
1563
Michael Ludwig7c12e282020-05-29 09:54:07 -04001564void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001565 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001566 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001567 const SkMatrix& viewMatrix,
1568 const SkRect& oval,
1569 SkScalar startAngle,
1570 SkScalar sweepAngle,
1571 bool useCenter,
1572 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001573 ASSERT_SINGLE_OWNER
1574 RETURN_IF_ABANDONED
1575 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001576 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001577
1578 AutoCheckFlush acf(this->drawingManager());
1579
Chris Dalton7d6748e2019-03-13 00:34:52 -06001580 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001581 if (GrAAType::kCoverage == aaType) {
1582 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1583 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1584 std::move(paint),
1585 viewMatrix,
1586 oval,
1587 startAngle,
1588 sweepAngle,
1589 useCenter,
1590 style,
1591 shaderCaps);
1592 if (op) {
1593 this->addDrawOp(clip, std::move(op));
1594 return;
1595 }
1596 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001597 }
Brian Salomone4949402018-04-26 15:22:04 -04001598 this->drawShapeUsingPathRenderer(
1599 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001600 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001601}
1602
Michael Ludwig7c12e282020-05-29 09:54:07 -04001603void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001604 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001605 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001606 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001607 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001608 sk_sp<GrColorSpaceXform> csxf,
1609 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001610 std::unique_ptr<SkLatticeIter> iter,
1611 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001612 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001613 RETURN_IF_ABANDONED
1614 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001615 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001616
Robert Phillips72152832017-01-25 17:31:35 -05001617 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001618
Brian Salomon2a943df2018-05-04 13:43:19 -04001619 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001620 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001621 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001622 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001623}
1624
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001625void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1626 const SkRect& bounds) {
1627 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1628 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001629 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001630}
1631
Brian Salomon031b0ba2019-05-23 11:05:26 -04001632void GrRenderTargetContext::asyncRescaleAndReadPixels(
1633 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1634 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1635 auto direct = fContext->priv().asDirectContext();
1636 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001637 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001638 return;
1639 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001640 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001641 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001642 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001643 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001644 if (this->asRenderTargetProxy()->framebufferOnly()) {
1645 callback(context, nullptr);
1646 return;
1647 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001648 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001649 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001650 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001651 return;
1652 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001653 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001654 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001655 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001656 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001657 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001658 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1659 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001660 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001661 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001662 backendFormatOfFinalContext, dstCT);
1663 // Fail if we can't read from the source surface's color type.
1664 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001665 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001666 return;
1667 }
Brian Salomon624f9062019-07-02 14:23:00 -04001668 // Fail if read color type does not have all of dstCT's color channels and those missing color
1669 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001670 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1671 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1672 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1673 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001674 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001675 return;
1676 }
1677
Brian Salomonbf6b9792019-08-21 09:38:10 -04001678 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001679 int x = srcRect.fLeft;
1680 int y = srcRect.fTop;
1681 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001682 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1683 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001684 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001685 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001686 return;
1687 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001688 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001689 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001690 x = y = 0;
1691 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001692 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1693 this->colorInfo().alphaType(),
1694 info.colorSpace(),
1695 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001696 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1697 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001698 // We flip or color convert by drawing and we don't currently support drawing to
1699 // kPremul.
1700 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001701 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001702 return;
1703 }
Greg Daniel40903af2020-01-30 14:55:05 -05001704 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001705 SkRect srcRectToDraw = SkRect::Make(srcRect);
1706 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001707 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001708 texProxyView =
1709 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1710 SkBackingFit::kApprox, SkBudgeted::kNo);
1711 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001712 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001713 return;
1714 }
Brian Salomonc5243782020-04-02 12:50:34 -04001715 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1717 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001718 tempRTC = GrRenderTargetContext::Make(
1719 direct, this->colorInfo().colorType(), info.refColorSpace(),
1720 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001721 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001722 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001723 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001724 return;
1725 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001726 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001727 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1728 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001729 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1730 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1731 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001732 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001733 }
1734 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001735 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001736 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1737 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001738}
1739
Brian Salomon9241a6d2019-10-03 13:26:54 -04001740class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1741public:
1742 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1743 ~AsyncReadResult() override {
1744 for (int i = 0; i < fPlanes.count(); ++i) {
1745 if (!fPlanes[i].fMappedBuffer) {
1746 delete[] static_cast<const char*>(fPlanes[i].fData);
1747 } else {
1748 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1749 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1750 }
1751 }
1752 }
1753
1754 int count() const override { return fPlanes.count(); }
1755 const void* data(int i) const override { return fPlanes[i].fData; }
1756 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1757
1758 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001759 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001760 size_t rowBytes,
1761 GrClientMappedBufferManager* manager) {
1762 SkASSERT(!result.fTransferBuffer->isMapped());
1763 const void* mappedData = result.fTransferBuffer->map();
1764 if (!mappedData) {
1765 return false;
1766 }
1767 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001768 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001769 result.fPixelConverter(convertedData.get(), mappedData);
1770 this->addCpuPlane(std::move(convertedData), rowBytes);
1771 result.fTransferBuffer->unmap();
1772 } else {
1773 manager->insert(result.fTransferBuffer);
1774 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1775 }
1776 return true;
1777 }
1778
1779 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1780 SkASSERT(data);
1781 SkASSERT(rowBytes > 0);
1782 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1783 }
1784
1785private:
1786 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1787 SkASSERT(data);
1788 SkASSERT(rowBytes > 0);
1789 SkASSERT(mappedBuffer);
1790 SkASSERT(mappedBuffer->isMapped());
1791 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1792 }
1793
1794 struct Plane {
1795 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1796 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1797 const void* fData;
1798 size_t fRowBytes;
1799 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1800 sk_sp<GrGpuBuffer> fMappedBuffer;
1801 };
1802 SkSTArray<3, Plane> fPlanes;
1803 uint32_t fInboxID;
1804};
1805
Brian Salomon024bd002019-06-11 11:38:16 -04001806void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1807 ReadPixelsCallback callback,
1808 ReadPixelsContext context) {
1809 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1810 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1811
Greg Danielca9dbe22020-01-02 13:44:30 -05001812 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001813 callback(context, nullptr);
1814 return;
1815 }
1816
Brian Salomon9241a6d2019-10-03 13:26:54 -04001817 auto directContext = fContext->priv().asDirectContext();
1818 SkASSERT(directContext);
1819 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1820
Brian Salomon024bd002019-06-11 11:38:16 -04001821 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1822
1823 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001824 auto ii = SkImageInfo::Make(rect.size(), colorType,
1825 this->colorInfo().alphaType(),
1826 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001827 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001828 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1829 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1830 result->addCpuPlane(std::move(data), pm.rowBytes());
1831
Brian Salomon1d435302019-07-01 13:05:28 -04001832 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001833 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001834 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001835 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001836 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001837 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001838 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001839
Brian Salomonab32f652019-05-10 14:24:50 -04001840 struct FinishContext {
1841 ReadPixelsCallback* fClientCallback;
1842 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001843 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001844 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001845 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001846 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001847 };
1848 // Assumption is that the caller would like to flush. We could take a parameter or require an
1849 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1850 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001851 auto* finishContext = new FinishContext{callback,
1852 context,
1853 rect.size(),
1854 colorType,
1855 mappedBufferManager,
1856 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001857 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001858 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001859 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001860 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1861 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1862 context->fMappedBufferManager)) {
1863 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001864 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001865 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001866 delete context;
1867 };
1868 GrFlushInfo flushInfo;
1869 flushInfo.fFinishedContext = finishContext;
1870 flushInfo.fFinishedProc = finishCallback;
1871 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1872}
1873
Brian Salomon9241a6d2019-10-03 13:26:54 -04001874void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1875 sk_sp<SkColorSpace> dstColorSpace,
1876 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001877 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001878 RescaleGamma rescaleGamma,
1879 SkFilterQuality rescaleQuality,
1880 ReadPixelsCallback callback,
1881 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001882 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1883 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001884 SkASSERT(!dstSize.isZero());
1885 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1886
Brian Salomon024bd002019-06-11 11:38:16 -04001887 auto direct = fContext->priv().asDirectContext();
1888 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001889 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001890 return;
1891 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001892 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001893 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001894 return;
1895 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001896 if (this->asRenderTargetProxy()->framebufferOnly()) {
1897 callback(context, nullptr);
1898 return;
1899 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001900 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001901 callback(context, nullptr);
1902 return;
1903 }
Brian Salomon024bd002019-06-11 11:38:16 -04001904 int x = srcRect.fLeft;
1905 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001906 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001907 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001908 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001909 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001910 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001911 dstColorSpace);
1912 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001913 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1914 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001915 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001916 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001917 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001918 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001919 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001920 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001921 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001922 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001923 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001924 srcView = this->readSurfaceView();
1925 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001926 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1927 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1928 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001929 // If we can't get a texture copy of the contents then give up.
1930 callback(context, nullptr);
1931 return;
1932 }
Brian Salomonc5243782020-04-02 12:50:34 -04001933 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001934 x = y = 0;
1935 }
Brian Salomon4d036892019-07-02 15:10:58 -04001936 // We assume the caller wants kPremul. There is no way to indicate a preference.
1937 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001938 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1939 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001940 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001941 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1942 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001943 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1944 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001945 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001946 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001947 return;
1948 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001949 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001950 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1951 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1952 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1953 SkMatrix::I(), std::move(xform));
1954 srcView = tempRTC->readSurfaceView();
1955 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001956 x = y = 0;
1957 }
1958 }
Greg Danielc594e622019-10-15 14:01:49 -04001959
Greg Daniele20fcad2020-01-08 11:52:34 -05001960 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1961 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1962 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001963 int halfW = dstSize.width()/2;
1964 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001965 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1966 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1967 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1968 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1969 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1970 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001971 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001972 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001973 return;
1974 }
1975
Brian Osman28ba5282019-10-30 14:18:07 -04001976 float baseM[20];
1977 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1978
Brian Salomon024bd002019-06-11 11:38:16 -04001979 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1980
Mike Reed1f607332020-05-21 12:11:27 -04001981 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04001982
Brian Salomon9241a6d2019-10-03 13:26:54 -04001983 SkRect dstRectY = SkRect::Make(dstSize);
1984 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00001985
Brian Salomon6aa65052020-01-28 12:16:53 -05001986 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1987 PixelTransferResult yTransfer, uTransfer, vTransfer;
1988
Brian Salomon024bd002019-06-11 11:38:16 -04001989 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1990 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001991 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001992 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001993 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04001994 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
1995 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
1996 /*unpremulInput=*/false,
1997 /*clampRGBOutput=*/true,
1998 /*premulOutput=*/false);
1999 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002000 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002001 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002002 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002003 if (!doSynchronousRead) {
2004 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2005 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2006 if (!yTransfer.fTransferBuffer) {
2007 callback(context, nullptr);
2008 return;
2009 }
Brian Salomon024bd002019-06-11 11:38:16 -04002010 }
2011
2012 texMatrix.preScale(2.f, 2.f);
2013 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2014 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002015 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002016 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002017 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002018 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2019 GrSamplerState::Filter::kBilerp);
2020 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2021 /*unpremulInput=*/false,
2022 /*clampRGBOutput=*/true,
2023 /*premulOutput=*/false);
2024 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002025 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002026 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002027 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002028 if (!doSynchronousRead) {
2029 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2030 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2031 if (!uTransfer.fTransferBuffer) {
2032 callback(context, nullptr);
2033 return;
2034 }
Brian Salomon024bd002019-06-11 11:38:16 -04002035 }
2036
2037 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2038 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002039 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002040 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002041 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002042 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2043 texMatrix, GrSamplerState::Filter::kBilerp);
2044 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2045 /*unpremulInput=*/false,
2046 /*clampRGBOutput=*/true,
2047 /*premulOutput=*/false);
2048 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002049 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002050 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002051 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002052 if (!doSynchronousRead) {
2053 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2054 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2055 if (!vTransfer.fTransferBuffer) {
2056 callback(context, nullptr);
2057 return;
2058 }
2059 }
2060
2061 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002062 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2063 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2064 size_t yRB = yInfo.minRowBytes();
2065 size_t uvRB = uvInfo.minRowBytes();
2066 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2067 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2068 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2069 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2070 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2071 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002072 callback(context, nullptr);
2073 return;
2074 }
2075 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002076 result->addCpuPlane(std::move(y), yRB );
2077 result->addCpuPlane(std::move(u), uvRB);
2078 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002079 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002080 return;
2081 }
2082
2083 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002084 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002085 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002086 GrClientMappedBufferManager* fMappedBufferManager;
2087 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002088 PixelTransferResult fYTransfer;
2089 PixelTransferResult fUTransfer;
2090 PixelTransferResult fVTransfer;
2091 };
2092 // Assumption is that the caller would like to flush. We could take a parameter or require an
2093 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2094 // callback to GrGpu until after the next flush that flushes our op list, though.
2095 auto* finishContext = new FinishContext{callback,
2096 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002097 direct->priv().clientMappedBufferManager(),
2098 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002099 std::move(yTransfer),
2100 std::move(uTransfer),
2101 std::move(vTransfer)};
2102 auto finishCallback = [](GrGpuFinishedContext c) {
2103 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002104 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002105 auto manager = context->fMappedBufferManager;
2106 size_t rowBytes = SkToSizeT(context->fSize.width());
2107 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2108 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002109 delete context;
2110 return;
2111 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002112 rowBytes /= 2;
2113 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2114 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2115 (*context->fClientCallback)(context->fClientContext, nullptr);
2116 delete context;
2117 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002118 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002119 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2120 (*context->fClientCallback)(context->fClientContext, nullptr);
2121 delete context;
2122 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002123 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002124 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002125 delete context;
2126 };
2127 GrFlushInfo flushInfo;
2128 flushInfo.fFinishedContext = finishContext;
2129 flushInfo.fFinishedProc = finishCallback;
2130 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002131}
2132
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002133GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2134 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002135 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002136 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002137 if (info.fSubmittedProc) {
2138 info.fSubmittedProc(info.fSubmittedContext, false);
2139 }
2140 if (info.fFinishedProc) {
2141 info.fFinishedProc(info.fFinishedContext);
2142 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002143 return GrSemaphoresSubmitted::kNo;
2144 }
robertphillips8c523e02016-07-26 07:41:00 -07002145 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002146 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002147
Greg Daniel55f040b2020-02-13 15:38:32 +00002148 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002149}
2150
Greg Danielc64ee462017-06-15 16:59:49 -04002151bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002152 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002153 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002154 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002155 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002156 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002157
2158 AutoCheckFlush acf(this->drawingManager());
2159
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002160 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002161 return false;
2162 }
2163
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002164 auto direct = fContext->priv().asDirectContext();
2165 if (!direct) {
2166 return false;
2167 }
2168
2169 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002170
Greg Daniel301015c2019-11-18 14:06:46 -05002171 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2172 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002173 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002174 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002175 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2176 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002177 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002178 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2179 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002180 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002181}
joshualitt33a5fce2015-11-18 13:28:51 -08002182
Michael Ludwig7c12e282020-05-29 09:54:07 -04002183void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002184 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002185 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002186 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002187 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002188 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002189 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002190 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002191 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002192 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2193
Michael Ludwig2686d692020-04-17 20:21:37 +00002194 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002195
Robert Phillips27927a52018-08-20 13:18:12 -04002196 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002197}
2198
Michael Ludwig7c12e282020-05-29 09:54:07 -04002199void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002200 GrPaint&& paint,
2201 GrAA aa,
2202 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002203 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002204 ASSERT_SINGLE_OWNER
2205 RETURN_IF_ABANDONED
2206 SkDEBUGCODE(this->validate();)
2207 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2208
Brian Salomon2fad74a2017-12-20 13:28:55 -05002209 if (shape.isEmpty()) {
2210 if (shape.inverseFilled()) {
2211 this->drawPaint(clip, std::move(paint), viewMatrix);
2212 }
2213 return;
robertphillipsea461502015-05-26 11:38:03 -07002214 }
2215
Robert Phillips72152832017-01-25 17:31:35 -05002216 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002217
Brian Salomon2fad74a2017-12-20 13:28:55 -05002218 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002219 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002220 SkRRect rrect;
2221 // We can ignore the starting point and direction since there is no path effect.
2222 bool inverted;
2223 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2224 if (rrect.isRect()) {
2225 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2226 &shape.style());
2227 return;
2228 } else if (rrect.isOval()) {
2229 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002230 return;
2231 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002232 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2233 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002234 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2235 viewMatrix.rectStaysRect()) {
2236 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2237 // the matrix to all the points individually rather than just to the rect
2238 SkRect rects[2];
2239 if (shape.asNestedRects(rects)) {
2240 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002241 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002242 fContext, std::move(paint), viewMatrix, rects);
2243 if (op) {
2244 this->addDrawOp(clip, std::move(op));
2245 }
2246 // Returning here indicates that there is nothing to draw in this case.
2247 return;
2248 }
robertphillipsea461502015-05-26 11:38:03 -07002249 }
2250 }
robertphillips4bc31812016-03-01 12:22:49 -08002251
Michael Ludwig6a6de652020-04-30 20:16:36 -04002252 // If we get here in drawShape(), we definitely need to use path rendering
2253 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2254 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002255}
2256
Michael Ludwig7c12e282020-05-29 09:54:07 -04002257bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002258 const GrUserStencilSettings* ss,
2259 SkRegion::Op op,
2260 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002261 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002262 const SkMatrix& viewMatrix,
2263 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002264 ASSERT_SINGLE_OWNER_PRIV
2265 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002266 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002267 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2268 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002269
2270 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002271 GrPaint paint;
2272 paint.setCoverageSetOpXPFactory(op, invert);
2273 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2274 SkRect::MakeIWH(fRenderTargetContext->width(),
2275 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002276 return true;
2277 }
2278
Robert Phillips72152832017-01-25 17:31:35 -05002279 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002280
2281 // An Assumption here is that path renderer would use some form of tweaking
2282 // the src color (either the input alpha or in the frag shader) to implement
2283 // aa. If we have some future driver-mojo path AA that can do the right
2284 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002285 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002286 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002287
Michael Ludwig7c12e282020-05-29 09:54:07 -04002288 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002289
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002290 GrPaint paint;
2291 paint.setCoverageSetOpXPFactory(op, invert);
2292
Michael Ludwig2686d692020-04-17 20:21:37 +00002293 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002294 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002295 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002296 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002297 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002298 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002299 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002300 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002301 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002302 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2303 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002304 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002305
2306 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002307 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002308 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002309 if (!pr) {
2310 return false;
2311 }
2312
Brian Salomonf3569f02017-10-24 12:52:33 -04002313 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2314 std::move(paint),
2315 ss,
2316 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002317 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002318 &clipConservativeBounds,
2319 &viewMatrix,
2320 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002321 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002322 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002323 pr->drawPath(args);
2324 return true;
2325}
2326
Brian Osman11052242016-10-27 14:47:55 -04002327SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002328 ASSERT_SINGLE_OWNER_PRIV
2329
Robert Phillips6a6de562019-02-15 15:19:15 -05002330 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002331 return SkBudgeted::kNo;
2332 }
2333
Brian Osman11052242016-10-27 14:47:55 -04002334 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002335
Greg Daniel3912a4b2020-01-14 09:56:04 -05002336 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002337}
2338
Michael Ludwig7c12e282020-05-29 09:54:07 -04002339void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002340 GrPaint&& paint,
2341 GrAA aa,
2342 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002343 const GrStyledShape& originalShape,
2344 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002345 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002346 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002347 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2348
Jim Van Verthf86073a2018-10-02 13:05:38 -04002349 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2350 return;
2351 }
2352
Michael Ludwig6a6de652020-04-30 20:16:36 -04002353 if (attemptShapeFallback && originalShape.simplified()) {
2354 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2355 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2356 // we ought to try again instead of going right to path rendering.
2357 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2358 return;
2359 }
2360
Michael Ludwig7c12e282020-05-29 09:54:07 -04002361 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002362
Michael Ludwig2686d692020-04-17 20:21:37 +00002363 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002364 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002365
robertphillips68737822015-10-29 12:12:21 -07002366 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002367 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002368 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002369 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002370 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002371 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002372 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002373 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002374 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002375
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002376 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002377 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002378 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002379 return;
2380 }
2381
Chris Dalton6ce447a2019-06-23 18:07:38 -06002382 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002383
2384 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2385 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2386 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2387
Brian Salomon2fad74a2017-12-20 13:28:55 -05002388 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002389 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002390 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2391 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002392 return;
2393 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002394 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002395 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002396 }
2397 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002398 if (canDrawArgs.fShape->style().applies()) {
2399 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2400 styleScale);
2401 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002402 return;
2403 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002404 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002405 // This time, allow SW renderer
2406 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2407 } else {
2408 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002409#if GR_PATH_RENDERER_SPEW
2410 SkDebugf("falling back to: %s\n", pr->name());
2411#endif
bsalomon6663acf2016-05-10 09:14:17 -07002412 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002413 }
robertphillipsea461502015-05-26 11:38:03 -07002414
bsalomon8acedde2016-06-24 10:42:16 -07002415 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002416#ifdef SK_DEBUG
2417 SkDebugf("Unable to find path renderer compatible with path.\n");
2418#endif
2419 return;
2420 }
2421
Robert Phillips256c37b2017-03-01 14:32:46 -05002422 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002423 std::move(paint),
2424 &GrUserStencilSettings::kUnused,
2425 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002426 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002427 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002428 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002429 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002430 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002431 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002432 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002433}
2434
Brian Salomon467921e2017-03-06 16:17:12 -05002435static void op_bounds(SkRect* bounds, const GrOp* op) {
2436 *bounds = op->bounds();
2437 if (op->hasZeroArea()) {
2438 if (op->hasAABloat()) {
2439 bounds->outset(0.5f, 0.5f);
2440 } else {
2441 // We don't know which way the particular GPU will snap lines or points at integer
2442 // coords. So we ensure that the bounds is large enough for either snap.
2443 SkRect before = *bounds;
2444 bounds->roundOut(bounds);
2445 if (bounds->fLeft == before.fLeft) {
2446 bounds->fLeft -= 1;
2447 }
2448 if (bounds->fTop == before.fTop) {
2449 bounds->fTop -= 1;
2450 }
2451 if (bounds->fRight == before.fRight) {
2452 bounds->fRight += 1;
2453 }
2454 if (bounds->fBottom == before.fBottom) {
2455 bounds->fBottom += 1;
2456 }
2457 }
2458 }
2459}
2460
Chris Dalton08755122019-08-05 16:13:47 -06002461void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002462 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002463 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2464}
2465
Michael Ludwig7c12e282020-05-29 09:54:07 -04002466void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002467 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002468 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002469 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002470 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002471 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002472 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002473 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002474 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002475 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002476
Brian Salomon467921e2017-03-06 16:17:12 -05002477 // Setup clip
2478 SkRect bounds;
2479 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002480 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002481 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002482 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002483 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002484
Chris Dalton9acf6822019-11-12 11:52:40 -07002485 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002486 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002487 }
2488
Michael Ludwig7c12e282020-05-29 09:54:07 -04002489 bool skipDraw = false;
2490 if (clip) {
2491 // Have a complex clip, so defer to its early clip culling
2492 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2493 skipDraw = true;
2494 }
2495 } else {
2496 // No clipping, so just clip the bounds against the logical render target dimensions
2497 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2498 skipDraw = true;
2499 }
2500 }
2501
2502 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002503 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002504 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002505 }
2506
Chris Dalton9acf6822019-11-12 11:52:40 -07002507 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2508 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2509
2510 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2511 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2512 // that uses a stencil test when the stencil buffer is multisampled.)
2513 bool hasMixedSampledCoverage = (
2514 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002515 SkASSERT(!hasMixedSampledCoverage ||
2516 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002517
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002518 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002519 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002520 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2521
Greg Daniel524e28b2019-11-01 11:48:53 -04002522 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002523 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002524 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002525 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002526 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002527 }
2528 }
2529
2530 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002531 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002532 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002533 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002534 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002535 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002536 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002537}
2538
Michael Ludwig7c12e282020-05-29 09:54:07 -04002539bool GrRenderTargetContext::setupDstProxyView(const GrClip* clip, const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002540 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002541 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2542 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2543 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002544 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002545 return false;
2546 }
2547
Greg Daniel3912a4b2020-01-14 09:56:04 -05002548 if (this->caps()->textureBarrierSupport() &&
2549 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002550 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002551 // The render target is a texture, so we can read from it directly in the shader. The XP
2552 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002553 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002554 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002555 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002556 }
2557 }
2558
Greg Daniel3912a4b2020-01-14 09:56:04 -05002559 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002560
Michael Ludwig7c12e282020-05-29 09:54:07 -04002561 SkIRect clippedRect = get_clip_bounds(this, clip);
Brian Salomon09181ef2018-11-14 13:39:51 -05002562 SkRect opBounds = op.bounds();
2563 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2564 // 0.5 pixels.
2565 if (op.hasAABloat() || op.hasZeroArea()) {
2566 opBounds.outset(0.5f, 0.5f);
2567 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2568 // performance we may ignore the clip when the draw is entirely inside the clip is float
2569 // space but will hit pixels just outside the clip when actually rasterizing.
2570 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002571 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002572 }
2573 SkIRect opIBounds;
2574 opBounds.roundOut(&opIBounds);
2575 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002576#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002577 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002578#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002579 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002580 }
2581
Greg Daniel3155f7f2020-01-16 16:54:26 -05002582 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002583 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2584 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002585 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002586 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002587
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002588 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002589 copyRect = clippedRect;
2590 }
Brian Salomon467921e2017-03-06 16:17:12 -05002591
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002592 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002593 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002594 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002595 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002596 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002597 } else {
Eric Karl74480882017-04-03 14:49:05 -07002598 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002600 }
Brian Salomonc5243782020-04-02 12:50:34 -04002601 auto copy =
2602 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2603 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2604 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002605
Brian Salomonc5243782020-04-02 12:50:34 -04002606 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002607 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002608 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002609}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002610
Greg Daniel573312e2020-02-07 17:22:35 -05002611bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002612 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002613 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002614 SkIRect clippedSrcRect;
2615 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002616 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2617 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002618 return false;
2619 }
2620
2621 GrPaint paint;
2622 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002623
Greg Danield2ccbb52020-02-05 10:45:39 -05002624 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002625 if (!fp) {
2626 return false;
2627 }
2628 paint.addColorFragmentProcessor(std::move(fp));
2629
2630 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002631 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002632 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2633 clippedSrcRect.height()),
2634 SkRect::Make(clippedSrcRect));
2635 return true;
2636}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002637
2638void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2639 SkASSERT(&task == fOpsTask.get());
2640 fOpsTask.reset();
2641}