blob: cdbc65dad2e990bc116c25d2e47705eeec6bf47d [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
Brian Osmanf5ecf512020-04-01 09:29:13 -040011#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrBackendSemaphore.h"
Brian Salomond005b692020-04-01 15:47:05 -040013#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/GrRecordingContext.h"
15#include "include/private/SkShadowFlags.h"
16#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040017#include "src/core/SkAutoPixmapStorage.h"
18#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkDrawShadowInfo.h"
20#include "src/core/SkGlyphRunPainter.h"
21#include "src/core/SkLatticeIter.h"
22#include "src/core/SkMatrixPriv.h"
23#include "src/core/SkRRectPriv.h"
24#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040025#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrBlurUtils.h"
29#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040030#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040033#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrDrawingManager.h"
35#include "src/gpu/GrFixedClip.h"
36#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040037#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040038#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrMemoryPool.h"
40#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040041#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040043#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/GrRenderTargetContextPriv.h"
45#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrStencilAttachment.h"
47#include "src/gpu/GrStyle.h"
48#include "src/gpu/GrTracing.h"
49#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040050#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040052#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040053#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000054#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000055#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrAtlasTextOp.h"
57#include "src/gpu/ops/GrClearOp.h"
58#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059#include "src/gpu/ops/GrDrawAtlasOp.h"
60#include "src/gpu/ops/GrDrawOp.h"
61#include "src/gpu/ops/GrDrawVerticesOp.h"
62#include "src/gpu/ops/GrDrawableOp.h"
63#include "src/gpu/ops/GrFillRRectOp.h"
64#include "src/gpu/ops/GrFillRectOp.h"
65#include "src/gpu/ops/GrLatticeOp.h"
66#include "src/gpu/ops/GrOp.h"
67#include "src/gpu/ops/GrOvalOpFactory.h"
68#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069#include "src/gpu/ops/GrShadowRRectOp.h"
70#include "src/gpu/ops/GrStencilPathOp.h"
71#include "src/gpu/ops/GrStrokeRectOp.h"
72#include "src/gpu/ops/GrTextureOp.h"
73#include "src/gpu/text/GrTextContext.h"
74#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040075
Herb Derbyc1b482c2018-08-09 15:02:27 -040076class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040077public:
78 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040079 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040081 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040082 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040083
Michael Ludwig7c12e282020-05-29 09:54:07 -040084 void addDrawOp(const GrClip* clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040085 fRenderTargetContext->addDrawOp(clip, std::move(op));
86 }
87
Michael Ludwig7c12e282020-05-29 09:54:07 -040088 void drawShape(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -040089 const SkPaint& paint,
90 const SkMatrixProvider& matrixProvider,
91 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040092 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040093 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 }
95
Brian Osman449b1152020-04-15 16:43:00 -040096 void makeGrPaint(GrMaskFormat maskFormat,
97 const SkPaint& skPaint,
98 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040099 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -0500100 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400101 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400103 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
104 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400105 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400106 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107 }
108 }
109
Robert Phillips69893702019-02-22 11:16:30 -0500110 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 return fRenderTargetContext->fContext;
112 }
113
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter* glyphPainter() override {
115 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400116 }
117
Brian Salomonf18b1d82017-10-27 11:30:49 -0400118private:
119 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400120 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400121
Brian Salomonf18b1d82017-10-27 11:30:49 -0400122};
joshualittbc907352016-01-13 06:45:40 -0800123
Robert Phillips72152832017-01-25 17:31:35 -0500124#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -0400125#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
126#define ASSERT_SINGLE_OWNER_PRIV GR_ASSERT_SINGLE_OWNER(fRenderTargetContext->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -0500127#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
128#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
129#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
130#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
131#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700132
Brian Salomone225b562017-06-14 13:00:03 -0400133//////////////////////////////////////////////////////////////////////////////
134
robertphillipsea461502015-05-26 11:38:03 -0700135class AutoCheckFlush {
136public:
halcanary9d524f22016-03-29 09:03:52 -0700137 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700138 SkASSERT(fDrawingManager);
139 }
bsalomonb77a9072016-09-07 10:02:04 -0700140 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700141
142private:
robertphillips77a2e522015-10-17 07:43:27 -0700143 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700144};
145
Greg Daniele20fcad2020-01-08 11:52:34 -0500146std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
147 GrRecordingContext* context,
148 GrColorType colorType,
149 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500150 sk_sp<GrSurfaceProxy> proxy,
151 GrSurfaceOrigin origin,
152 const SkSurfaceProps* surfaceProps,
153 bool managedOps) {
154 if (!proxy) {
155 return nullptr;
156 }
157
158 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400159 GrSwizzle readSwizzle, writeSwizzle;
160 if (colorType != GrColorType::kUnknown) {
161 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
162 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
163 }
Greg Danielba0ff782020-01-07 15:42:57 -0500164
Greg Daniel3912a4b2020-01-14 09:56:04 -0500165 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400166 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500167
168 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400169 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500170 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500171}
172
173std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
174 GrRecordingContext* context,
175 GrColorType colorType,
176 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500177 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500178 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 const GrBackendFormat& format,
180 int sampleCnt,
181 GrMipMapped mipMapped,
182 GrProtected isProtected,
183 GrSurfaceOrigin origin,
184 SkBudgeted budgeted,
185 const SkSurfaceProps* surfaceProps) {
186 // It is probably not necessary to check if the context is abandoned here since uses of the
187 // GrRenderTargetContext which need the context will mostly likely fail later on without an
188 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
189 // an abandoned context correctly. It also lets us early out of some extra work.
190 if (context->priv().abandoned()) {
191 return nullptr;
192 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500193
Greg Daniele20fcad2020-01-08 11:52:34 -0500194 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400195 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500196 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500197 if (!proxy) {
198 return nullptr;
199 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500200
Greg Danielba0ff782020-01-07 15:42:57 -0500201 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
202 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500203 if (!rtc) {
204 return nullptr;
205 }
206 rtc->discard();
207 return rtc;
208}
209
210std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
211 GrRecordingContext* context,
212 GrColorType colorType,
213 sk_sp<SkColorSpace> colorSpace,
214 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500215 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500216 int sampleCnt,
217 GrMipMapped mipMapped,
218 GrProtected isProtected,
219 GrSurfaceOrigin origin,
220 SkBudgeted budgeted,
221 const SkSurfaceProps* surfaceProps) {
222 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
223 if (!format.isValid()) {
224 return nullptr;
225 }
226
227 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
228 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
229 surfaceProps);
230}
231
232static inline GrColorType color_type_fallback(GrColorType ct) {
233 switch (ct) {
234 // kRGBA_8888 is our default fallback for many color types that may not have renderable
235 // backend formats.
236 case GrColorType::kAlpha_8:
237 case GrColorType::kBGR_565:
238 case GrColorType::kABGR_4444:
239 case GrColorType::kBGRA_8888:
240 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400241 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500242 case GrColorType::kRGBA_F16:
243 case GrColorType::kRGBA_F16_Clamped:
244 return GrColorType::kRGBA_8888;
245 case GrColorType::kAlpha_F16:
246 return GrColorType::kRGBA_F16;
247 case GrColorType::kGray_8:
248 return GrColorType::kRGB_888x;
249 default:
250 return GrColorType::kUnknown;
251 }
252}
253
Brian Salomond005b692020-04-01 15:47:05 -0400254std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400255 GrImageContext* context, GrColorType colorType, int sampleCnt) {
256 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400257 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400258 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
259 // We continue to the fallback color type if there no default renderable format or we
260 // requested msaa and the format doesn't support msaa.
261 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400262 return {colorType, format};
263 }
264 colorType = color_type_fallback(colorType);
265 } while (colorType != GrColorType::kUnknown);
266 return {GrColorType::kUnknown, {}};
267}
268
Greg Daniele20fcad2020-01-08 11:52:34 -0500269std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
270 GrRecordingContext* context,
271 GrColorType colorType,
272 sk_sp<SkColorSpace> colorSpace,
273 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500274 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500275 int sampleCnt,
276 GrMipMapped mipMapped,
277 GrProtected isProtected,
278 GrSurfaceOrigin origin,
279 SkBudgeted budgeted,
280 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400281 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400282 if (ct == GrColorType::kUnknown) {
283 return nullptr;
284 }
285 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
286 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500287}
288
Greg Danielba0ff782020-01-07 15:42:57 -0500289std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
290 GrRecordingContext* context,
291 GrColorType colorType,
292 sk_sp<SkColorSpace> colorSpace,
293 const GrBackendTexture& tex,
294 int sampleCnt,
295 GrSurfaceOrigin origin,
296 const SkSurfaceProps* surfaceProps,
297 ReleaseProc releaseProc,
298 ReleaseContext releaseCtx) {
299 SkASSERT(sampleCnt > 0);
300 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400301 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500302 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500303 if (!proxy) {
304 return nullptr;
305 }
306
307 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
308 origin, surfaceProps);
309}
310
311std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
312 GrRecordingContext* context,
313 GrColorType colorType,
314 sk_sp<SkColorSpace> colorSpace,
315 const GrBackendTexture& tex,
316 int sampleCnt,
317 GrSurfaceOrigin origin,
318 const SkSurfaceProps* surfaceProps) {
319 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400320 sk_sp<GrSurfaceProxy> proxy(
321 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500322 if (!proxy) {
323 return nullptr;
324 }
325
326 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
327 origin, surfaceProps);
328}
329
330std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
331 GrRecordingContext* context,
332 GrColorType colorType,
333 sk_sp<SkColorSpace> colorSpace,
334 const GrBackendRenderTarget& rt,
335 GrSurfaceOrigin origin,
336 const SkSurfaceProps* surfaceProps,
337 ReleaseProc releaseProc,
338 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400339 sk_sp<GrSurfaceProxy> proxy(
340 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500341 if (!proxy) {
342 return nullptr;
343 }
344
345 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
346 origin, surfaceProps);
347}
348
349std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
350 GrRecordingContext* context,
351 const SkImageInfo& imageInfo,
352 const GrVkDrawableInfo& vkInfo,
353 const SkSurfaceProps* props) {
354 sk_sp<GrSurfaceProxy> proxy(
355 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
356 vkInfo));
357 if (!proxy) {
358 return nullptr;
359 }
360
Greg Danielba0ff782020-01-07 15:42:57 -0500361 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
362 imageInfo.refColorSpace(), std::move(proxy),
363 kTopLeft_GrSurfaceOrigin, props);
364}
365
Greg Danielf41b2bd2019-08-22 16:19:24 -0400366// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
367// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
368// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
369// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500370GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500371 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400372 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400373 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400374 sk_sp<SkColorSpace> colorSpace,
375 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400376 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500377 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
378 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400379 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500380 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400381 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400382 , fManagedOpsTask(managedOpsTask) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400383 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400384 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400385 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400386 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
387 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500388
Brian Salomonf18b1d82017-10-27 11:30:49 -0400389 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700390 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700391}
392
robertphillips2e1e51f2015-10-15 08:01:48 -0700393#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500394void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400395 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400396 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700397 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700398}
399#endif
400
Brian Osman11052242016-10-27 14:47:55 -0400401GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800402 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400403 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400404 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400405 }
robertphillipsa106c622015-10-16 09:07:06 -0700406}
407
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600409 if (GrAA::kNo == aa) {
410 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
411 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600412 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600413 return GrAAType::kMSAA;
414 }
415 return GrAAType::kNone;
416 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600417 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600418}
419
Greg Daniele252f082017-10-23 16:05:23 -0400420GrMipMapped GrRenderTargetContext::mipMapped() const {
421 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
422 return proxy->mipMapped();
423 }
424 return GrMipMapped::kNo;
425}
426
Greg Danielf41b2bd2019-08-22 16:19:24 -0400427GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800428 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700429 SkDEBUGCODE(this->validate();)
430
Brian Salomon3b8486a2020-04-21 12:43:26 -0400431 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600432 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400433 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600434 if (fOpsTask && fNumStencilSamples > 0) {
435 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600436 fOpsTask->setMustPreserveStencil();
437 // Reload the stencil buffer content at the beginning of newOpsTask.
438 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
439 // values?
440 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
441 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400442 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600443 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700444 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400445 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400446 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700447}
448
Michael Ludwig7c12e282020-05-29 09:54:07 -0400449void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400450 const SkMatrixProvider& matrixProvider,
451 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800452 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700453 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700454 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700456
Greg Danielbe7fc462019-01-03 16:40:42 -0500457 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
458 // secondary command buffers because it would require stopping and starting a render pass which
459 // we don't have access to.
460 if (this->wrapsVkSecondaryCB()) {
461 return;
462 }
463
Herb Derby26cbe512018-05-24 14:39:01 -0400464 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400465 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400466 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700467}
468
Brian Osman11052242016-10-27 14:47:55 -0400469void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800470 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700471 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700472 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400473 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700474
Robert Phillips72152832017-01-25 17:31:35 -0500475 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400476
Greg Danielf41b2bd2019-08-22 16:19:24 -0400477 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700478}
479
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500480static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
481 paint->setColor4f(color);
482 if (color.isOpaque()) {
483 // Can just rely on the src-over blend mode to do the right thing
484 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
485 } else {
486 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
487 // were src blended
488 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
489 }
490}
491
Michael Ludwig81d41722020-05-26 16:57:38 -0400492// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
493// premul alpha type. If we ever support different alpha type render targets, this function should
494// transform the color as appropriate.
495void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500496 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400497 bool upgradePartialToFull) {
498 ASSERT_SINGLE_OWNER
499 RETURN_IF_ABANDONED
500 SkDEBUGCODE(this->validate();)
501 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
502
Michael Ludwig4926b072020-06-04 19:39:42 +0000503 // The clear will be fullscreen if no scissor is provided, or if the scissor is larger than
504 // the logical bounds of the render target, or if the special flag was provided that allows
505 // partial clears to upgrade to full (because it's a scratch resource and the caller knows
506 // anything outside the scissor doesn't matter, but if full screen clears aren't free, then
507 // the scissor is still provided so that fewer pixels are written to).
508 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
509 // only clear the entire target if we knew it had not been cleared before. As
510 // is this could end up doing a lot of redundant clears.
511 GrScissorState scissorState;
512 if (scissor) {
513 // TODO(michaelludwig) - This will get simpler when GrScissorState knows the device dims
514 scissorState.set(*scissor);
515 if (!scissorState.intersect(SkIRect::MakeWH(this->width(), this->height()))) {
516 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
517 // except clear ops are not draw ops).
518 return;
Michael Ludwig81d41722020-05-26 16:57:38 -0400519 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500520 }
Michael Ludwig4926b072020-06-04 19:39:42 +0000521 bool isFull = !scissorState.enabled() ||
522 scissorState.rect().contains(SkIRect::MakeWH(this->width(), this->height())) ||
523 (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
524 this->caps()->shouldInitializeTextures()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500525
Michael Ludwig4926b072020-06-04 19:39:42 +0000526 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400527 GrOpsTask* opsTask = this->getOpsTask();
528 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500529 !this->caps()->performColorClearsAsDraws()) {
530 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400531 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500532 return;
533 } else {
534 // Will use an op for the clear, reset the load op to discard since the op will
535 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400536 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500537 }
538
Michael Ludwig4926b072020-06-04 19:39:42 +0000539 // Must add an op to the list (either because we couldn't use a load op, or because the
540 // clear load op isn't supported)
541 if (this->caps()->performColorClearsAsDraws()) {
542 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
543 GrPaint paint;
544 clear_to_grpaint(color, &paint);
545 this->addDrawOp(nullptr,
546 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
547 rtRect));
548 } else {
549 this->addOp(GrClearOp::Make(fContext, GrScissorState(), color, this->asSurfaceProxy()));
550 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500551 } else {
Michael Ludwig4926b072020-06-04 19:39:42 +0000552 if (this->caps()->performPartialClearsAsDraws()) {
553 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
554 GrPaint paint;
555 clear_to_grpaint(color, &paint);
556
557 this->addDrawOp(nullptr,
558 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
559 SkRect::Make(scissorState.rect())));
560 } else {
561 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, scissorState, color,
562 this->asSurfaceProxy()));
563 // This version of the clear op factory can return null if the clip doesn't intersect
564 // with the surface proxy's boundary
565 if (!op) {
566 return;
567 }
568 this->addOp(std::move(op));
569 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500570 }
571}
572
Michael Ludwig7c12e282020-05-29 09:54:07 -0400573void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500574 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400575 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000576 // Start with the render target, since that is the maximum content we could possibly fill.
577 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500578 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400579 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000580 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
581 // inverse view matrix.
582 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
583 } else {
584 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
585 SkMatrix localMatrix;
586 if (!viewMatrix.invert(&localMatrix)) {
587 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400588 }
Michael Ludwig61328202019-06-19 14:48:58 +0000589 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
590 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700591 }
robertphillipsea461502015-05-26 11:38:03 -0700592}
593
Michael Ludwig61328202019-06-19 14:48:58 +0000594enum class GrRenderTargetContext::QuadOptimization {
595 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
596 kDiscarded,
597 // The rect to draw was converted to some other op and appended to the oplist, so no additional
598 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
599 // a constColor is provided.
600 kSubmitted,
601 // The clip was folded into the device quad, with updated edge flags and local coords, and
602 // caller is responsible for adding an appropriate op.
603 kClipApplied,
604 // No change to clip, but quad updated to better fit clip/render target, and caller is
605 // responsible for adding an appropriate op.
606 kCropped
607};
Michael Ludwig61a16512019-01-15 11:15:13 -0500608
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400609static bool make_vertex_finite(float* value) {
610 if (SkScalarIsNaN(*value)) {
611 return false;
612 }
613
614 if (!SkScalarIsFinite(*value)) {
615 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
616 // left when calculating crops.
617 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
618 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
619 }
620
621 return true;
622}
623
Michael Ludwig7c12e282020-05-29 09:54:07 -0400624static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
625 return clip ? clip->getConservativeBounds(rtc->width(), rtc->height())
626 : SkIRect::MakeWH(rtc->width(), rtc->height());
627}
628
Michael Ludwig61328202019-06-19 14:48:58 +0000629GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400630 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500631 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000632 // Optimization requirements:
633 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500634 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
635 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
636 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
637 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
638 // 5. kCropped in all other scenarios (although a crop may be a no-op)
639
640 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
641 // better to just keep the old flags instead of introducing mixed edge flags.
642 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000643
Michael Ludwig4926b072020-06-04 19:39:42 +0000644 SkRect rtRect;
645 if (stencilSettings) {
646 // Must use size at which the rendertarget will ultimately be allocated so that stencil
647 // buffer updates on approximately sized render targets don't get corrupted.
648 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
649 } else {
650 // Use the logical size of the render target, which allows for "fullscreen" clears even if
651 // the render target has an approximate backing fit
652 rtRect = SkRect::MakeWH(this->width(), this->height());
653 }
Michael Ludwige08b4432019-06-19 18:00:48 -0400654
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500655 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000656 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400657 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
658 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500659 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400660 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500661 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
662 !make_vertex_finite(quad->fDevice.ys() + i) ||
663 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400664 // Discard if we see a nan
665 return QuadOptimization::kDiscarded;
666 }
667 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500668 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400669 }
670 } else {
671 // CropToRect requires the quads to be finite. If they are not finite and we have local
672 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500673 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400674 return QuadOptimization::kDiscarded;
675 }
Brian Salomon7694b902019-06-18 21:00:21 +0000676 }
677
Michael Ludwig61328202019-06-19 14:48:58 +0000678 // If the quad is entirely off screen, it doesn't matter what the clip does
679 if (!rtRect.intersects(drawBounds)) {
680 return QuadOptimization::kDiscarded;
681 }
Brian Salomon7694b902019-06-18 21:00:21 +0000682
Michael Ludwig61328202019-06-19 14:48:58 +0000683 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
684 // the render target).
685 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400686 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
687 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
688 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
689 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000690 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400691 if (clip && !clip->quickContains(rtRect)) {
692 if (!clip->isRRect(rtRect, &clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000693 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000694 }
Brian Salomon7694b902019-06-18 21:00:21 +0000695 }
696
Michael Ludwig61328202019-06-19 14:48:58 +0000697 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
698 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400699 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000700 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
701 // of the render target and the clip rect)
702 SkRect clipBounds = rtRect;
703 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
704 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000705 }
706
Michael Ludwig61328202019-06-19 14:48:58 +0000707 if (clipRRect.isRect()) {
708 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500709 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700710 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500711 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000712 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500713 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000714 if (drawBounds.contains(rtRect)) {
715 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400716 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000717 return QuadOptimization::kSubmitted;
718 } else if (GrClip::IsPixelAligned(drawBounds) &&
719 drawBounds.width() > 256 && drawBounds.height() > 256) {
720 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
721 SkIRect scissorRect;
722 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400723 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000724 return QuadOptimization::kSubmitted;
725 }
726 }
727
728 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000729 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500730 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000731 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
732 // properly reflect the smooth edge of the clip.
733 *aa = GrAA::kYes;
734 }
735 // We intentionally do not downgrade AA here because we don't know if we need to
736 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
737 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
738
739 // deviceQuad is exactly the intersection of original quad and clip, so it can be
740 // drawn with no clip (submitted by caller)
741 return QuadOptimization::kClipApplied;
742 } else {
743 // The quads have been updated to better fit the clip bounds, but can't get rid of
744 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500745 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000746 return QuadOptimization::kCropped;
747 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700748 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000749 // Rounded corners and constant filled color (limit ourselves to solid colors because
750 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500751 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
752 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
753 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000754 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
755 // we can draw the rrect directly and ignore the edge flags
756 GrPaint paint;
757 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400758 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000759 clipRRect, GrStyle::SimpleFill());
760 return QuadOptimization::kSubmitted;
761 } else {
762 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500763 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000764 return QuadOptimization::kCropped;
765 }
Brian Salomon7694b902019-06-18 21:00:21 +0000766 }
Brian Salomon7694b902019-06-18 21:00:21 +0000767 }
768
Michael Ludwig61328202019-06-19 14:48:58 +0000769 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400770 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000771
772 // One final check for discarding, since we may have gone here directly due to a complex clip
773 if (!clipBounds.intersects(drawBounds)) {
774 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000775 }
776
Michael Ludwig61328202019-06-19 14:48:58 +0000777 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
778 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500779 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
780 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000781
782 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000783}
784
Michael Ludwig7c12e282020-05-29 09:54:07 -0400785void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500786 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500787 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500788 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400789 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000790 ASSERT_SINGLE_OWNER
791 RETURN_IF_ABANDONED
792 SkDEBUGCODE(this->validate();)
793 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500794
Michael Ludwig61328202019-06-19 14:48:58 +0000795 AutoCheckFlush acf(this->drawingManager());
796
797 SkPMColor4f* constColor = nullptr;
798 SkPMColor4f paintColor;
799 if (!ss && !paint.numCoverageFragmentProcessors() &&
800 paint.isConstantBlendedColor(&paintColor)) {
801 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
802 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500803 }
804
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500805 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000806 if (opt >= QuadOptimization::kClipApplied) {
807 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400808 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000809 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
810 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500811 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
812 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700813 }
Michael Ludwig61328202019-06-19 14:48:58 +0000814 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800815}
816
Michael Ludwig7c12e282020-05-29 09:54:07 -0400817void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400818 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500819 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000820 sk_sp<GrColorSpaceXform> textureXform,
821 GrSamplerState::Filter filter,
822 const SkPMColor4f& color,
823 SkBlendMode blendMode,
824 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500825 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400826 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000827 ASSERT_SINGLE_OWNER
828 RETURN_IF_ABANDONED
829 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400830 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000831 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
832
833 AutoCheckFlush acf(this->drawingManager());
834
835 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
836 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500837 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000838
839 SkASSERT(opt != QuadOptimization::kSubmitted);
840 if (opt != QuadOptimization::kDiscarded) {
841 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400842 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000843 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400844 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400845 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
846 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400847 // Use the provided subset, although hypothetically we could detect that the cropped local
848 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500849 this->addDrawOp(finalClip,
850 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
851 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400852 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000853 }
854}
855
Michael Ludwig7c12e282020-05-29 09:54:07 -0400856void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500857 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500858 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400859 const SkMatrix& viewMatrix,
860 const SkRect& rect,
861 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700862 if (!style) {
863 style = &GrStyle::SimpleFill();
864 }
joshualitt1de610a2016-01-06 08:26:09 -0800865 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700866 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700867 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400868 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700869
bsalomon6663acf2016-05-10 09:14:17 -0700870 // Path effects should've been devolved to a path in SkGpuDevice
871 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700872
Robert Phillips72152832017-01-25 17:31:35 -0500873 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700874
bsalomon6663acf2016-05-10 09:14:17 -0700875 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400876 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000877 // Fills the rect, using rect as its own local coordinates
878 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500879 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400880 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
881 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
882 (rect.width() && rect.height())) {
883 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
884 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400885 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700886
Chris Dalton7d6748e2019-03-13 00:34:52 -0600887 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500888 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
889 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
890 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500891 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400892 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700893 return;
robertphillips4bc31812016-03-01 12:22:49 -0800894 }
robertphillips4bc31812016-03-01 12:22:49 -0800895 }
Mike Klein16885072018-12-11 09:54:31 -0500896 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000897 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
898 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700899}
900
Michael Ludwig7c12e282020-05-29 09:54:07 -0400901void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500902 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
903 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600904 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500905
906 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
907 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500908}
909
Robert Phillipsec2249f2016-11-09 08:54:35 -0500910int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500911 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400912 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500913}
914
Greg Danielf41b2bd2019-08-22 16:19:24 -0400915GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600916 ) const {
917#if GR_TEST_UTILS
918 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400919 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600920 }
921#endif
922 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
923 // would normally be overwritten. The one exception is if the render target context is marked as
924 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
925 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
926 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
927 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400928 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600929}
930
Chris Dalton858cf232019-10-14 16:20:00 -0600931void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600932 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600933 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600934 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600935
Chris Daltoneffee202019-07-01 22:28:03 -0600936 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600937 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500938 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600939 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400940 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600941 }
942 SkASSERT(numRequiredSamples > 0);
943
944 if (numRequiredSamples > fNumStencilSamples) {
945 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500946 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600947 }
Chris Dalton6b982802019-06-27 13:53:46 -0600948
Chris Dalton674f77a2019-09-30 20:49:39 -0600949 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600950 if (this->caps()->performStencilClearsAsDraws()) {
951 // There is a driver bug with clearing stencil. We must use an op to manually clear the
952 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400953 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600954 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600955 this->getOpsTask()->setInitialStencilContent(
956 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600957 }
958 }
959}
960
Michael Ludwig81d41722020-05-26 16:57:38 -0400961void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600962 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
963
Michael Ludwig1e632792020-05-21 12:45:31 -0400964 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwig4926b072020-06-04 19:39:42 +0000965 (scissor && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400966 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500967 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwig4926b072020-06-04 19:39:42 +0000968 SkRect rect = scissor ? SkRect::Make(*scissor)
969 : SkRect::MakeWH(this->width(), this->height());
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500970
971 // Configure the paint to have no impact on the color buffer
972 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400973 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwig4926b072020-06-04 19:39:42 +0000974 this->addDrawOp(nullptr, GrFillRectOp::MakeNonAARect(fContext, std::move(paint),
975 SkMatrix::I(), rect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500976 } else {
Michael Ludwig4926b072020-06-04 19:39:42 +0000977 GrScissorState scissorState;
978 if (scissor) {
979 scissorState.set(*scissor);
980 }
981
982 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
983 fContext, scissorState, insideStencilMask, this->asRenderTargetProxy()));
984 if (!op) {
985 return;
986 }
987 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500988 }
robertphillips976f5f02016-06-03 10:59:20 -0700989}
990
Michael Ludwig7c12e282020-05-29 09:54:07 -0400991void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600992 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400993 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400994 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500995 ASSERT_SINGLE_OWNER_PRIV
996 RETURN_IF_ABANDONED_PRIV
997 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400998 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
999 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001000
Brian Salomon467921e2017-03-06 16:17:12 -05001001 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1002 SkASSERT(path);
1003 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1004
1005 // FIXME: Use path bounds instead of this WAR once
1006 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1007 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1008
Michael Ludwig7c12e282020-05-29 09:54:07 -04001009 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
1010 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
1011 // during this clip application.
Michael Ludwig4926b072020-06-04 19:39:42 +00001012 GrAppliedHardClip appliedClip;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001013 if (clip && !clip->apply(fRenderTargetContext->width(), fRenderTargetContext->height(),
1014 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001015 return;
1016 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001017 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1018 // but as it is, we're just using the full render target so intersecting the two bounds would
1019 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001020
Robert Phillips7c525e62018-06-12 10:11:12 -04001021 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1022 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001023 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001024 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001025 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001026 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001027 if (!op) {
1028 return;
1029 }
Brian Salomon97180af2017-03-14 13:42:58 -04001030 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001031
Chris Daltoneffee202019-07-01 22:28:03 -06001032 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001033 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001034}
1035
Michael Ludwig7c12e282020-05-29 09:54:07 -04001036void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001037 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001038 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001039 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1040 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001041 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001042 ASSERT_SINGLE_OWNER
1043 RETURN_IF_ABANDONED
1044 SkDEBUGCODE(this->validate();)
1045 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001046
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001047 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1048 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1049 AutoCheckFlush acf(this->drawingManager());
1050 GrAAType aaType = this->chooseAAType(aa);
1051 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1052 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1053 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001054 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1055 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001056}
1057
Michael Ludwig7c12e282020-05-29 09:54:07 -04001058void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001059 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001060 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001061 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001062 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001063 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001064 ASSERT_SINGLE_OWNER
1065 RETURN_IF_ABANDONED
1066 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001067 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001068
1069 AutoCheckFlush acf(this->drawingManager());
1070
1071 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001072 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001073 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001074 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001075 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001076 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001077 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001078}
1079
1080///////////////////////////////////////////////////////////////////////////////
1081
Michael Ludwig7c12e282020-05-29 09:54:07 -04001082void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001083 GrPaint&& paint,
1084 const SkMatrix& viewMatrix,
1085 int spriteCount,
1086 const SkRSXform xform[],
1087 const SkRect texRect[],
1088 const SkColor colors[]) {
1089 ASSERT_SINGLE_OWNER
1090 RETURN_IF_ABANDONED
1091 SkDEBUGCODE(this->validate();)
1092 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1093
1094 AutoCheckFlush acf(this->drawingManager());
1095
1096 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1097 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1098 aaType, spriteCount, xform, texRect, colors);
1099 this->addDrawOp(clip, std::move(op));
1100}
1101
1102///////////////////////////////////////////////////////////////////////////////
1103
Michael Ludwig7c12e282020-05-29 09:54:07 -04001104void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001105 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001106 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001107 const SkMatrix& viewMatrix,
1108 const SkRRect& rrect,
1109 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001110 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001111 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001112 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001113 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001114
1115 const SkStrokeRec& stroke = style.strokeRec();
1116 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001117 return;
1118 }
1119
Michael Ludwig7c12e282020-05-29 09:54:07 -04001120 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001121#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1122 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001123 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001124 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1125 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001126 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1127 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001128 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001129 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1130 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1131 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001132 }
1133#endif
bsalomon6663acf2016-05-10 09:14:17 -07001134 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001135
Robert Phillips72152832017-01-25 17:31:35 -05001136 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001137
Chris Dalton7d6748e2019-03-13 00:34:52 -06001138 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001139
Chris Dalton0dffbab2019-03-27 13:08:50 -06001140 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001141 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1142 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1143 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1144 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1145 // to avoid perf regressions on some platforms.
1146 assert_alive(paint);
1147 op = GrOvalOpFactory::MakeCircularRRectOp(
1148 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1149 }
1150 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001151 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001152 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001153 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001154 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001155 assert_alive(paint);
1156 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001157 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001158 }
1159 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001160 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001161 return;
robertphillipsea461502015-05-26 11:38:03 -07001162 }
robertphillipsb56f9272016-02-25 11:03:52 -08001163
Mike Klein16885072018-12-11 09:54:31 -05001164 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001165 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001166 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001167}
1168
Jim Van Verthc5903412016-11-17 15:27:09 -05001169///////////////////////////////////////////////////////////////////////////////
1170
Jim Van Verth3af1af92017-05-18 15:06:54 -04001171static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1172 SkPoint3 result;
1173 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1174 result.fZ = pt.fZ;
1175 return result;
1176}
1177
Michael Ludwig7c12e282020-05-29 09:54:07 -04001178bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001179 const SkMatrix& viewMatrix,
1180 const SkPath& path,
1181 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001182 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001183 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001184 return true;
1185 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001186 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001187 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001188
1189 // check z plane
1190 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1191 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1192 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1193 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1194 return false;
1195 }
1196
1197 SkRRect rrect;
1198 SkRect rect;
1199 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001200 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001201 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1202 if (!isRRect &&
1203 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1204 rect.width() > SK_ScalarNearlyZero) {
1205 rrect.setOval(rect);
1206 isRRect = true;
1207 }
1208 if (!isRRect && path.isRect(&rect)) {
1209 rrect.setRect(rect);
1210 isRRect = true;
1211 }
1212
1213 if (!isRRect) {
1214 return false;
1215 }
1216
Jim Van Verthc5903412016-11-17 15:27:09 -05001217 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001218 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001219 }
1220
Robert Phillips72152832017-01-25 17:31:35 -05001221 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001222
Jim Van Verth3af1af92017-05-18 15:06:54 -04001223 // transform light
1224 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1225
1226 // 1/scale
1227 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001228 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001229 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1230 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1231
1232 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001233 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1234
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001235 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001236 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1237 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1238 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001239
1240 // Outset the shadow rrect to the border of the penumbra
1241 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1242 SkRRect ambientRRect;
1243 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1244 // If the rrect was an oval then its outset will also be one.
1245 // We set it explicitly to avoid errors.
1246 if (rrect.isOval()) {
1247 ambientRRect = SkRRect::MakeOval(outsetRect);
1248 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001249 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001250 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1251 }
1252
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001253 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 if (transparent) {
1255 // set a large inset to force a fill
1256 devSpaceInsetWidth = ambientRRect.width();
1257 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001258
Robert Phillips7c525e62018-06-12 10:11:12 -04001259 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1260 ambientColor,
1261 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001262 ambientRRect,
1263 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001264 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001265 if (op) {
1266 this->addDrawOp(clip, std::move(op));
1267 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001268 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001269
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001270 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001271 SkScalar devSpaceSpotBlur;
1272 SkScalar spotScale;
1273 SkVector spotOffset;
1274 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1275 devLightPos.fZ, rec.fLightRadius,
1276 &devSpaceSpotBlur, &spotScale, &spotOffset);
1277 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001278 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1279
Jim Van Verth3af1af92017-05-18 15:06:54 -04001280 // Adjust translate for the effect of the scale.
1281 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1282 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1283 // This offset is in dev space, need to transform it into source space.
1284 SkMatrix ctmInverse;
1285 if (viewMatrix.invert(&ctmInverse)) {
1286 ctmInverse.mapPoints(&spotOffset, 1);
1287 } else {
1288 // Since the matrix is a similarity, this should never happen, but just in case...
1289 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1290 SkASSERT(false);
1291 }
1292
1293 // Compute the transformed shadow rrect
1294 SkRRect spotShadowRRect;
1295 SkMatrix shadowTransform;
1296 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1297 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001298 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001299
1300 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001301 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001302 SkScalar insetWidth = blurOutset;
1303 if (transparent) {
1304 // If transparent, just do a fill
1305 insetWidth += spotShadowRRect.width();
1306 } else {
1307 // For shadows, instead of using a stroke we specify an inset from the penumbra
1308 // border. We want to extend this inset area so that it meets up with the caster
1309 // geometry. The inset geometry will by default already be inset by the blur width.
1310 //
1311 // We compare the min and max corners inset by the radius between the original
1312 // rrect and the shadow rrect. The distance between the two plus the difference
1313 // between the scaled radius and the original radius gives the distance from the
1314 // transformed shadow shape to the original shape in that corner. The max
1315 // of these gives the maximum distance we need to cover.
1316 //
1317 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1318 // that to get the full insetWidth.
1319 SkScalar maxOffset;
1320 if (rrect.isRect()) {
1321 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001322 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001323 rrect.rect().fLeft),
1324 SkTAbs(spotShadowRRect.rect().fTop -
1325 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001326 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001327 rrect.rect().fRight),
1328 SkTAbs(spotShadowRRect.rect().fBottom -
1329 rrect.rect().fBottom)));
1330 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001331 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001332 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1333 rrect.rect().fLeft + dr,
1334 spotShadowRRect.rect().fTop -
1335 rrect.rect().fTop + dr);
1336 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1337 rrect.rect().fRight - dr,
1338 spotShadowRRect.rect().fBottom -
1339 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001340 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001341 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 }
Brian Osman788b9162020-02-07 10:36:46 -05001343 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001344 }
1345
1346 // Outset the shadow rrect to the border of the penumbra
1347 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1348 if (spotShadowRRect.isOval()) {
1349 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1350 } else {
1351 SkScalar outsetRad = spotRadius + blurOutset;
1352 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1353 }
1354
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001355 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001356
Robert Phillips7c525e62018-06-12 10:11:12 -04001357 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1358 spotColor,
1359 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001360 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001361 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001362 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001363 if (op) {
1364 this->addDrawOp(clip, std::move(op));
1365 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366 }
1367
1368 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001369}
1370
1371///////////////////////////////////////////////////////////////////////////////
1372
Michael Ludwig7c12e282020-05-29 09:54:07 -04001373bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001374 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001375 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001376 const SkMatrix& viewMatrix,
1377 const SkRRect& origOuter,
1378 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001379 SkASSERT(!origInner.isEmpty());
1380 SkASSERT(!origOuter.isEmpty());
1381
Brian Salomon65749212017-12-01 16:01:47 -05001382 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1383
Chris Dalton7d6748e2019-03-13 00:34:52 -06001384 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001385
1386 if (GrAAType::kMSAA == aaType) {
1387 return false;
1388 }
1389
Greg Daniel2655ede2019-04-10 00:49:28 +00001390 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1391 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001392 auto outerR = outer->width() / 2.f;
1393 auto innerR = inner->width() / 2.f;
1394 auto cx = outer->getBounds().fLeft + outerR;
1395 auto cy = outer->getBounds().fTop + outerR;
1396 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1397 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1398 auto avgR = (innerR + outerR) / 2.f;
1399 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1400 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1401 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001402 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001403 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001404 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001405 if (op) {
1406 this->addDrawOp(clip, std::move(op));
1407 return true;
1408 }
Mike Klein16885072018-12-11 09:54:31 -05001409 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001410 }
1411 }
1412
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001413 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001414 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001415 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1416 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001417 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001418 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1419 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001420 }
robertphillips00095892016-02-29 13:50:40 -08001421
robertphillips00095892016-02-29 13:50:40 -08001422 SkMatrix inverseVM;
1423 if (!viewMatrix.isIdentity()) {
1424 if (!origInner.transform(viewMatrix, inner.writable())) {
1425 return false;
1426 }
1427 if (!origOuter.transform(viewMatrix, outer.writable())) {
1428 return false;
1429 }
1430 if (!viewMatrix.invert(&inverseVM)) {
1431 return false;
1432 }
1433 } else {
1434 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001435 }
robertphillips00095892016-02-29 13:50:40 -08001436
Ethan Nicholaseace9352018-10-15 20:09:54 +00001437 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001438 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001439 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001440 if (!innerEffect) {
1441 return false;
1442 }
1443
Ethan Nicholaseace9352018-10-15 20:09:54 +00001444 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001445 if (!outerEffect) {
1446 return false;
1447 }
1448
Brian Salomon82f44312017-01-11 13:42:54 -05001449 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1450 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001451
1452 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001453 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001454 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1455 }
halcanary9d524f22016-03-29 09:03:52 -07001456
Brian Salomon82f44312017-01-11 13:42:54 -05001457 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1458 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001459 return true;
1460}
1461
Michael Ludwig7c12e282020-05-29 09:54:07 -04001462void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001463 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001464 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001465 const SkMatrix& viewMatrix,
1466 const SkRRect& outer,
1467 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001468 ASSERT_SINGLE_OWNER
1469 RETURN_IF_ABANDONED
1470 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001471 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001472
1473 SkASSERT(!outer.isEmpty());
1474 SkASSERT(!inner.isEmpty());
1475
Robert Phillips72152832017-01-25 17:31:35 -05001476 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001477
Brian Salomon82f44312017-01-11 13:42:54 -05001478 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001479 return;
1480 }
Mike Klein16885072018-12-11 09:54:31 -05001481 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001482
1483 SkPath path;
1484 path.setIsVolatile(true);
1485 path.addRRect(inner);
1486 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001487 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001488 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001489}
1490
robertphillipsea461502015-05-26 11:38:03 -07001491///////////////////////////////////////////////////////////////////////////////
1492
Michael Ludwig7c12e282020-05-29 09:54:07 -04001493void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001494 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001495 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001496 const SkMatrix& viewMatrix,
1497 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001498 const GrStyle& style,
1499 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001500 ASSERT_SINGLE_OWNER
1501 RETURN_IF_ABANDONED
1502 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001503 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001504
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001505 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001506 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001507 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001508 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001509 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1510 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001511 aa = GrAA::kNo;
1512 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001513 }
msarettcc319b92016-08-25 18:07:18 -07001514 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001515 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001516 SkPath path;
1517 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001518 path.setIsVolatile(true);
1519
Brian Salomon82f44312017-01-11 13:42:54 -05001520 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001521 }
1522
Chris Dalton7d6748e2019-03-13 00:34:52 -06001523 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001524 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1525 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001526 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001527}
1528
Michael Ludwig7c12e282020-05-29 09:54:07 -04001529void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001530 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001531 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001532 const SkMatrix& viewMatrix,
1533 const SkRect& oval,
1534 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001535 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001536 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001537 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001538 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001539
Robert Phillips7484d202018-07-03 09:09:08 -04001540 const SkStrokeRec& stroke = style.strokeRec();
1541
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001542 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001543 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1544 return;
1545 }
1546
1547 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001548 return;
robertphillipsea461502015-05-26 11:38:03 -07001549 }
1550
Robert Phillips72152832017-01-25 17:31:35 -05001551 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001552
Chris Dalton7d6748e2019-03-13 00:34:52 -06001553 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001554
1555 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001556 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1557 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001558 // We don't draw true circles as round rects in coverage mode, because it can
1559 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1560 assert_alive(paint);
1561 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1562 this->caps()->shaderCaps());
1563 }
1564 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001565 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1566 // the arc equation. This same special geometry and fragment branch also turn out to be a
1567 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1568 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1569 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001570 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001571 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1572 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001573 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001574 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001575 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001576 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1577 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001578 }
1579 if (op) {
1580 this->addDrawOp(clip, std::move(op));
1581 return;
robertphillipsea461502015-05-26 11:38:03 -07001582 }
robertphillipsb56f9272016-02-25 11:03:52 -08001583
Mike Klein16885072018-12-11 09:54:31 -05001584 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001585 this->drawShapeUsingPathRenderer(
1586 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001587 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001588}
1589
Michael Ludwig7c12e282020-05-29 09:54:07 -04001590void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001591 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001592 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001593 const SkMatrix& viewMatrix,
1594 const SkRect& oval,
1595 SkScalar startAngle,
1596 SkScalar sweepAngle,
1597 bool useCenter,
1598 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001599 ASSERT_SINGLE_OWNER
1600 RETURN_IF_ABANDONED
1601 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001602 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001603
1604 AutoCheckFlush acf(this->drawingManager());
1605
Chris Dalton7d6748e2019-03-13 00:34:52 -06001606 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001607 if (GrAAType::kCoverage == aaType) {
1608 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1609 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1610 std::move(paint),
1611 viewMatrix,
1612 oval,
1613 startAngle,
1614 sweepAngle,
1615 useCenter,
1616 style,
1617 shaderCaps);
1618 if (op) {
1619 this->addDrawOp(clip, std::move(op));
1620 return;
1621 }
1622 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001623 }
Brian Salomone4949402018-04-26 15:22:04 -04001624 this->drawShapeUsingPathRenderer(
1625 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001626 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001627}
1628
Michael Ludwig7c12e282020-05-29 09:54:07 -04001629void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001630 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001631 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001632 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001633 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001634 sk_sp<GrColorSpaceXform> csxf,
1635 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001636 std::unique_ptr<SkLatticeIter> iter,
1637 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001638 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001639 RETURN_IF_ABANDONED
1640 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001641 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001642
Robert Phillips72152832017-01-25 17:31:35 -05001643 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001644
Brian Salomon2a943df2018-05-04 13:43:19 -04001645 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001646 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001647 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001648 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001649}
1650
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001651void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1652 const SkRect& bounds) {
1653 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1654 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001655 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001656}
1657
Brian Salomon031b0ba2019-05-23 11:05:26 -04001658void GrRenderTargetContext::asyncRescaleAndReadPixels(
1659 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1660 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1661 auto direct = fContext->priv().asDirectContext();
1662 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001663 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001664 return;
1665 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001666 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001667 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001668 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001669 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001670 if (this->asRenderTargetProxy()->framebufferOnly()) {
1671 callback(context, nullptr);
1672 return;
1673 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001674 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001675 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001676 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001677 return;
1678 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001679 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001680 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001681 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001682 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001683 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001684 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1685 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001686 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001687 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001688 backendFormatOfFinalContext, dstCT);
1689 // Fail if we can't read from the source surface's color type.
1690 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001691 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001692 return;
1693 }
Brian Salomon624f9062019-07-02 14:23:00 -04001694 // Fail if read color type does not have all of dstCT's color channels and those missing color
1695 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001696 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1697 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1698 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1699 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001700 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001701 return;
1702 }
1703
Brian Salomonbf6b9792019-08-21 09:38:10 -04001704 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001705 int x = srcRect.fLeft;
1706 int y = srcRect.fTop;
1707 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001708 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1709 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001710 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001711 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001712 return;
1713 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001714 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001715 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 x = y = 0;
1717 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001718 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1719 this->colorInfo().alphaType(),
1720 info.colorSpace(),
1721 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001722 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1723 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001724 // We flip or color convert by drawing and we don't currently support drawing to
1725 // kPremul.
1726 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001727 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001728 return;
1729 }
Greg Daniel40903af2020-01-30 14:55:05 -05001730 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001731 SkRect srcRectToDraw = SkRect::Make(srcRect);
1732 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001733 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001734 texProxyView =
1735 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1736 SkBackingFit::kApprox, SkBudgeted::kNo);
1737 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001738 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001739 return;
1740 }
Brian Salomonc5243782020-04-02 12:50:34 -04001741 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001742 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1743 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001744 tempRTC = GrRenderTargetContext::Make(
1745 direct, this->colorInfo().colorType(), info.refColorSpace(),
1746 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001747 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001748 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001749 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001750 return;
1751 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001752 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001753 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1754 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001755 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1756 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1757 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001758 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001759 }
1760 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001761 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001762 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1763 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001764}
1765
Brian Salomon9241a6d2019-10-03 13:26:54 -04001766class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1767public:
1768 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1769 ~AsyncReadResult() override {
1770 for (int i = 0; i < fPlanes.count(); ++i) {
1771 if (!fPlanes[i].fMappedBuffer) {
1772 delete[] static_cast<const char*>(fPlanes[i].fData);
1773 } else {
1774 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1775 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1776 }
1777 }
1778 }
1779
1780 int count() const override { return fPlanes.count(); }
1781 const void* data(int i) const override { return fPlanes[i].fData; }
1782 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1783
1784 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001785 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001786 size_t rowBytes,
1787 GrClientMappedBufferManager* manager) {
1788 SkASSERT(!result.fTransferBuffer->isMapped());
1789 const void* mappedData = result.fTransferBuffer->map();
1790 if (!mappedData) {
1791 return false;
1792 }
1793 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001794 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001795 result.fPixelConverter(convertedData.get(), mappedData);
1796 this->addCpuPlane(std::move(convertedData), rowBytes);
1797 result.fTransferBuffer->unmap();
1798 } else {
1799 manager->insert(result.fTransferBuffer);
1800 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1801 }
1802 return true;
1803 }
1804
1805 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1806 SkASSERT(data);
1807 SkASSERT(rowBytes > 0);
1808 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1809 }
1810
1811private:
1812 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1813 SkASSERT(data);
1814 SkASSERT(rowBytes > 0);
1815 SkASSERT(mappedBuffer);
1816 SkASSERT(mappedBuffer->isMapped());
1817 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1818 }
1819
1820 struct Plane {
1821 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1822 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1823 const void* fData;
1824 size_t fRowBytes;
1825 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1826 sk_sp<GrGpuBuffer> fMappedBuffer;
1827 };
1828 SkSTArray<3, Plane> fPlanes;
1829 uint32_t fInboxID;
1830};
1831
Brian Salomon024bd002019-06-11 11:38:16 -04001832void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1833 ReadPixelsCallback callback,
1834 ReadPixelsContext context) {
1835 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1836 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1837
Greg Danielca9dbe22020-01-02 13:44:30 -05001838 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001839 callback(context, nullptr);
1840 return;
1841 }
1842
Brian Salomon9241a6d2019-10-03 13:26:54 -04001843 auto directContext = fContext->priv().asDirectContext();
1844 SkASSERT(directContext);
1845 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1846
Brian Salomon024bd002019-06-11 11:38:16 -04001847 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1848
1849 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001850 auto ii = SkImageInfo::Make(rect.size(), colorType,
1851 this->colorInfo().alphaType(),
1852 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001853 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001854 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1855 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1856 result->addCpuPlane(std::move(data), pm.rowBytes());
1857
Brian Salomon1d435302019-07-01 13:05:28 -04001858 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001859 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001860 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001861 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001862 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001863 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001864 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001865
Brian Salomonab32f652019-05-10 14:24:50 -04001866 struct FinishContext {
1867 ReadPixelsCallback* fClientCallback;
1868 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001869 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001870 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001871 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001872 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001873 };
1874 // Assumption is that the caller would like to flush. We could take a parameter or require an
1875 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1876 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001877 auto* finishContext = new FinishContext{callback,
1878 context,
1879 rect.size(),
1880 colorType,
1881 mappedBufferManager,
1882 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001883 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001884 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001885 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001886 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1887 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1888 context->fMappedBufferManager)) {
1889 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001890 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001891 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001892 delete context;
1893 };
1894 GrFlushInfo flushInfo;
1895 flushInfo.fFinishedContext = finishContext;
1896 flushInfo.fFinishedProc = finishCallback;
1897 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1898}
1899
Brian Salomon9241a6d2019-10-03 13:26:54 -04001900void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1901 sk_sp<SkColorSpace> dstColorSpace,
1902 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001903 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001904 RescaleGamma rescaleGamma,
1905 SkFilterQuality rescaleQuality,
1906 ReadPixelsCallback callback,
1907 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001908 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1909 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001910 SkASSERT(!dstSize.isZero());
1911 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1912
Brian Salomon024bd002019-06-11 11:38:16 -04001913 auto direct = fContext->priv().asDirectContext();
1914 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001915 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001916 return;
1917 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001918 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001920 return;
1921 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001922 if (this->asRenderTargetProxy()->framebufferOnly()) {
1923 callback(context, nullptr);
1924 return;
1925 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001926 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001927 callback(context, nullptr);
1928 return;
1929 }
Brian Salomon024bd002019-06-11 11:38:16 -04001930 int x = srcRect.fLeft;
1931 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001932 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001933 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001934 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001935 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001937 dstColorSpace);
1938 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001939 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1940 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001941 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001942 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001943 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001944 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001945 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001946 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001947 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001948 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001949 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001950 srcView = this->readSurfaceView();
1951 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001952 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1953 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1954 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001955 // If we can't get a texture copy of the contents then give up.
1956 callback(context, nullptr);
1957 return;
1958 }
Brian Salomonc5243782020-04-02 12:50:34 -04001959 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001960 x = y = 0;
1961 }
Brian Salomon4d036892019-07-02 15:10:58 -04001962 // We assume the caller wants kPremul. There is no way to indicate a preference.
1963 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001964 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1965 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001966 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001967 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1968 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001969 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1970 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001971 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001972 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001973 return;
1974 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001975 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001976 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1977 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1978 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1979 SkMatrix::I(), std::move(xform));
1980 srcView = tempRTC->readSurfaceView();
1981 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001982 x = y = 0;
1983 }
1984 }
Greg Danielc594e622019-10-15 14:01:49 -04001985
Greg Daniele20fcad2020-01-08 11:52:34 -05001986 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1987 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1988 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001989 int halfW = dstSize.width()/2;
1990 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001991 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1992 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1993 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1994 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1995 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1996 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001997 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001998 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001999 return;
2000 }
2001
Brian Osman28ba5282019-10-30 14:18:07 -04002002 float baseM[20];
2003 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2004
Brian Salomon024bd002019-06-11 11:38:16 -04002005 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2006
Mike Reed1f607332020-05-21 12:11:27 -04002007 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002008
Brian Salomon9241a6d2019-10-03 13:26:54 -04002009 SkRect dstRectY = SkRect::Make(dstSize);
2010 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002011
Brian Salomon6aa65052020-01-28 12:16:53 -05002012 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2013 PixelTransferResult yTransfer, uTransfer, vTransfer;
2014
Brian Salomon024bd002019-06-11 11:38:16 -04002015 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2016 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002017 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002018 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002019 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002020 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002021 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002022 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2023 yPaint.addColorFragmentProcessor(std::move(yFP));
2024 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002025 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002026 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002027 if (!doSynchronousRead) {
2028 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2029 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2030 if (!yTransfer.fTransferBuffer) {
2031 callback(context, nullptr);
2032 return;
2033 }
Brian Salomon024bd002019-06-11 11:38:16 -04002034 }
2035
2036 texMatrix.preScale(2.f, 2.f);
2037 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2038 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002039 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002040 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002041 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002042 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002043 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002044 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2045 uPaint.addColorFragmentProcessor(std::move(uFP));
2046 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002047 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002048 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002049 if (!doSynchronousRead) {
2050 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2051 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2052 if (!uTransfer.fTransferBuffer) {
2053 callback(context, nullptr);
2054 return;
2055 }
Brian Salomon024bd002019-06-11 11:38:16 -04002056 }
2057
2058 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2059 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002060 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002061 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002062 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002063 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2064 this->colorInfo().alphaType(), texMatrix,
2065 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002066 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2067 vPaint.addColorFragmentProcessor(std::move(vFP));
2068 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002069 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002070 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002071 if (!doSynchronousRead) {
2072 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2073 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2074 if (!vTransfer.fTransferBuffer) {
2075 callback(context, nullptr);
2076 return;
2077 }
2078 }
2079
2080 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002081 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2082 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2083 size_t yRB = yInfo.minRowBytes();
2084 size_t uvRB = uvInfo.minRowBytes();
2085 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2086 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2087 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2088 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2089 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2090 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002091 callback(context, nullptr);
2092 return;
2093 }
2094 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002095 result->addCpuPlane(std::move(y), yRB );
2096 result->addCpuPlane(std::move(u), uvRB);
2097 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002098 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002099 return;
2100 }
2101
2102 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002103 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002104 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002105 GrClientMappedBufferManager* fMappedBufferManager;
2106 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002107 PixelTransferResult fYTransfer;
2108 PixelTransferResult fUTransfer;
2109 PixelTransferResult fVTransfer;
2110 };
2111 // Assumption is that the caller would like to flush. We could take a parameter or require an
2112 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2113 // callback to GrGpu until after the next flush that flushes our op list, though.
2114 auto* finishContext = new FinishContext{callback,
2115 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002116 direct->priv().clientMappedBufferManager(),
2117 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002118 std::move(yTransfer),
2119 std::move(uTransfer),
2120 std::move(vTransfer)};
2121 auto finishCallback = [](GrGpuFinishedContext c) {
2122 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002123 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002124 auto manager = context->fMappedBufferManager;
2125 size_t rowBytes = SkToSizeT(context->fSize.width());
2126 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2127 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002128 delete context;
2129 return;
2130 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002131 rowBytes /= 2;
2132 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2133 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2134 (*context->fClientCallback)(context->fClientContext, nullptr);
2135 delete context;
2136 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002137 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002138 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2139 (*context->fClientCallback)(context->fClientContext, nullptr);
2140 delete context;
2141 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002142 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002143 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002144 delete context;
2145 };
2146 GrFlushInfo flushInfo;
2147 flushInfo.fFinishedContext = finishContext;
2148 flushInfo.fFinishedProc = finishCallback;
2149 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002150}
2151
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002152GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2153 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002154 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002155 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002156 if (info.fSubmittedProc) {
2157 info.fSubmittedProc(info.fSubmittedContext, false);
2158 }
2159 if (info.fFinishedProc) {
2160 info.fFinishedProc(info.fFinishedContext);
2161 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002162 return GrSemaphoresSubmitted::kNo;
2163 }
robertphillips8c523e02016-07-26 07:41:00 -07002164 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002165 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002166
Greg Daniel55f040b2020-02-13 15:38:32 +00002167 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002168}
2169
Greg Danielc64ee462017-06-15 16:59:49 -04002170bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002171 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002172 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002173 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002174 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002175 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002176
2177 AutoCheckFlush acf(this->drawingManager());
2178
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002179 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002180 return false;
2181 }
2182
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002183 auto direct = fContext->priv().asDirectContext();
2184 if (!direct) {
2185 return false;
2186 }
2187
2188 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002189
Greg Daniel301015c2019-11-18 14:06:46 -05002190 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2191 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002192 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002193 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002194 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2195 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002196 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002197 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2198 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002199 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002200}
joshualitt33a5fce2015-11-18 13:28:51 -08002201
Michael Ludwig7c12e282020-05-29 09:54:07 -04002202void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002203 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002204 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002205 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002206 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002207 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002208 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002209 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002210 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002211 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2212
Michael Ludwig2686d692020-04-17 20:21:37 +00002213 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002214
Robert Phillips27927a52018-08-20 13:18:12 -04002215 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002216}
2217
Michael Ludwig7c12e282020-05-29 09:54:07 -04002218void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002219 GrPaint&& paint,
2220 GrAA aa,
2221 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002222 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002223 ASSERT_SINGLE_OWNER
2224 RETURN_IF_ABANDONED
2225 SkDEBUGCODE(this->validate();)
2226 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2227
Brian Salomon2fad74a2017-12-20 13:28:55 -05002228 if (shape.isEmpty()) {
2229 if (shape.inverseFilled()) {
2230 this->drawPaint(clip, std::move(paint), viewMatrix);
2231 }
2232 return;
robertphillipsea461502015-05-26 11:38:03 -07002233 }
2234
Robert Phillips72152832017-01-25 17:31:35 -05002235 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002236
Brian Salomon2fad74a2017-12-20 13:28:55 -05002237 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002238 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002239 SkRRect rrect;
2240 // We can ignore the starting point and direction since there is no path effect.
2241 bool inverted;
2242 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2243 if (rrect.isRect()) {
2244 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2245 &shape.style());
2246 return;
2247 } else if (rrect.isOval()) {
2248 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002249 return;
2250 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002251 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2252 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002253 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2254 viewMatrix.rectStaysRect()) {
2255 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2256 // the matrix to all the points individually rather than just to the rect
2257 SkRect rects[2];
2258 if (shape.asNestedRects(rects)) {
2259 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002260 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002261 fContext, std::move(paint), viewMatrix, rects);
2262 if (op) {
2263 this->addDrawOp(clip, std::move(op));
2264 }
2265 // Returning here indicates that there is nothing to draw in this case.
2266 return;
2267 }
robertphillipsea461502015-05-26 11:38:03 -07002268 }
2269 }
robertphillips4bc31812016-03-01 12:22:49 -08002270
Michael Ludwig6a6de652020-04-30 20:16:36 -04002271 // If we get here in drawShape(), we definitely need to use path rendering
2272 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2273 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002274}
2275
Michael Ludwig7c12e282020-05-29 09:54:07 -04002276bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002277 const GrUserStencilSettings* ss,
2278 SkRegion::Op op,
2279 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002280 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002281 const SkMatrix& viewMatrix,
2282 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002283 ASSERT_SINGLE_OWNER_PRIV
2284 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002285 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002286 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2287 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002288
2289 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002290 GrPaint paint;
2291 paint.setCoverageSetOpXPFactory(op, invert);
2292 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2293 SkRect::MakeIWH(fRenderTargetContext->width(),
2294 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002295 return true;
2296 }
2297
Robert Phillips72152832017-01-25 17:31:35 -05002298 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002299
2300 // An Assumption here is that path renderer would use some form of tweaking
2301 // the src color (either the input alpha or in the frag shader) to implement
2302 // aa. If we have some future driver-mojo path AA that can do the right
2303 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002304 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002305 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002306
Michael Ludwig7c12e282020-05-29 09:54:07 -04002307 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002308
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002309 GrPaint paint;
2310 paint.setCoverageSetOpXPFactory(op, invert);
2311
Michael Ludwig2686d692020-04-17 20:21:37 +00002312 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002313 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002314 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002315 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002316 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002317 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002318 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002319 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002320 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002321 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2322 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002323 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002324
2325 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002326 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002327 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002328 if (!pr) {
2329 return false;
2330 }
2331
Brian Salomonf3569f02017-10-24 12:52:33 -04002332 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2333 std::move(paint),
2334 ss,
2335 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002336 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002337 &clipConservativeBounds,
2338 &viewMatrix,
2339 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002340 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002341 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002342 pr->drawPath(args);
2343 return true;
2344}
2345
Brian Osman11052242016-10-27 14:47:55 -04002346SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002347 ASSERT_SINGLE_OWNER_PRIV
2348
Robert Phillips6a6de562019-02-15 15:19:15 -05002349 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002350 return SkBudgeted::kNo;
2351 }
2352
Brian Osman11052242016-10-27 14:47:55 -04002353 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002354
Greg Daniel3912a4b2020-01-14 09:56:04 -05002355 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002356}
2357
Michael Ludwig7c12e282020-05-29 09:54:07 -04002358void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002359 GrPaint&& paint,
2360 GrAA aa,
2361 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002362 const GrStyledShape& originalShape,
2363 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002364 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002365 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002366 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2367
Jim Van Verthf86073a2018-10-02 13:05:38 -04002368 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2369 return;
2370 }
2371
Michael Ludwig6a6de652020-04-30 20:16:36 -04002372 if (attemptShapeFallback && originalShape.simplified()) {
2373 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2374 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2375 // we ought to try again instead of going right to path rendering.
2376 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2377 return;
2378 }
2379
Michael Ludwig7c12e282020-05-29 09:54:07 -04002380 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002381
Michael Ludwig2686d692020-04-17 20:21:37 +00002382 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002383 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002384
robertphillips68737822015-10-29 12:12:21 -07002385 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002386 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002387 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002388 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002389 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002390 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002391 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002392 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002393 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002394
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002395 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002396 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002397 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002398 return;
2399 }
2400
Chris Dalton6ce447a2019-06-23 18:07:38 -06002401 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002402
2403 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2404 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2405 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2406
Brian Salomon2fad74a2017-12-20 13:28:55 -05002407 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002408 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002409 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2410 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002411 return;
2412 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002413 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002414 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002415 }
2416 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002417 if (canDrawArgs.fShape->style().applies()) {
2418 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2419 styleScale);
2420 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002421 return;
2422 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002423 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002424 // This time, allow SW renderer
2425 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2426 } else {
2427 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002428#if GR_PATH_RENDERER_SPEW
2429 SkDebugf("falling back to: %s\n", pr->name());
2430#endif
bsalomon6663acf2016-05-10 09:14:17 -07002431 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002432 }
robertphillipsea461502015-05-26 11:38:03 -07002433
bsalomon8acedde2016-06-24 10:42:16 -07002434 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002435#ifdef SK_DEBUG
2436 SkDebugf("Unable to find path renderer compatible with path.\n");
2437#endif
2438 return;
2439 }
2440
Robert Phillips256c37b2017-03-01 14:32:46 -05002441 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002442 std::move(paint),
2443 &GrUserStencilSettings::kUnused,
2444 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002445 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002446 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002447 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002448 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002449 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002450 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002451 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002452}
2453
Brian Salomon467921e2017-03-06 16:17:12 -05002454static void op_bounds(SkRect* bounds, const GrOp* op) {
2455 *bounds = op->bounds();
2456 if (op->hasZeroArea()) {
2457 if (op->hasAABloat()) {
2458 bounds->outset(0.5f, 0.5f);
2459 } else {
2460 // We don't know which way the particular GPU will snap lines or points at integer
2461 // coords. So we ensure that the bounds is large enough for either snap.
2462 SkRect before = *bounds;
2463 bounds->roundOut(bounds);
2464 if (bounds->fLeft == before.fLeft) {
2465 bounds->fLeft -= 1;
2466 }
2467 if (bounds->fTop == before.fTop) {
2468 bounds->fTop -= 1;
2469 }
2470 if (bounds->fRight == before.fRight) {
2471 bounds->fRight += 1;
2472 }
2473 if (bounds->fBottom == before.fBottom) {
2474 bounds->fBottom += 1;
2475 }
2476 }
2477 }
2478}
2479
Chris Dalton08755122019-08-05 16:13:47 -06002480void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002481 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002482 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2483}
2484
Michael Ludwig7c12e282020-05-29 09:54:07 -04002485void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002486 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002487 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002488 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002489 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002490 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002491 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002492 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002493 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002494 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002495
Brian Salomon467921e2017-03-06 16:17:12 -05002496 // Setup clip
2497 SkRect bounds;
2498 op_bounds(&bounds, op.get());
Michael Ludwig4926b072020-06-04 19:39:42 +00002499 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002500 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002501 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002502 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002503
Chris Dalton9acf6822019-11-12 11:52:40 -07002504 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002505 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002506 }
2507
Michael Ludwig7c12e282020-05-29 09:54:07 -04002508 bool skipDraw = false;
2509 if (clip) {
2510 // Have a complex clip, so defer to its early clip culling
2511 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2512 skipDraw = true;
2513 }
2514 } else {
2515 // No clipping, so just clip the bounds against the logical render target dimensions
2516 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2517 skipDraw = true;
2518 }
2519 }
2520
2521 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002522 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002523 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002524 }
2525
Chris Dalton9acf6822019-11-12 11:52:40 -07002526 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2527 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2528
2529 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2530 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2531 // that uses a stencil test when the stencil buffer is multisampled.)
2532 bool hasMixedSampledCoverage = (
2533 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002534 SkASSERT(!hasMixedSampledCoverage ||
2535 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002536
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002537 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002538 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002539 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2540
Greg Daniel524e28b2019-11-01 11:48:53 -04002541 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002542 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002543 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002544 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002545 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002546 }
2547 }
2548
2549 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002550 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002551 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002552 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002553 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002554 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002555 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002556}
2557
Michael Ludwig7c12e282020-05-29 09:54:07 -04002558bool GrRenderTargetContext::setupDstProxyView(const GrClip* clip, const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002559 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002560 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2561 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2562 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002563 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002564 return false;
2565 }
2566
Greg Daniel3912a4b2020-01-14 09:56:04 -05002567 if (this->caps()->textureBarrierSupport() &&
2568 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002569 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002570 // The render target is a texture, so we can read from it directly in the shader. The XP
2571 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002572 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002573 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002574 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002575 }
2576 }
2577
Greg Daniel3912a4b2020-01-14 09:56:04 -05002578 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002579
Michael Ludwig7c12e282020-05-29 09:54:07 -04002580 SkIRect clippedRect = get_clip_bounds(this, clip);
Brian Salomon09181ef2018-11-14 13:39:51 -05002581 SkRect opBounds = op.bounds();
2582 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2583 // 0.5 pixels.
2584 if (op.hasAABloat() || op.hasZeroArea()) {
2585 opBounds.outset(0.5f, 0.5f);
2586 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2587 // performance we may ignore the clip when the draw is entirely inside the clip is float
2588 // space but will hit pixels just outside the clip when actually rasterizing.
2589 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002590 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002591 }
2592 SkIRect opIBounds;
2593 opBounds.roundOut(&opIBounds);
2594 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002595#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002596 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002597#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002598 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002599 }
2600
Greg Daniel3155f7f2020-01-16 16:54:26 -05002601 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002602 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2603 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002604 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002605 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002606
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002607 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002608 copyRect = clippedRect;
2609 }
Brian Salomon467921e2017-03-06 16:17:12 -05002610
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002611 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002612 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002613 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002614 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002615 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002616 } else {
Eric Karl74480882017-04-03 14:49:05 -07002617 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002618 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002619 }
Brian Salomonc5243782020-04-02 12:50:34 -04002620 auto copy =
2621 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2622 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2623 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002624
Brian Salomonc5243782020-04-02 12:50:34 -04002625 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002626 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002627 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002628}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002629
Greg Daniel573312e2020-02-07 17:22:35 -05002630bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002631 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002632 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002633 SkIRect clippedSrcRect;
2634 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002635 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2636 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002637 return false;
2638 }
2639
2640 GrPaint paint;
2641 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002642
Greg Danield2ccbb52020-02-05 10:45:39 -05002643 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002644 if (!fp) {
2645 return false;
2646 }
2647 paint.addColorFragmentProcessor(std::move(fp));
2648
2649 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002650 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002651 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2652 clippedSrcRect.height()),
2653 SkRect::Make(clippedSrcRect));
2654 return true;
2655}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002656
2657void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2658 SkASSERT(&task == fOpsTask.get());
2659 fOpsTask.reset();
2660}