blob: d3eb4ccd8e98476261c99fbe789740008343a2e7 [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"
Herb Derby64688222020-06-08 14:19:57 -040023#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/core/SkRRectPriv.h"
25#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040026#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040028#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrBlurUtils.h"
30#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040031#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040032#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040034#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrDrawingManager.h"
36#include "src/gpu/GrFixedClip.h"
37#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040038#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040039#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrMemoryPool.h"
41#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040042#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040044#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/GrRenderTargetContextPriv.h"
46#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/GrStencilAttachment.h"
48#include "src/gpu/GrStyle.h"
49#include "src/gpu/GrTracing.h"
50#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040051#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040053#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040054#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000055#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000056#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "src/gpu/ops/GrAtlasTextOp.h"
58#include "src/gpu/ops/GrClearOp.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"
Herb Derby64688222020-06-08 14:19:57 -040073#include "src/gpu/text/GrTextBlobCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050074#include "src/gpu/text/GrTextContext.h"
75#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040076
Herb Derbyc1b482c2018-08-09 15:02:27 -040077class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040078public:
79 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040080 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040081 renderTargetContext->colorInfo())
Herb Derbyd29207a2020-06-08 13:50:19 -040082 , fRenderTargetContext(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 {
Herb Derbyd29207a2020-06-08 13:50:19 -0400115 return fRenderTargetContext->glyphPainter();
Herb Derby74c6ed32018-07-28 18:07:54 -0400116 }
117
Brian Salomonf18b1d82017-10-27 11:30:49 -0400118private:
119 GrRenderTargetContext* fRenderTargetContext;
120};
joshualittbc907352016-01-13 06:45:40 -0800121
Robert Phillips72152832017-01-25 17:31:35 -0500122#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
Adlai Holler33dbd652020-06-01 12:35:42 -0400123#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
124#define ASSERT_SINGLE_OWNER_PRIV GR_ASSERT_SINGLE_OWNER(fRenderTargetContext->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -0500125#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
126#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
127#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
128#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
129#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700130
Brian Salomone225b562017-06-14 13:00:03 -0400131//////////////////////////////////////////////////////////////////////////////
132
robertphillipsea461502015-05-26 11:38:03 -0700133class AutoCheckFlush {
134public:
halcanary9d524f22016-03-29 09:03:52 -0700135 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700136 SkASSERT(fDrawingManager);
137 }
bsalomonb77a9072016-09-07 10:02:04 -0700138 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700139
140private:
robertphillips77a2e522015-10-17 07:43:27 -0700141 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700142};
143
Greg Daniele20fcad2020-01-08 11:52:34 -0500144std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
145 GrRecordingContext* context,
146 GrColorType colorType,
147 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500148 sk_sp<GrSurfaceProxy> proxy,
149 GrSurfaceOrigin origin,
150 const SkSurfaceProps* surfaceProps,
151 bool managedOps) {
152 if (!proxy) {
153 return nullptr;
154 }
155
156 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400157 GrSwizzle readSwizzle, writeSwizzle;
158 if (colorType != GrColorType::kUnknown) {
159 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
160 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
161 }
Greg Danielba0ff782020-01-07 15:42:57 -0500162
Greg Daniel3912a4b2020-01-14 09:56:04 -0500163 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400164 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500165
166 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400167 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500168 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500169}
170
171std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
172 GrRecordingContext* context,
173 GrColorType colorType,
174 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500175 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500176 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500177 const GrBackendFormat& format,
178 int sampleCnt,
179 GrMipMapped mipMapped,
180 GrProtected isProtected,
181 GrSurfaceOrigin origin,
182 SkBudgeted budgeted,
183 const SkSurfaceProps* surfaceProps) {
184 // It is probably not necessary to check if the context is abandoned here since uses of the
185 // GrRenderTargetContext which need the context will mostly likely fail later on without an
186 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
187 // an abandoned context correctly. It also lets us early out of some extra work.
188 if (context->priv().abandoned()) {
189 return nullptr;
190 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500191
Greg Daniele20fcad2020-01-08 11:52:34 -0500192 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400193 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500194 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500195 if (!proxy) {
196 return nullptr;
197 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500198
Greg Danielba0ff782020-01-07 15:42:57 -0500199 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
200 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500201 if (!rtc) {
202 return nullptr;
203 }
204 rtc->discard();
205 return rtc;
206}
207
208std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
209 GrRecordingContext* context,
210 GrColorType colorType,
211 sk_sp<SkColorSpace> colorSpace,
212 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500213 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500214 int sampleCnt,
215 GrMipMapped mipMapped,
216 GrProtected isProtected,
217 GrSurfaceOrigin origin,
218 SkBudgeted budgeted,
219 const SkSurfaceProps* surfaceProps) {
220 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
221 if (!format.isValid()) {
222 return nullptr;
223 }
224
225 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
226 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
227 surfaceProps);
228}
229
230static inline GrColorType color_type_fallback(GrColorType ct) {
231 switch (ct) {
232 // kRGBA_8888 is our default fallback for many color types that may not have renderable
233 // backend formats.
234 case GrColorType::kAlpha_8:
235 case GrColorType::kBGR_565:
236 case GrColorType::kABGR_4444:
237 case GrColorType::kBGRA_8888:
238 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400239 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500240 case GrColorType::kRGBA_F16:
241 case GrColorType::kRGBA_F16_Clamped:
242 return GrColorType::kRGBA_8888;
243 case GrColorType::kAlpha_F16:
244 return GrColorType::kRGBA_F16;
245 case GrColorType::kGray_8:
246 return GrColorType::kRGB_888x;
247 default:
248 return GrColorType::kUnknown;
249 }
250}
251
Brian Salomond005b692020-04-01 15:47:05 -0400252std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400253 GrImageContext* context, GrColorType colorType, int sampleCnt) {
254 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400255 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400256 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
257 // We continue to the fallback color type if there no default renderable format or we
258 // requested msaa and the format doesn't support msaa.
259 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400260 return {colorType, format};
261 }
262 colorType = color_type_fallback(colorType);
263 } while (colorType != GrColorType::kUnknown);
264 return {GrColorType::kUnknown, {}};
265}
266
Greg Daniele20fcad2020-01-08 11:52:34 -0500267std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
268 GrRecordingContext* context,
269 GrColorType colorType,
270 sk_sp<SkColorSpace> colorSpace,
271 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500272 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500273 int sampleCnt,
274 GrMipMapped mipMapped,
275 GrProtected isProtected,
276 GrSurfaceOrigin origin,
277 SkBudgeted budgeted,
278 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400279 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400280 if (ct == GrColorType::kUnknown) {
281 return nullptr;
282 }
283 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
284 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500285}
286
Greg Danielba0ff782020-01-07 15:42:57 -0500287std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
288 GrRecordingContext* context,
289 GrColorType colorType,
290 sk_sp<SkColorSpace> colorSpace,
291 const GrBackendTexture& tex,
292 int sampleCnt,
293 GrSurfaceOrigin origin,
294 const SkSurfaceProps* surfaceProps,
295 ReleaseProc releaseProc,
296 ReleaseContext releaseCtx) {
297 SkASSERT(sampleCnt > 0);
298 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400299 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500300 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500301 if (!proxy) {
302 return nullptr;
303 }
304
305 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
306 origin, surfaceProps);
307}
308
309std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
310 GrRecordingContext* context,
311 GrColorType colorType,
312 sk_sp<SkColorSpace> colorSpace,
313 const GrBackendTexture& tex,
314 int sampleCnt,
315 GrSurfaceOrigin origin,
316 const SkSurfaceProps* surfaceProps) {
317 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400318 sk_sp<GrSurfaceProxy> proxy(
319 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500320 if (!proxy) {
321 return nullptr;
322 }
323
324 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
325 origin, surfaceProps);
326}
327
328std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
329 GrRecordingContext* context,
330 GrColorType colorType,
331 sk_sp<SkColorSpace> colorSpace,
332 const GrBackendRenderTarget& rt,
333 GrSurfaceOrigin origin,
334 const SkSurfaceProps* surfaceProps,
335 ReleaseProc releaseProc,
336 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400337 sk_sp<GrSurfaceProxy> proxy(
338 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500339 if (!proxy) {
340 return nullptr;
341 }
342
343 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
344 origin, surfaceProps);
345}
346
347std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
348 GrRecordingContext* context,
349 const SkImageInfo& imageInfo,
350 const GrVkDrawableInfo& vkInfo,
351 const SkSurfaceProps* props) {
352 sk_sp<GrSurfaceProxy> proxy(
353 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
354 vkInfo));
355 if (!proxy) {
356 return nullptr;
357 }
358
Greg Danielba0ff782020-01-07 15:42:57 -0500359 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
360 imageInfo.refColorSpace(), std::move(proxy),
361 kTopLeft_GrSurfaceOrigin, props);
362}
363
Greg Danielf41b2bd2019-08-22 16:19:24 -0400364// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
365// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
366// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
367// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500368GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400370 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400371 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400372 sk_sp<SkColorSpace> colorSpace,
373 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400374 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500375 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
376 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400377 , fWriteView(std::move(writeView))
Brian Salomonf3569f02017-10-24 12:52:33 -0400378 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400379 , fManagedOpsTask(managedOpsTask)
380 , fGlyphPainter(*this) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400381 fOpsTask = sk_ref_sp(context->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
Brian Salomon3b8486a2020-04-21 12:43:26 -0400382 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400383 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400384 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400385 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
386 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500387
Brian Salomonf18b1d82017-10-27 11:30:49 -0400388 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700389 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700390}
391
robertphillips2e1e51f2015-10-15 08:01:48 -0700392#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500393void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400394 if (fOpsTask && !fOpsTask->isClosed()) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400395 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700396 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700397}
398#endif
399
Brian Osman11052242016-10-27 14:47:55 -0400400GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800401 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400402 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400403 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400404 }
robertphillipsa106c622015-10-16 09:07:06 -0700405}
406
Chris Dalton7d6748e2019-03-13 00:34:52 -0600407inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408 if (GrAA::kNo == aa) {
409 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
410 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600411 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600412 return GrAAType::kMSAA;
413 }
414 return GrAAType::kNone;
415 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600416 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600417}
418
Greg Daniele252f082017-10-23 16:05:23 -0400419GrMipMapped GrRenderTargetContext::mipMapped() const {
420 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
421 return proxy->mipMapped();
422 }
423 return GrMipMapped::kNo;
424}
425
Greg Danielf41b2bd2019-08-22 16:19:24 -0400426GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800427 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700428 SkDEBUGCODE(this->validate();)
429
Brian Salomon3b8486a2020-04-21 12:43:26 -0400430 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600431 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400432 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600433 if (fOpsTask && fNumStencilSamples > 0) {
434 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600435 fOpsTask->setMustPreserveStencil();
436 // Reload the stencil buffer content at the beginning of newOpsTask.
437 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
438 // values?
439 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
440 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400441 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600442 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700443 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400444 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400445 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700446}
447
Herb Derby64688222020-06-08 14:19:57 -0400448static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
449 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
450 if (lcd) {
451 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
452 // can be modified. For now we just regenerate if any run in a textblob has LCD.
453 // TODO figure out where all of these modifications are and see if we can incorporate that
454 // logic at a higher level *OR* use sRGB
455 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
456
457 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
458 // see the note above. We pick a dummy value for LCD text to ensure we always match the
459 // same key
460 return SK_ColorTRANSPARENT;
461 } else {
462 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
463 // gamma corrected masks anyways, nor color
464 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
465 SkColorGetG(canonicalColor),
466 SkColorGetB(canonicalColor));
467 // reduce to our finite number of bits
468 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
469 }
470 return canonicalColor;
471}
472
Michael Ludwig7c12e282020-05-29 09:54:07 -0400473void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400474 const SkMatrixProvider& matrixProvider,
Herb Derby64688222020-06-08 14:19:57 -0400475 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800476 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700477 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700478 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400479 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700480
Greg Danielbe7fc462019-01-03 16:40:42 -0500481 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
482 // secondary command buffers because it would require stopping and starting a render pass which
483 // we don't have access to.
484 if (this->wrapsVkSecondaryCB()) {
485 return;
486 }
487
Herb Derby64688222020-06-08 14:19:57 -0400488 auto options = this->drawingManager()->getTextContext()->options();
489 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
490
491 // Get the first paint to use as the key paint.
492 const SkPaint& blobPaint = glyphRunList.paint();
493
494 SkPoint drawOrigin = glyphRunList.origin();
495
496 SkMaskFilterBase::BlurRec blurRec;
497 // It might be worth caching these things, but its not clear at this time
498 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
499 const SkMaskFilter* mf = blobPaint.getMaskFilter();
500 bool canCache = glyphRunList.canCache() &&
501 !(blobPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
502
503 // If we're doing linear blending, then we can disable the gamma hacks.
504 // Otherwise, leave them on. In either case, we still want the contrast boost:
505 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
506 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
507 ? SkScalerContextFlags::kBoostContrast
508 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
509
510 sk_sp<GrTextBlob> blob;
511 GrTextBlob::Key key;
512 if (canCache) {
513 bool hasLCD = glyphRunList.anyRunsLCD();
514
515 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
516 SkPixelGeometry pixelGeometry =
517 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
518
519 GrColor canonicalColor = compute_canonical_color(blobPaint, hasLCD);
520
521 key.fPixelGeometry = pixelGeometry;
522 key.fUniqueID = glyphRunList.uniqueID();
523 key.fStyle = blobPaint.getStyle();
524 key.fHasBlur = SkToBool(mf);
525 key.fCanonicalColor = canonicalColor;
526 key.fScalerContextFlags = scalerContextFlags;
527 blob = textBlobCache->find(key);
528 }
529
530 const SkMatrix& drawMatrix(matrixProvider.localToDevice());
531 if (blob != nullptr && blob->canReuse(blobPaint, blurRec, drawMatrix, drawOrigin)) {
532 // Reusing the blob. Move it to the front of LRU cache.
533 textBlobCache->makeMRU(blob.get());
534 } else {
535 // Build or Rebuild the GrTextBlob
536 if (blob != nullptr) {
537 // We have to remake the blob because changes may invalidate our masks.
538 // TODO we could probably get away with reuse most of the time if the pointer is unique,
539 // but we'd have to clear the SubRun information
540 textBlobCache->remove(blob.get());
541 }
542 if (canCache) {
543 blob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, drawMatrix);
544 } else {
545 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
546 }
547 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
548 fGlyphPainter.processGlyphRunList(
549 glyphRunList, drawMatrix, fSurfaceProps, supportsSDFT, options, blob.get());
550 }
551
552 blob->insertOpsIntoTarget(
553 fTextTarget.get(), fSurfaceProps, blobPaint, clip, matrixProvider, drawOrigin);
robertphillipsea461502015-05-26 11:38:03 -0700554}
555
Brian Osman11052242016-10-27 14:47:55 -0400556void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800557 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700558 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700559 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400560 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700561
Robert Phillips72152832017-01-25 17:31:35 -0500562 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400563
Greg Danielf41b2bd2019-08-22 16:19:24 -0400564 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700565}
566
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500567static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
568 paint->setColor4f(color);
569 if (color.isOpaque()) {
570 // Can just rely on the src-over blend mode to do the right thing
571 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
572 } else {
573 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
574 // were src blended
575 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
576 }
577}
578
Michael Ludwig81d41722020-05-26 16:57:38 -0400579// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
580// premul alpha type. If we ever support different alpha type render targets, this function should
581// transform the color as appropriate.
582void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500583 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400584 bool upgradePartialToFull) {
585 ASSERT_SINGLE_OWNER
586 RETURN_IF_ABANDONED
587 SkDEBUGCODE(this->validate();)
588 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
589
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400590 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
591 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
592 // supports then. Drawing an axis-aligned rect is the fallback path.
593 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
594 if (scissor && !scissorState.set(*scissor)) {
595 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
596 // except clear ops are not draw ops).
597 return;
598 }
599
600 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
601 // the test. We only do this when the clear would be handled by a load op or natively.
602 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
603 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
604 this->caps()->shouldInitializeTextures())) {
605 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
606 // only clear the entire target if we knew it had not been cleared before. As
607 // is this could end up doing a lot of redundant clears.
608 scissorState.setDisabled();
609 } else {
610 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
611 // render target to overflow into any approx-fit padding of the backing store dimensions
612 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400613 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500614 }
615
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400616 if (!scissorState.enabled()) {
617 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
618 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400619 GrOpsTask* opsTask = this->getOpsTask();
620 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500621 !this->caps()->performColorClearsAsDraws()) {
622 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400623 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500624 return;
625 } else {
626 // Will use an op for the clear, reset the load op to discard since the op will
627 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500629 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400630 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500631
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400632 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
633 // as a load op.
634 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
635 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
636 if (clearAsDraw) {
637 GrPaint paint;
638 clear_to_grpaint(color, &paint);
639 this->addDrawOp(nullptr,
640 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
641 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500642 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400643 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500644 }
645}
646
Michael Ludwig7c12e282020-05-29 09:54:07 -0400647void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500648 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400649 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000650 // Start with the render target, since that is the maximum content we could possibly fill.
651 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500652 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400653 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000654 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
655 // inverse view matrix.
656 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
657 } else {
658 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
659 SkMatrix localMatrix;
660 if (!viewMatrix.invert(&localMatrix)) {
661 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400662 }
Michael Ludwig61328202019-06-19 14:48:58 +0000663 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
664 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700665 }
robertphillipsea461502015-05-26 11:38:03 -0700666}
667
Michael Ludwig61328202019-06-19 14:48:58 +0000668enum class GrRenderTargetContext::QuadOptimization {
669 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
670 kDiscarded,
671 // The rect to draw was converted to some other op and appended to the oplist, so no additional
672 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
673 // a constColor is provided.
674 kSubmitted,
675 // The clip was folded into the device quad, with updated edge flags and local coords, and
676 // caller is responsible for adding an appropriate op.
677 kClipApplied,
678 // No change to clip, but quad updated to better fit clip/render target, and caller is
679 // responsible for adding an appropriate op.
680 kCropped
681};
Michael Ludwig61a16512019-01-15 11:15:13 -0500682
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400683static bool make_vertex_finite(float* value) {
684 if (SkScalarIsNaN(*value)) {
685 return false;
686 }
687
688 if (!SkScalarIsFinite(*value)) {
689 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
690 // left when calculating crops.
691 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
692 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
693 }
694
695 return true;
696}
697
Michael Ludwig7c12e282020-05-29 09:54:07 -0400698static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
699 return clip ? clip->getConservativeBounds(rtc->width(), rtc->height())
700 : SkIRect::MakeWH(rtc->width(), rtc->height());
701}
702
Michael Ludwig61328202019-06-19 14:48:58 +0000703GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400704 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500705 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000706 // Optimization requirements:
707 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500708 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
709 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
710 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
711 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
712 // 5. kCropped in all other scenarios (although a crop may be a no-op)
713
714 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
715 // better to just keep the old flags instead of introducing mixed edge flags.
716 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000717
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400718 // Use the logical size of the render target, which allows for "fullscreen" clears even if
719 // the render target has an approximate backing fit
720 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400721
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500722 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000723 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400724 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
725 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500726 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400727 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500728 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
729 !make_vertex_finite(quad->fDevice.ys() + i) ||
730 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400731 // Discard if we see a nan
732 return QuadOptimization::kDiscarded;
733 }
734 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500735 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400736 }
737 } else {
738 // CropToRect requires the quads to be finite. If they are not finite and we have local
739 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500740 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400741 return QuadOptimization::kDiscarded;
742 }
Brian Salomon7694b902019-06-18 21:00:21 +0000743 }
744
Michael Ludwig61328202019-06-19 14:48:58 +0000745 // If the quad is entirely off screen, it doesn't matter what the clip does
746 if (!rtRect.intersects(drawBounds)) {
747 return QuadOptimization::kDiscarded;
748 }
Brian Salomon7694b902019-06-18 21:00:21 +0000749
Michael Ludwig61328202019-06-19 14:48:58 +0000750 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
751 // the render target).
752 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400753 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
754 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
755 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
756 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000757 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400758 if (clip && !clip->quickContains(rtRect)) {
759 if (!clip->isRRect(rtRect, &clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000760 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000761 }
Brian Salomon7694b902019-06-18 21:00:21 +0000762 }
763
Michael Ludwig61328202019-06-19 14:48:58 +0000764 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
765 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400766 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000767 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
768 // of the render target and the clip rect)
769 SkRect clipBounds = rtRect;
770 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
771 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000772 }
773
Michael Ludwig61328202019-06-19 14:48:58 +0000774 if (clipRRect.isRect()) {
775 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500776 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700777 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500778 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000779 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500780 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000781 if (drawBounds.contains(rtRect)) {
782 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400783 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000784 return QuadOptimization::kSubmitted;
785 } else if (GrClip::IsPixelAligned(drawBounds) &&
786 drawBounds.width() > 256 && drawBounds.height() > 256) {
787 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
788 SkIRect scissorRect;
789 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400790 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000791 return QuadOptimization::kSubmitted;
792 }
793 }
794
795 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000796 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500797 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000798 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
799 // properly reflect the smooth edge of the clip.
800 *aa = GrAA::kYes;
801 }
802 // We intentionally do not downgrade AA here because we don't know if we need to
803 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
804 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
805
806 // deviceQuad is exactly the intersection of original quad and clip, so it can be
807 // drawn with no clip (submitted by caller)
808 return QuadOptimization::kClipApplied;
809 } else {
810 // The quads have been updated to better fit the clip bounds, but can't get rid of
811 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500812 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000813 return QuadOptimization::kCropped;
814 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700815 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000816 // Rounded corners and constant filled color (limit ourselves to solid colors because
817 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500818 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
819 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
820 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000821 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
822 // we can draw the rrect directly and ignore the edge flags
823 GrPaint paint;
824 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400825 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000826 clipRRect, GrStyle::SimpleFill());
827 return QuadOptimization::kSubmitted;
828 } else {
829 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500830 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000831 return QuadOptimization::kCropped;
832 }
Brian Salomon7694b902019-06-18 21:00:21 +0000833 }
Brian Salomon7694b902019-06-18 21:00:21 +0000834 }
835
Michael Ludwig61328202019-06-19 14:48:58 +0000836 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400837 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000838
839 // One final check for discarding, since we may have gone here directly due to a complex clip
840 if (!clipBounds.intersects(drawBounds)) {
841 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000842 }
843
Michael Ludwig61328202019-06-19 14:48:58 +0000844 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
845 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500846 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
847 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000848
849 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000850}
851
Michael Ludwig7c12e282020-05-29 09:54:07 -0400852void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500853 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500854 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500855 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400856 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000857 ASSERT_SINGLE_OWNER
858 RETURN_IF_ABANDONED
859 SkDEBUGCODE(this->validate();)
860 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500861
Michael Ludwig61328202019-06-19 14:48:58 +0000862 AutoCheckFlush acf(this->drawingManager());
863
864 SkPMColor4f* constColor = nullptr;
865 SkPMColor4f paintColor;
866 if (!ss && !paint.numCoverageFragmentProcessors() &&
867 paint.isConstantBlendedColor(&paintColor)) {
868 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
869 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500870 }
871
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500872 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000873 if (opt >= QuadOptimization::kClipApplied) {
874 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400875 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000876 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
877 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500878 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
879 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700880 }
Michael Ludwig61328202019-06-19 14:48:58 +0000881 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800882}
883
Michael Ludwig7c12e282020-05-29 09:54:07 -0400884void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400885 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500886 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000887 sk_sp<GrColorSpaceXform> textureXform,
888 GrSamplerState::Filter filter,
889 const SkPMColor4f& color,
890 SkBlendMode blendMode,
891 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500892 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400893 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000894 ASSERT_SINGLE_OWNER
895 RETURN_IF_ABANDONED
896 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400897 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000898 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
899
900 AutoCheckFlush acf(this->drawingManager());
901
902 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
903 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500904 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000905
906 SkASSERT(opt != QuadOptimization::kSubmitted);
907 if (opt != QuadOptimization::kDiscarded) {
908 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400909 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000910 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400911 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400912 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
913 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400914 // Use the provided subset, although hypothetically we could detect that the cropped local
915 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500916 this->addDrawOp(finalClip,
917 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
918 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400919 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000920 }
921}
922
Michael Ludwig7c12e282020-05-29 09:54:07 -0400923void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500924 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500925 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400926 const SkMatrix& viewMatrix,
927 const SkRect& rect,
928 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700929 if (!style) {
930 style = &GrStyle::SimpleFill();
931 }
joshualitt1de610a2016-01-06 08:26:09 -0800932 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700933 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700934 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400935 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700936
bsalomon6663acf2016-05-10 09:14:17 -0700937 // Path effects should've been devolved to a path in SkGpuDevice
938 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700939
Robert Phillips72152832017-01-25 17:31:35 -0500940 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700941
bsalomon6663acf2016-05-10 09:14:17 -0700942 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400943 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000944 // Fills the rect, using rect as its own local coordinates
945 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500946 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400947 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
948 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
949 (rect.width() && rect.height())) {
950 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
951 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400952 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700953
Chris Dalton7d6748e2019-03-13 00:34:52 -0600954 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500955 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
956 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
957 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500958 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400959 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700960 return;
robertphillips4bc31812016-03-01 12:22:49 -0800961 }
robertphillips4bc31812016-03-01 12:22:49 -0800962 }
Mike Klein16885072018-12-11 09:54:31 -0500963 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000964 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
965 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700966}
967
Michael Ludwig7c12e282020-05-29 09:54:07 -0400968void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500969 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
970 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600971 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500972
973 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
974 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500975}
976
Robert Phillipsec2249f2016-11-09 08:54:35 -0500977int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500978 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400979 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500980}
981
Greg Danielf41b2bd2019-08-22 16:19:24 -0400982GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600983 ) const {
984#if GR_TEST_UTILS
985 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400986 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600987 }
988#endif
989 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
990 // would normally be overwritten. The one exception is if the render target context is marked as
991 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
992 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
993 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
994 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400995 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600996}
997
Chris Dalton858cf232019-10-14 16:20:00 -0600998void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600999 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -06001000 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -06001001 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -06001002
Chris Daltoneffee202019-07-01 22:28:03 -06001003 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -06001004 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -05001005 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -06001006 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -04001007 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -06001008 }
1009 SkASSERT(numRequiredSamples > 0);
1010
1011 if (numRequiredSamples > fNumStencilSamples) {
1012 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -05001013 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -06001014 }
Chris Dalton6b982802019-06-27 13:53:46 -06001015
Chris Dalton674f77a2019-09-30 20:49:39 -06001016 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -06001017 if (this->caps()->performStencilClearsAsDraws()) {
1018 // There is a driver bug with clearing stencil. We must use an op to manually clear the
1019 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -04001020 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -06001021 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -06001022 this->getOpsTask()->setInitialStencilContent(
1023 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -06001024 }
1025 }
1026}
1027
Michael Ludwig81d41722020-05-26 16:57:38 -04001028void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001029 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1030
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001031 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
1032 if (scissor && !scissorState.set(*scissor)) {
1033 // The requested clear region is off screen, so nothing to do.
1034 return;
1035 }
1036
Michael Ludwig1e632792020-05-21 12:45:31 -04001037 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001038 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -04001039 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001040 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001041
1042 // Configure the paint to have no impact on the color buffer
1043 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001044 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001045 this->addDrawOp(nullptr,
1046 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1047 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001048 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -04001049 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -05001050 }
robertphillips976f5f02016-06-03 10:59:20 -07001051}
1052
Michael Ludwig7c12e282020-05-29 09:54:07 -04001053void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001054 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001055 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001056 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001057 ASSERT_SINGLE_OWNER_PRIV
1058 RETURN_IF_ABANDONED_PRIV
1059 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001060 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1061 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001062
Brian Salomon467921e2017-03-06 16:17:12 -05001063 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1064 SkASSERT(path);
1065 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1066
1067 // FIXME: Use path bounds instead of this WAR once
1068 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1069 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1070
Michael Ludwig7c12e282020-05-29 09:54:07 -04001071 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
1072 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
1073 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001074 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
1075 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
1076
Michael Ludwig7c12e282020-05-29 09:54:07 -04001077 if (clip && !clip->apply(fRenderTargetContext->width(), fRenderTargetContext->height(),
1078 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001079 return;
1080 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001081 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1082 // but as it is, we're just using the full render target so intersecting the two bounds would
1083 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001084
Robert Phillips7c525e62018-06-12 10:11:12 -04001085 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1086 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001087 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001088 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001089 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001090 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001091 if (!op) {
1092 return;
1093 }
Brian Salomon97180af2017-03-14 13:42:58 -04001094 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001095
Chris Daltoneffee202019-07-01 22:28:03 -06001096 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001097 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001098}
1099
Michael Ludwig7c12e282020-05-29 09:54:07 -04001100void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001101 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001102 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001103 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1104 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001105 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001106 ASSERT_SINGLE_OWNER
1107 RETURN_IF_ABANDONED
1108 SkDEBUGCODE(this->validate();)
1109 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001110
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001111 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1112 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1113 AutoCheckFlush acf(this->drawingManager());
1114 GrAAType aaType = this->chooseAAType(aa);
1115 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1116 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1117 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001118 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1119 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001120}
1121
Michael Ludwig7c12e282020-05-29 09:54:07 -04001122void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001123 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001124 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001125 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001126 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001127 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001128 ASSERT_SINGLE_OWNER
1129 RETURN_IF_ABANDONED
1130 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001131 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001132
1133 AutoCheckFlush acf(this->drawingManager());
1134
1135 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001136 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001137 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001138 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001139 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001140 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001141 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001142}
1143
1144///////////////////////////////////////////////////////////////////////////////
1145
Michael Ludwig7c12e282020-05-29 09:54:07 -04001146void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001147 GrPaint&& paint,
1148 const SkMatrix& viewMatrix,
1149 int spriteCount,
1150 const SkRSXform xform[],
1151 const SkRect texRect[],
1152 const SkColor colors[]) {
1153 ASSERT_SINGLE_OWNER
1154 RETURN_IF_ABANDONED
1155 SkDEBUGCODE(this->validate();)
1156 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1157
1158 AutoCheckFlush acf(this->drawingManager());
1159
1160 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1161 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1162 aaType, spriteCount, xform, texRect, colors);
1163 this->addDrawOp(clip, std::move(op));
1164}
1165
1166///////////////////////////////////////////////////////////////////////////////
1167
Michael Ludwig7c12e282020-05-29 09:54:07 -04001168void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001169 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001170 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001171 const SkMatrix& viewMatrix,
1172 const SkRRect& rrect,
1173 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001174 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001175 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001176 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001177 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001178
1179 const SkStrokeRec& stroke = style.strokeRec();
1180 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001181 return;
1182 }
1183
Michael Ludwig7c12e282020-05-29 09:54:07 -04001184 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001185#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1186 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001187 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001188 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1189 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001190 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1191 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001192 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001193 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1194 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1195 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001196 }
1197#endif
bsalomon6663acf2016-05-10 09:14:17 -07001198 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001199
Robert Phillips72152832017-01-25 17:31:35 -05001200 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001201
Chris Dalton7d6748e2019-03-13 00:34:52 -06001202 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001203
Chris Dalton0dffbab2019-03-27 13:08:50 -06001204 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001205 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1206 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1207 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1208 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1209 // to avoid perf regressions on some platforms.
1210 assert_alive(paint);
1211 op = GrOvalOpFactory::MakeCircularRRectOp(
1212 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1213 }
1214 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001215 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001216 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001217 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001218 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001219 assert_alive(paint);
1220 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001221 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001222 }
1223 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001224 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001225 return;
robertphillipsea461502015-05-26 11:38:03 -07001226 }
robertphillipsb56f9272016-02-25 11:03:52 -08001227
Mike Klein16885072018-12-11 09:54:31 -05001228 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001229 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001230 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001231}
1232
Jim Van Verthc5903412016-11-17 15:27:09 -05001233///////////////////////////////////////////////////////////////////////////////
1234
Jim Van Verth3af1af92017-05-18 15:06:54 -04001235static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1236 SkPoint3 result;
1237 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1238 result.fZ = pt.fZ;
1239 return result;
1240}
1241
Michael Ludwig7c12e282020-05-29 09:54:07 -04001242bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001243 const SkMatrix& viewMatrix,
1244 const SkPath& path,
1245 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001246 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001247 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 return true;
1249 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001250 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001251 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001252
1253 // check z plane
1254 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1255 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1256 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1257 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1258 return false;
1259 }
1260
1261 SkRRect rrect;
1262 SkRect rect;
1263 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001264 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001265 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1266 if (!isRRect &&
1267 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1268 rect.width() > SK_ScalarNearlyZero) {
1269 rrect.setOval(rect);
1270 isRRect = true;
1271 }
1272 if (!isRRect && path.isRect(&rect)) {
1273 rrect.setRect(rect);
1274 isRRect = true;
1275 }
1276
1277 if (!isRRect) {
1278 return false;
1279 }
1280
Jim Van Verthc5903412016-11-17 15:27:09 -05001281 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001282 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001283 }
1284
Robert Phillips72152832017-01-25 17:31:35 -05001285 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001286
Jim Van Verth3af1af92017-05-18 15:06:54 -04001287 // transform light
1288 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1289
1290 // 1/scale
1291 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001292 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001293 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1294 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1295
1296 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001297 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1298
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001299 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001300 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1301 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1302 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001303
1304 // Outset the shadow rrect to the border of the penumbra
1305 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1306 SkRRect ambientRRect;
1307 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1308 // If the rrect was an oval then its outset will also be one.
1309 // We set it explicitly to avoid errors.
1310 if (rrect.isOval()) {
1311 ambientRRect = SkRRect::MakeOval(outsetRect);
1312 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001313 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001314 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1315 }
1316
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001317 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001318 if (transparent) {
1319 // set a large inset to force a fill
1320 devSpaceInsetWidth = ambientRRect.width();
1321 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001322
Robert Phillips7c525e62018-06-12 10:11:12 -04001323 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1324 ambientColor,
1325 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001326 ambientRRect,
1327 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001328 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001329 if (op) {
1330 this->addDrawOp(clip, std::move(op));
1331 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001332 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001333
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001334 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001335 SkScalar devSpaceSpotBlur;
1336 SkScalar spotScale;
1337 SkVector spotOffset;
1338 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1339 devLightPos.fZ, rec.fLightRadius,
1340 &devSpaceSpotBlur, &spotScale, &spotOffset);
1341 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1343
Jim Van Verth3af1af92017-05-18 15:06:54 -04001344 // Adjust translate for the effect of the scale.
1345 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1346 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1347 // This offset is in dev space, need to transform it into source space.
1348 SkMatrix ctmInverse;
1349 if (viewMatrix.invert(&ctmInverse)) {
1350 ctmInverse.mapPoints(&spotOffset, 1);
1351 } else {
1352 // Since the matrix is a similarity, this should never happen, but just in case...
1353 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1354 SkASSERT(false);
1355 }
1356
1357 // Compute the transformed shadow rrect
1358 SkRRect spotShadowRRect;
1359 SkMatrix shadowTransform;
1360 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1361 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001362 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001363
1364 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001365 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366 SkScalar insetWidth = blurOutset;
1367 if (transparent) {
1368 // If transparent, just do a fill
1369 insetWidth += spotShadowRRect.width();
1370 } else {
1371 // For shadows, instead of using a stroke we specify an inset from the penumbra
1372 // border. We want to extend this inset area so that it meets up with the caster
1373 // geometry. The inset geometry will by default already be inset by the blur width.
1374 //
1375 // We compare the min and max corners inset by the radius between the original
1376 // rrect and the shadow rrect. The distance between the two plus the difference
1377 // between the scaled radius and the original radius gives the distance from the
1378 // transformed shadow shape to the original shape in that corner. The max
1379 // of these gives the maximum distance we need to cover.
1380 //
1381 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1382 // that to get the full insetWidth.
1383 SkScalar maxOffset;
1384 if (rrect.isRect()) {
1385 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001386 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001387 rrect.rect().fLeft),
1388 SkTAbs(spotShadowRRect.rect().fTop -
1389 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001390 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001391 rrect.rect().fRight),
1392 SkTAbs(spotShadowRRect.rect().fBottom -
1393 rrect.rect().fBottom)));
1394 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001395 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001396 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1397 rrect.rect().fLeft + dr,
1398 spotShadowRRect.rect().fTop -
1399 rrect.rect().fTop + dr);
1400 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1401 rrect.rect().fRight - dr,
1402 spotShadowRRect.rect().fBottom -
1403 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001404 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001405 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001406 }
Brian Osman788b9162020-02-07 10:36:46 -05001407 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001408 }
1409
1410 // Outset the shadow rrect to the border of the penumbra
1411 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1412 if (spotShadowRRect.isOval()) {
1413 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1414 } else {
1415 SkScalar outsetRad = spotRadius + blurOutset;
1416 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1417 }
1418
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001419 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001420
Robert Phillips7c525e62018-06-12 10:11:12 -04001421 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1422 spotColor,
1423 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001424 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001425 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001426 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001427 if (op) {
1428 this->addDrawOp(clip, std::move(op));
1429 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001430 }
1431
1432 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001433}
1434
1435///////////////////////////////////////////////////////////////////////////////
1436
Michael Ludwig7c12e282020-05-29 09:54:07 -04001437bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001438 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001439 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001440 const SkMatrix& viewMatrix,
1441 const SkRRect& origOuter,
1442 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001443 SkASSERT(!origInner.isEmpty());
1444 SkASSERT(!origOuter.isEmpty());
1445
Brian Salomon65749212017-12-01 16:01:47 -05001446 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1447
Chris Dalton7d6748e2019-03-13 00:34:52 -06001448 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001449
1450 if (GrAAType::kMSAA == aaType) {
1451 return false;
1452 }
1453
Greg Daniel2655ede2019-04-10 00:49:28 +00001454 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1455 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001456 auto outerR = outer->width() / 2.f;
1457 auto innerR = inner->width() / 2.f;
1458 auto cx = outer->getBounds().fLeft + outerR;
1459 auto cy = outer->getBounds().fTop + outerR;
1460 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1461 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1462 auto avgR = (innerR + outerR) / 2.f;
1463 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1464 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1465 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001466 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001467 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001468 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001469 if (op) {
1470 this->addDrawOp(clip, std::move(op));
1471 return true;
1472 }
Mike Klein16885072018-12-11 09:54:31 -05001473 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001474 }
1475 }
1476
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001477 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001478 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001479 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1480 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001481 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001482 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1483 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001484 }
robertphillips00095892016-02-29 13:50:40 -08001485
robertphillips00095892016-02-29 13:50:40 -08001486 SkMatrix inverseVM;
1487 if (!viewMatrix.isIdentity()) {
1488 if (!origInner.transform(viewMatrix, inner.writable())) {
1489 return false;
1490 }
1491 if (!origOuter.transform(viewMatrix, outer.writable())) {
1492 return false;
1493 }
1494 if (!viewMatrix.invert(&inverseVM)) {
1495 return false;
1496 }
1497 } else {
1498 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001499 }
robertphillips00095892016-02-29 13:50:40 -08001500
Ethan Nicholaseace9352018-10-15 20:09:54 +00001501 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001502 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001503 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001504 if (!innerEffect) {
1505 return false;
1506 }
1507
Ethan Nicholaseace9352018-10-15 20:09:54 +00001508 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001509 if (!outerEffect) {
1510 return false;
1511 }
1512
Brian Salomon82f44312017-01-11 13:42:54 -05001513 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1514 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001515
1516 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001517 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001518 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1519 }
halcanary9d524f22016-03-29 09:03:52 -07001520
Brian Salomon82f44312017-01-11 13:42:54 -05001521 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1522 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001523 return true;
1524}
1525
Michael Ludwig7c12e282020-05-29 09:54:07 -04001526void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001527 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001528 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001529 const SkMatrix& viewMatrix,
1530 const SkRRect& outer,
1531 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001532 ASSERT_SINGLE_OWNER
1533 RETURN_IF_ABANDONED
1534 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001535 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001536
1537 SkASSERT(!outer.isEmpty());
1538 SkASSERT(!inner.isEmpty());
1539
Robert Phillips72152832017-01-25 17:31:35 -05001540 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001541
Brian Salomon82f44312017-01-11 13:42:54 -05001542 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001543 return;
1544 }
Mike Klein16885072018-12-11 09:54:31 -05001545 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001546
1547 SkPath path;
1548 path.setIsVolatile(true);
1549 path.addRRect(inner);
1550 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001551 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001552 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001553}
1554
robertphillipsea461502015-05-26 11:38:03 -07001555///////////////////////////////////////////////////////////////////////////////
1556
Michael Ludwig7c12e282020-05-29 09:54:07 -04001557void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001558 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001559 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001560 const SkMatrix& viewMatrix,
1561 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001562 const GrStyle& style,
1563 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001564 ASSERT_SINGLE_OWNER
1565 RETURN_IF_ABANDONED
1566 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001567 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001568
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001569 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001570 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001571 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001572 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001573 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1574 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001575 aa = GrAA::kNo;
1576 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001577 }
msarettcc319b92016-08-25 18:07:18 -07001578 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001579 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001580 SkPath path;
1581 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001582 path.setIsVolatile(true);
1583
Brian Salomon82f44312017-01-11 13:42:54 -05001584 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001585 }
1586
Chris Dalton7d6748e2019-03-13 00:34:52 -06001587 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001588 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1589 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001590 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001591}
1592
Michael Ludwig7c12e282020-05-29 09:54:07 -04001593void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001594 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001595 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001596 const SkMatrix& viewMatrix,
1597 const SkRect& oval,
1598 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001599 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001600 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001601 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001602 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001603
Robert Phillips7484d202018-07-03 09:09:08 -04001604 const SkStrokeRec& stroke = style.strokeRec();
1605
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001606 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001607 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1608 return;
1609 }
1610
1611 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001612 return;
robertphillipsea461502015-05-26 11:38:03 -07001613 }
1614
Robert Phillips72152832017-01-25 17:31:35 -05001615 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001616
Chris Dalton7d6748e2019-03-13 00:34:52 -06001617 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001618
1619 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001620 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1621 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001622 // We don't draw true circles as round rects in coverage mode, because it can
1623 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1624 assert_alive(paint);
1625 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1626 this->caps()->shaderCaps());
1627 }
1628 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001629 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1630 // the arc equation. This same special geometry and fragment branch also turn out to be a
1631 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1632 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1633 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001634 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001635 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1636 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001637 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001638 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001639 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001640 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1641 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001642 }
1643 if (op) {
1644 this->addDrawOp(clip, std::move(op));
1645 return;
robertphillipsea461502015-05-26 11:38:03 -07001646 }
robertphillipsb56f9272016-02-25 11:03:52 -08001647
Mike Klein16885072018-12-11 09:54:31 -05001648 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001649 this->drawShapeUsingPathRenderer(
1650 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001651 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001652}
1653
Michael Ludwig7c12e282020-05-29 09:54:07 -04001654void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001655 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001656 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001657 const SkMatrix& viewMatrix,
1658 const SkRect& oval,
1659 SkScalar startAngle,
1660 SkScalar sweepAngle,
1661 bool useCenter,
1662 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001663 ASSERT_SINGLE_OWNER
1664 RETURN_IF_ABANDONED
1665 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001666 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001667
1668 AutoCheckFlush acf(this->drawingManager());
1669
Chris Dalton7d6748e2019-03-13 00:34:52 -06001670 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001671 if (GrAAType::kCoverage == aaType) {
1672 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1673 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1674 std::move(paint),
1675 viewMatrix,
1676 oval,
1677 startAngle,
1678 sweepAngle,
1679 useCenter,
1680 style,
1681 shaderCaps);
1682 if (op) {
1683 this->addDrawOp(clip, std::move(op));
1684 return;
1685 }
1686 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001687 }
Brian Salomone4949402018-04-26 15:22:04 -04001688 this->drawShapeUsingPathRenderer(
1689 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001690 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001691}
1692
Michael Ludwig7c12e282020-05-29 09:54:07 -04001693void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001694 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001695 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001696 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001697 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001698 sk_sp<GrColorSpaceXform> csxf,
1699 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001700 std::unique_ptr<SkLatticeIter> iter,
1701 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001702 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001703 RETURN_IF_ABANDONED
1704 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001705 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001706
Robert Phillips72152832017-01-25 17:31:35 -05001707 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001708
Brian Salomon2a943df2018-05-04 13:43:19 -04001709 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001710 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001711 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001712 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001713}
1714
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001715void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1716 const SkRect& bounds) {
1717 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1718 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001719 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001720}
1721
Brian Salomon031b0ba2019-05-23 11:05:26 -04001722void GrRenderTargetContext::asyncRescaleAndReadPixels(
1723 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1724 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1725 auto direct = fContext->priv().asDirectContext();
1726 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001727 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001728 return;
1729 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001730 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001731 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001732 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001733 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001734 if (this->asRenderTargetProxy()->framebufferOnly()) {
1735 callback(context, nullptr);
1736 return;
1737 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001738 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001739 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001740 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001741 return;
1742 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001743 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001744 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001745 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001747 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001748 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1749 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001750 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001751 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001752 backendFormatOfFinalContext, dstCT);
1753 // Fail if we can't read from the source surface's color type.
1754 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001755 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001756 return;
1757 }
Brian Salomon624f9062019-07-02 14:23:00 -04001758 // Fail if read color type does not have all of dstCT's color channels and those missing color
1759 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001760 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1761 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1762 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1763 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001764 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001765 return;
1766 }
1767
Brian Salomonbf6b9792019-08-21 09:38:10 -04001768 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001769 int x = srcRect.fLeft;
1770 int y = srcRect.fTop;
1771 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001772 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1773 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001774 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001775 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001776 return;
1777 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001778 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001779 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001780 x = y = 0;
1781 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001782 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1783 this->colorInfo().alphaType(),
1784 info.colorSpace(),
1785 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001786 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1787 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001788 // We flip or color convert by drawing and we don't currently support drawing to
1789 // kPremul.
1790 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001791 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001792 return;
1793 }
Greg Daniel40903af2020-01-30 14:55:05 -05001794 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001795 SkRect srcRectToDraw = SkRect::Make(srcRect);
1796 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001797 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001798 texProxyView =
1799 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1800 SkBackingFit::kApprox, SkBudgeted::kNo);
1801 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001802 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001803 return;
1804 }
Brian Salomonc5243782020-04-02 12:50:34 -04001805 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001806 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1807 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001808 tempRTC = GrRenderTargetContext::Make(
1809 direct, this->colorInfo().colorType(), info.refColorSpace(),
1810 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001811 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001812 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001813 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001814 return;
1815 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001816 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001817 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1818 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001819 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1820 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1821 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001822 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001823 }
1824 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001825 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001826 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1827 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001828}
1829
Brian Salomon9241a6d2019-10-03 13:26:54 -04001830class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1831public:
1832 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1833 ~AsyncReadResult() override {
1834 for (int i = 0; i < fPlanes.count(); ++i) {
1835 if (!fPlanes[i].fMappedBuffer) {
1836 delete[] static_cast<const char*>(fPlanes[i].fData);
1837 } else {
1838 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1839 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1840 }
1841 }
1842 }
1843
1844 int count() const override { return fPlanes.count(); }
1845 const void* data(int i) const override { return fPlanes[i].fData; }
1846 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1847
1848 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001849 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001850 size_t rowBytes,
1851 GrClientMappedBufferManager* manager) {
1852 SkASSERT(!result.fTransferBuffer->isMapped());
1853 const void* mappedData = result.fTransferBuffer->map();
1854 if (!mappedData) {
1855 return false;
1856 }
1857 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001858 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001859 result.fPixelConverter(convertedData.get(), mappedData);
1860 this->addCpuPlane(std::move(convertedData), rowBytes);
1861 result.fTransferBuffer->unmap();
1862 } else {
1863 manager->insert(result.fTransferBuffer);
1864 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1865 }
1866 return true;
1867 }
1868
1869 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1870 SkASSERT(data);
1871 SkASSERT(rowBytes > 0);
1872 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1873 }
1874
1875private:
1876 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1877 SkASSERT(data);
1878 SkASSERT(rowBytes > 0);
1879 SkASSERT(mappedBuffer);
1880 SkASSERT(mappedBuffer->isMapped());
1881 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1882 }
1883
1884 struct Plane {
1885 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1886 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1887 const void* fData;
1888 size_t fRowBytes;
1889 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1890 sk_sp<GrGpuBuffer> fMappedBuffer;
1891 };
1892 SkSTArray<3, Plane> fPlanes;
1893 uint32_t fInboxID;
1894};
1895
Brian Salomon024bd002019-06-11 11:38:16 -04001896void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1897 ReadPixelsCallback callback,
1898 ReadPixelsContext context) {
1899 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1900 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1901
Greg Danielca9dbe22020-01-02 13:44:30 -05001902 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001903 callback(context, nullptr);
1904 return;
1905 }
1906
Brian Salomon9241a6d2019-10-03 13:26:54 -04001907 auto directContext = fContext->priv().asDirectContext();
1908 SkASSERT(directContext);
1909 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1910
Brian Salomon024bd002019-06-11 11:38:16 -04001911 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1912
1913 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001914 auto ii = SkImageInfo::Make(rect.size(), colorType,
1915 this->colorInfo().alphaType(),
1916 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001917 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001918 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1919 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1920 result->addCpuPlane(std::move(data), pm.rowBytes());
1921
Brian Salomon1d435302019-07-01 13:05:28 -04001922 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001923 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001924 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001925 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001926 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001927 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001928 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001929
Brian Salomonab32f652019-05-10 14:24:50 -04001930 struct FinishContext {
1931 ReadPixelsCallback* fClientCallback;
1932 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001933 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001934 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001935 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001936 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001937 };
1938 // Assumption is that the caller would like to flush. We could take a parameter or require an
1939 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1940 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001941 auto* finishContext = new FinishContext{callback,
1942 context,
1943 rect.size(),
1944 colorType,
1945 mappedBufferManager,
1946 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001947 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001948 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001949 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001950 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1951 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1952 context->fMappedBufferManager)) {
1953 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001954 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001955 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001956 delete context;
1957 };
1958 GrFlushInfo flushInfo;
1959 flushInfo.fFinishedContext = finishContext;
1960 flushInfo.fFinishedProc = finishCallback;
1961 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1962}
1963
Brian Salomon9241a6d2019-10-03 13:26:54 -04001964void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1965 sk_sp<SkColorSpace> dstColorSpace,
1966 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001967 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001968 RescaleGamma rescaleGamma,
1969 SkFilterQuality rescaleQuality,
1970 ReadPixelsCallback callback,
1971 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001972 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1973 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001974 SkASSERT(!dstSize.isZero());
1975 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1976
Brian Salomon024bd002019-06-11 11:38:16 -04001977 auto direct = fContext->priv().asDirectContext();
1978 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001979 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001980 return;
1981 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001982 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001983 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001984 return;
1985 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001986 if (this->asRenderTargetProxy()->framebufferOnly()) {
1987 callback(context, nullptr);
1988 return;
1989 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001990 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001991 callback(context, nullptr);
1992 return;
1993 }
Brian Salomon024bd002019-06-11 11:38:16 -04001994 int x = srcRect.fLeft;
1995 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001996 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001997 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001998 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001999 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04002000 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04002001 dstColorSpace);
2002 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04002003 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
2004 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002005 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002006 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002007 return;
Brian Salomonab32f652019-05-10 14:24:50 -04002008 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002009 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04002010 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002011 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05002012 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04002013 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05002014 srcView = this->readSurfaceView();
2015 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04002016 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
2017 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
2018 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002019 // If we can't get a texture copy of the contents then give up.
2020 callback(context, nullptr);
2021 return;
2022 }
Brian Salomonc5243782020-04-02 12:50:34 -04002023 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002024 x = y = 0;
2025 }
Brian Salomon4d036892019-07-02 15:10:58 -04002026 // We assume the caller wants kPremul. There is no way to indicate a preference.
2027 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002028 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
2029 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04002030 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002031 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2032 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002033 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2034 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002035 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002036 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002037 return;
2038 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002039 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05002040 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2041 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2042 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2043 SkMatrix::I(), std::move(xform));
2044 srcView = tempRTC->readSurfaceView();
2045 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002046 x = y = 0;
2047 }
2048 }
Greg Danielc594e622019-10-15 14:01:49 -04002049
Greg Daniele20fcad2020-01-08 11:52:34 -05002050 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2051 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2052 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002053 int halfW = dstSize.width()/2;
2054 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002055 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2056 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2057 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2058 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2059 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2060 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002061 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002062 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002063 return;
2064 }
2065
Brian Osman28ba5282019-10-30 14:18:07 -04002066 float baseM[20];
2067 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2068
Brian Salomon024bd002019-06-11 11:38:16 -04002069 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2070
Mike Reed1f607332020-05-21 12:11:27 -04002071 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002072
Brian Salomon9241a6d2019-10-03 13:26:54 -04002073 SkRect dstRectY = SkRect::Make(dstSize);
2074 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002075
Brian Salomon6aa65052020-01-28 12:16:53 -05002076 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2077 PixelTransferResult yTransfer, uTransfer, vTransfer;
2078
Brian Salomon024bd002019-06-11 11:38:16 -04002079 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2080 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002081 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002082 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002083 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002084 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
2085 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
2086 /*unpremulInput=*/false,
2087 /*clampRGBOutput=*/true,
2088 /*premulOutput=*/false);
2089 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002090 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002091 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002092 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002093 if (!doSynchronousRead) {
2094 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2095 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2096 if (!yTransfer.fTransferBuffer) {
2097 callback(context, nullptr);
2098 return;
2099 }
Brian Salomon024bd002019-06-11 11:38:16 -04002100 }
2101
2102 texMatrix.preScale(2.f, 2.f);
2103 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2104 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002105 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002106 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002107 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002108 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2109 GrSamplerState::Filter::kBilerp);
2110 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2111 /*unpremulInput=*/false,
2112 /*clampRGBOutput=*/true,
2113 /*premulOutput=*/false);
2114 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002115 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002116 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002117 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002118 if (!doSynchronousRead) {
2119 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2120 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2121 if (!uTransfer.fTransferBuffer) {
2122 callback(context, nullptr);
2123 return;
2124 }
Brian Salomon024bd002019-06-11 11:38:16 -04002125 }
2126
2127 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2128 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002129 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002130 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002131 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002132 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2133 texMatrix, GrSamplerState::Filter::kBilerp);
2134 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2135 /*unpremulInput=*/false,
2136 /*clampRGBOutput=*/true,
2137 /*premulOutput=*/false);
2138 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002139 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002140 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002141 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002142 if (!doSynchronousRead) {
2143 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2144 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2145 if (!vTransfer.fTransferBuffer) {
2146 callback(context, nullptr);
2147 return;
2148 }
2149 }
2150
2151 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002152 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2153 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2154 size_t yRB = yInfo.minRowBytes();
2155 size_t uvRB = uvInfo.minRowBytes();
2156 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2157 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2158 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2159 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2160 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2161 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002162 callback(context, nullptr);
2163 return;
2164 }
2165 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002166 result->addCpuPlane(std::move(y), yRB );
2167 result->addCpuPlane(std::move(u), uvRB);
2168 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002169 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002170 return;
2171 }
2172
2173 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002174 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002175 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002176 GrClientMappedBufferManager* fMappedBufferManager;
2177 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002178 PixelTransferResult fYTransfer;
2179 PixelTransferResult fUTransfer;
2180 PixelTransferResult fVTransfer;
2181 };
2182 // Assumption is that the caller would like to flush. We could take a parameter or require an
2183 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2184 // callback to GrGpu until after the next flush that flushes our op list, though.
2185 auto* finishContext = new FinishContext{callback,
2186 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002187 direct->priv().clientMappedBufferManager(),
2188 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002189 std::move(yTransfer),
2190 std::move(uTransfer),
2191 std::move(vTransfer)};
2192 auto finishCallback = [](GrGpuFinishedContext c) {
2193 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002194 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002195 auto manager = context->fMappedBufferManager;
2196 size_t rowBytes = SkToSizeT(context->fSize.width());
2197 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2198 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002199 delete context;
2200 return;
2201 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002202 rowBytes /= 2;
2203 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2204 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2205 (*context->fClientCallback)(context->fClientContext, nullptr);
2206 delete context;
2207 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002208 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002209 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2210 (*context->fClientCallback)(context->fClientContext, nullptr);
2211 delete context;
2212 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002213 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002214 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002215 delete context;
2216 };
2217 GrFlushInfo flushInfo;
2218 flushInfo.fFinishedContext = finishContext;
2219 flushInfo.fFinishedProc = finishCallback;
2220 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002221}
2222
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002223GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2224 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002225 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002226 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002227 if (info.fSubmittedProc) {
2228 info.fSubmittedProc(info.fSubmittedContext, false);
2229 }
2230 if (info.fFinishedProc) {
2231 info.fFinishedProc(info.fFinishedContext);
2232 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002233 return GrSemaphoresSubmitted::kNo;
2234 }
robertphillips8c523e02016-07-26 07:41:00 -07002235 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002236 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002237
Greg Daniel55f040b2020-02-13 15:38:32 +00002238 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002239}
2240
Greg Danielc64ee462017-06-15 16:59:49 -04002241bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002242 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002243 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002244 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002245 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002246 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002247
2248 AutoCheckFlush acf(this->drawingManager());
2249
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002250 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002251 return false;
2252 }
2253
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002254 auto direct = fContext->priv().asDirectContext();
2255 if (!direct) {
2256 return false;
2257 }
2258
2259 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002260
Greg Daniel301015c2019-11-18 14:06:46 -05002261 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2262 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002263 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002264 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002265 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2266 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002267 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002268 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2269 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002270 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002271}
joshualitt33a5fce2015-11-18 13:28:51 -08002272
Michael Ludwig7c12e282020-05-29 09:54:07 -04002273void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002274 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002275 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002276 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002277 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002278 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002279 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002280 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002281 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002282 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2283
Michael Ludwig2686d692020-04-17 20:21:37 +00002284 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002285
Robert Phillips27927a52018-08-20 13:18:12 -04002286 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002287}
2288
Michael Ludwig7c12e282020-05-29 09:54:07 -04002289void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002290 GrPaint&& paint,
2291 GrAA aa,
2292 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002293 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002294 ASSERT_SINGLE_OWNER
2295 RETURN_IF_ABANDONED
2296 SkDEBUGCODE(this->validate();)
2297 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2298
Brian Salomon2fad74a2017-12-20 13:28:55 -05002299 if (shape.isEmpty()) {
2300 if (shape.inverseFilled()) {
2301 this->drawPaint(clip, std::move(paint), viewMatrix);
2302 }
2303 return;
robertphillipsea461502015-05-26 11:38:03 -07002304 }
2305
Robert Phillips72152832017-01-25 17:31:35 -05002306 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002307
Brian Salomon2fad74a2017-12-20 13:28:55 -05002308 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002309 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002310 SkRRect rrect;
2311 // We can ignore the starting point and direction since there is no path effect.
2312 bool inverted;
2313 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2314 if (rrect.isRect()) {
2315 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2316 &shape.style());
2317 return;
2318 } else if (rrect.isOval()) {
2319 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002320 return;
2321 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002322 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2323 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002324 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2325 viewMatrix.rectStaysRect()) {
2326 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2327 // the matrix to all the points individually rather than just to the rect
2328 SkRect rects[2];
2329 if (shape.asNestedRects(rects)) {
2330 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002331 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002332 fContext, std::move(paint), viewMatrix, rects);
2333 if (op) {
2334 this->addDrawOp(clip, std::move(op));
2335 }
2336 // Returning here indicates that there is nothing to draw in this case.
2337 return;
2338 }
robertphillipsea461502015-05-26 11:38:03 -07002339 }
2340 }
robertphillips4bc31812016-03-01 12:22:49 -08002341
Michael Ludwig6a6de652020-04-30 20:16:36 -04002342 // If we get here in drawShape(), we definitely need to use path rendering
2343 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2344 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002345}
2346
Michael Ludwig7c12e282020-05-29 09:54:07 -04002347bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002348 const GrUserStencilSettings* ss,
2349 SkRegion::Op op,
2350 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002351 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002352 const SkMatrix& viewMatrix,
2353 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002354 ASSERT_SINGLE_OWNER_PRIV
2355 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002356 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002357 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2358 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002359
2360 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002361 GrPaint paint;
2362 paint.setCoverageSetOpXPFactory(op, invert);
2363 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2364 SkRect::MakeIWH(fRenderTargetContext->width(),
2365 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002366 return true;
2367 }
2368
Robert Phillips72152832017-01-25 17:31:35 -05002369 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002370
2371 // An Assumption here is that path renderer would use some form of tweaking
2372 // the src color (either the input alpha or in the frag shader) to implement
2373 // aa. If we have some future driver-mojo path AA that can do the right
2374 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002375 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002376 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002377
Michael Ludwig7c12e282020-05-29 09:54:07 -04002378 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002379
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002380 GrPaint paint;
2381 paint.setCoverageSetOpXPFactory(op, invert);
2382
Michael Ludwig2686d692020-04-17 20:21:37 +00002383 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002384 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002385 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002386 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002387 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002388 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002389 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002390 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002391 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002392 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2393 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002394 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002395
2396 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002397 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002398 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002399 if (!pr) {
2400 return false;
2401 }
2402
Brian Salomonf3569f02017-10-24 12:52:33 -04002403 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2404 std::move(paint),
2405 ss,
2406 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002407 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002408 &clipConservativeBounds,
2409 &viewMatrix,
2410 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002411 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002412 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002413 pr->drawPath(args);
2414 return true;
2415}
2416
Brian Osman11052242016-10-27 14:47:55 -04002417SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002418 ASSERT_SINGLE_OWNER_PRIV
2419
Robert Phillips6a6de562019-02-15 15:19:15 -05002420 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002421 return SkBudgeted::kNo;
2422 }
2423
Brian Osman11052242016-10-27 14:47:55 -04002424 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002425
Greg Daniel3912a4b2020-01-14 09:56:04 -05002426 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002427}
2428
Michael Ludwig7c12e282020-05-29 09:54:07 -04002429void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002430 GrPaint&& paint,
2431 GrAA aa,
2432 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002433 const GrStyledShape& originalShape,
2434 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002435 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002436 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002437 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2438
Jim Van Verthf86073a2018-10-02 13:05:38 -04002439 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2440 return;
2441 }
2442
Michael Ludwig6a6de652020-04-30 20:16:36 -04002443 if (attemptShapeFallback && originalShape.simplified()) {
2444 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2445 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2446 // we ought to try again instead of going right to path rendering.
2447 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2448 return;
2449 }
2450
Michael Ludwig7c12e282020-05-29 09:54:07 -04002451 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002452
Michael Ludwig2686d692020-04-17 20:21:37 +00002453 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002454 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002455
robertphillips68737822015-10-29 12:12:21 -07002456 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002457 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002458 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002459 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002460 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002461 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002462 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002463 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002464 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002465
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002466 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002467 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002468 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002469 return;
2470 }
2471
Chris Dalton6ce447a2019-06-23 18:07:38 -06002472 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002473
2474 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2475 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2476 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2477
Brian Salomon2fad74a2017-12-20 13:28:55 -05002478 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002479 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002480 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2481 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002482 return;
2483 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002484 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002485 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002486 }
2487 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002488 if (canDrawArgs.fShape->style().applies()) {
2489 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2490 styleScale);
2491 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002492 return;
2493 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002494 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002495 // This time, allow SW renderer
2496 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2497 } else {
2498 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002499#if GR_PATH_RENDERER_SPEW
2500 SkDebugf("falling back to: %s\n", pr->name());
2501#endif
bsalomon6663acf2016-05-10 09:14:17 -07002502 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002503 }
robertphillipsea461502015-05-26 11:38:03 -07002504
bsalomon8acedde2016-06-24 10:42:16 -07002505 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002506#ifdef SK_DEBUG
2507 SkDebugf("Unable to find path renderer compatible with path.\n");
2508#endif
2509 return;
2510 }
2511
Robert Phillips256c37b2017-03-01 14:32:46 -05002512 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002513 std::move(paint),
2514 &GrUserStencilSettings::kUnused,
2515 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002516 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002517 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002518 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002519 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002520 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002521 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002522 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002523}
2524
Brian Salomon467921e2017-03-06 16:17:12 -05002525static void op_bounds(SkRect* bounds, const GrOp* op) {
2526 *bounds = op->bounds();
2527 if (op->hasZeroArea()) {
2528 if (op->hasAABloat()) {
2529 bounds->outset(0.5f, 0.5f);
2530 } else {
2531 // We don't know which way the particular GPU will snap lines or points at integer
2532 // coords. So we ensure that the bounds is large enough for either snap.
2533 SkRect before = *bounds;
2534 bounds->roundOut(bounds);
2535 if (bounds->fLeft == before.fLeft) {
2536 bounds->fLeft -= 1;
2537 }
2538 if (bounds->fTop == before.fTop) {
2539 bounds->fTop -= 1;
2540 }
2541 if (bounds->fRight == before.fRight) {
2542 bounds->fRight += 1;
2543 }
2544 if (bounds->fBottom == before.fBottom) {
2545 bounds->fBottom += 1;
2546 }
2547 }
2548 }
2549}
2550
Chris Dalton08755122019-08-05 16:13:47 -06002551void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002552 GrDrawingManager* drawingMgr = this->drawingManager();
2553 this->getOpsTask()->addOp(drawingMgr,
2554 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06002555}
2556
Michael Ludwig7c12e282020-05-29 09:54:07 -04002557void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002558 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002559 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002560 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002561 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002562 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002563 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002564 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002565 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002566 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002567
Brian Salomon467921e2017-03-06 16:17:12 -05002568 // Setup clip
2569 SkRect bounds;
2570 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002571 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002572 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002573 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002574 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002575
Chris Dalton9acf6822019-11-12 11:52:40 -07002576 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002577 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002578 }
2579
Michael Ludwig7c12e282020-05-29 09:54:07 -04002580 bool skipDraw = false;
2581 if (clip) {
2582 // Have a complex clip, so defer to its early clip culling
2583 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2584 skipDraw = true;
2585 }
2586 } else {
2587 // No clipping, so just clip the bounds against the logical render target dimensions
2588 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2589 skipDraw = true;
2590 }
2591 }
2592
2593 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002594 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002595 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002596 }
2597
Chris Dalton9acf6822019-11-12 11:52:40 -07002598 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2599 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2600
2601 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2602 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2603 // that uses a stencil test when the stencil buffer is multisampled.)
2604 bool hasMixedSampledCoverage = (
2605 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002606 SkASSERT(!hasMixedSampledCoverage ||
2607 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002608
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002609 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002610 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002611 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2612
Michael Ludwig28e5f112020-06-09 10:57:24 -04002613 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2614 op->setClippedBounds(bounds);
2615
Greg Daniel524e28b2019-11-01 11:48:53 -04002616 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002617 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002618 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002619 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002620 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002621 }
2622 }
2623
Greg Danielf41b2bd2019-08-22 16:19:24 -04002624 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002625 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002626 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002627 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002628 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
2629 dstProxyView,GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002630}
2631
Michael Ludwig28e5f112020-06-09 10:57:24 -04002632bool GrRenderTargetContext::setupDstProxyView(const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002633 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002634 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2635 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2636 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002637 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002638 return false;
2639 }
2640
Greg Daniel3912a4b2020-01-14 09:56:04 -05002641 if (this->caps()->textureBarrierSupport() &&
2642 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002643 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002644 // The render target is a texture, so we can read from it directly in the shader. The XP
2645 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002646 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002647 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002648 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002649 }
2650 }
2651
Greg Daniel3155f7f2020-01-16 16:54:26 -05002652 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002653 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2654 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002655 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002656 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002657
Michael Ludwig28e5f112020-06-09 10:57:24 -04002658 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002659 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002660 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2661 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2662 // centers during rasterization.
2663 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2664 conservativeDrawBounds.outset(1, 1);
2665 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002666 }
Brian Salomon467921e2017-03-06 16:17:12 -05002667
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002668 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002669 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002670 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002671 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002672 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002673 } else {
Eric Karl74480882017-04-03 14:49:05 -07002674 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002675 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002676 }
Brian Salomonc5243782020-04-02 12:50:34 -04002677 auto copy =
2678 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2679 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2680 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002681
Brian Salomonc5243782020-04-02 12:50:34 -04002682 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002683 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002684 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002685}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002686
Greg Daniel573312e2020-02-07 17:22:35 -05002687bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002688 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002689 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002690 SkIRect clippedSrcRect;
2691 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002692 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2693 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002694 return false;
2695 }
2696
2697 GrPaint paint;
2698 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002699
Greg Danield2ccbb52020-02-05 10:45:39 -05002700 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002701 if (!fp) {
2702 return false;
2703 }
2704 paint.addColorFragmentProcessor(std::move(fp));
2705
2706 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002707 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002708 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2709 clippedSrcRect.height()),
2710 SkRect::Make(clippedSrcRect));
2711 return true;
2712}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002713
2714void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2715 SkASSERT(&task == fOpsTask.get());
2716 fOpsTask.reset();
2717}