blob: d9727adf898557711752d743777fff4b95b942fb [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"
Michael Ludwig58f569b2020-05-21 17:03:08 -040032#include "src/gpu/GrClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040033#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040035#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#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 Derby082232b2020-06-10 15:08:18 -0400488 GrTextContext::Options options = {
489 fContext->priv().options().fMinDistanceFieldFontSize,
490 fContext->priv().options().fGlyphsAsPathsFontSize
491 };
492 GrTextContext::SanitizeOptions(&options);
493
Herb Derby64688222020-06-08 14:19:57 -0400494 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
495
496 // Get the first paint to use as the key paint.
497 const SkPaint& blobPaint = glyphRunList.paint();
498
499 SkPoint drawOrigin = glyphRunList.origin();
500
501 SkMaskFilterBase::BlurRec blurRec;
502 // It might be worth caching these things, but its not clear at this time
503 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
504 const SkMaskFilter* mf = blobPaint.getMaskFilter();
505 bool canCache = glyphRunList.canCache() &&
506 !(blobPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
507
508 // If we're doing linear blending, then we can disable the gamma hacks.
509 // Otherwise, leave them on. In either case, we still want the contrast boost:
510 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
511 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
512 ? SkScalerContextFlags::kBoostContrast
513 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
514
515 sk_sp<GrTextBlob> blob;
516 GrTextBlob::Key key;
517 if (canCache) {
518 bool hasLCD = glyphRunList.anyRunsLCD();
519
520 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
521 SkPixelGeometry pixelGeometry =
522 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
523
524 GrColor canonicalColor = compute_canonical_color(blobPaint, hasLCD);
525
526 key.fPixelGeometry = pixelGeometry;
527 key.fUniqueID = glyphRunList.uniqueID();
528 key.fStyle = blobPaint.getStyle();
529 key.fHasBlur = SkToBool(mf);
530 key.fCanonicalColor = canonicalColor;
531 key.fScalerContextFlags = scalerContextFlags;
532 blob = textBlobCache->find(key);
533 }
534
535 const SkMatrix& drawMatrix(matrixProvider.localToDevice());
536 if (blob != nullptr && blob->canReuse(blobPaint, blurRec, drawMatrix, drawOrigin)) {
537 // Reusing the blob. Move it to the front of LRU cache.
538 textBlobCache->makeMRU(blob.get());
539 } else {
540 // Build or Rebuild the GrTextBlob
541 if (blob != nullptr) {
542 // We have to remake the blob because changes may invalidate our masks.
543 // TODO we could probably get away with reuse most of the time if the pointer is unique,
544 // but we'd have to clear the SubRun information
545 textBlobCache->remove(blob.get());
546 }
547 if (canCache) {
548 blob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, drawMatrix);
549 } else {
550 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
551 }
552 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
553 fGlyphPainter.processGlyphRunList(
554 glyphRunList, drawMatrix, fSurfaceProps, supportsSDFT, options, blob.get());
555 }
556
557 blob->insertOpsIntoTarget(
558 fTextTarget.get(), fSurfaceProps, blobPaint, clip, matrixProvider, drawOrigin);
robertphillipsea461502015-05-26 11:38:03 -0700559}
560
Brian Osman11052242016-10-27 14:47:55 -0400561void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800562 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700563 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700564 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400565 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700566
Robert Phillips72152832017-01-25 17:31:35 -0500567 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400568
Greg Danielf41b2bd2019-08-22 16:19:24 -0400569 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700570}
571
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500572static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
573 paint->setColor4f(color);
574 if (color.isOpaque()) {
575 // Can just rely on the src-over blend mode to do the right thing
576 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
577 } else {
578 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
579 // were src blended
580 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
581 }
582}
583
Michael Ludwig81d41722020-05-26 16:57:38 -0400584// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
585// premul alpha type. If we ever support different alpha type render targets, this function should
586// transform the color as appropriate.
587void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500588 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400589 bool upgradePartialToFull) {
590 ASSERT_SINGLE_OWNER
591 RETURN_IF_ABANDONED
592 SkDEBUGCODE(this->validate();)
593 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
594
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400595 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
596 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
597 // supports then. Drawing an axis-aligned rect is the fallback path.
598 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
599 if (scissor && !scissorState.set(*scissor)) {
600 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
601 // except clear ops are not draw ops).
602 return;
603 }
604
605 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
606 // the test. We only do this when the clear would be handled by a load op or natively.
607 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
608 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
609 this->caps()->shouldInitializeTextures())) {
610 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
611 // only clear the entire target if we knew it had not been cleared before. As
612 // is this could end up doing a lot of redundant clears.
613 scissorState.setDisabled();
614 } else {
615 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
616 // render target to overflow into any approx-fit padding of the backing store dimensions
617 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400618 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500619 }
620
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400621 if (!scissorState.enabled()) {
622 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
623 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400624 GrOpsTask* opsTask = this->getOpsTask();
625 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500626 !this->caps()->performColorClearsAsDraws()) {
627 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500629 return;
630 } else {
631 // Will use an op for the clear, reset the load op to discard since the op will
632 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400633 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500634 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400635 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500636
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400637 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
638 // as a load op.
639 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
640 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
641 if (clearAsDraw) {
642 GrPaint paint;
643 clear_to_grpaint(color, &paint);
644 this->addDrawOp(nullptr,
645 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
646 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500647 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400648 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500649 }
650}
651
Michael Ludwig7c12e282020-05-29 09:54:07 -0400652void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500653 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400654 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000655 // Start with the render target, since that is the maximum content we could possibly fill.
656 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500657 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400658 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000659 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
660 // inverse view matrix.
661 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
662 } else {
663 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
664 SkMatrix localMatrix;
665 if (!viewMatrix.invert(&localMatrix)) {
666 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400667 }
Michael Ludwig61328202019-06-19 14:48:58 +0000668 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
669 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700670 }
robertphillipsea461502015-05-26 11:38:03 -0700671}
672
Michael Ludwig61328202019-06-19 14:48:58 +0000673enum class GrRenderTargetContext::QuadOptimization {
674 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
675 kDiscarded,
676 // The rect to draw was converted to some other op and appended to the oplist, so no additional
677 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
678 // a constColor is provided.
679 kSubmitted,
680 // The clip was folded into the device quad, with updated edge flags and local coords, and
681 // caller is responsible for adding an appropriate op.
682 kClipApplied,
683 // No change to clip, but quad updated to better fit clip/render target, and caller is
684 // responsible for adding an appropriate op.
685 kCropped
686};
Michael Ludwig61a16512019-01-15 11:15:13 -0500687
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400688static bool make_vertex_finite(float* value) {
689 if (SkScalarIsNaN(*value)) {
690 return false;
691 }
692
693 if (!SkScalarIsFinite(*value)) {
694 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
695 // left when calculating crops.
696 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
697 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
698 }
699
700 return true;
701}
702
Michael Ludwig7c12e282020-05-29 09:54:07 -0400703static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
Michael Ludwige06a8972020-06-11 10:29:00 -0400704 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
Michael Ludwig7c12e282020-05-29 09:54:07 -0400705}
706
Michael Ludwig61328202019-06-19 14:48:58 +0000707GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400708 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500709 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000710 // Optimization requirements:
711 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500712 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
713 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
714 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
715 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
716 // 5. kCropped in all other scenarios (although a crop may be a no-op)
717
718 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
719 // better to just keep the old flags instead of introducing mixed edge flags.
720 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000721
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400722 // Use the logical size of the render target, which allows for "fullscreen" clears even if
723 // the render target has an approximate backing fit
724 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400725
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500726 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000727 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400728 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
729 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500730 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400731 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500732 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
733 !make_vertex_finite(quad->fDevice.ys() + i) ||
734 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400735 // Discard if we see a nan
736 return QuadOptimization::kDiscarded;
737 }
738 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500739 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400740 }
741 } else {
742 // CropToRect requires the quads to be finite. If they are not finite and we have local
743 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500744 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400745 return QuadOptimization::kDiscarded;
746 }
Brian Salomon7694b902019-06-18 21:00:21 +0000747 }
748
Michael Ludwig61328202019-06-19 14:48:58 +0000749 // If the quad is entirely off screen, it doesn't matter what the clip does
750 if (!rtRect.intersects(drawBounds)) {
751 return QuadOptimization::kDiscarded;
752 }
Brian Salomon7694b902019-06-18 21:00:21 +0000753
Michael Ludwig61328202019-06-19 14:48:58 +0000754 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
755 // the render target).
756 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400757 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
758 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
759 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
760 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000761 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400762 if (clip && !clip->quickContains(rtRect)) {
Michael Ludwige06a8972020-06-11 10:29:00 -0400763 if (!clip->isRRect(&clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000764 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000765 }
Brian Salomon7694b902019-06-18 21:00:21 +0000766 }
767
Michael Ludwig61328202019-06-19 14:48:58 +0000768 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
769 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400770 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000771 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
772 // of the render target and the clip rect)
773 SkRect clipBounds = rtRect;
774 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
775 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000776 }
777
Michael Ludwig61328202019-06-19 14:48:58 +0000778 if (clipRRect.isRect()) {
779 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500780 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700781 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500782 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000783 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500784 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000785 if (drawBounds.contains(rtRect)) {
786 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400787 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000788 return QuadOptimization::kSubmitted;
789 } else if (GrClip::IsPixelAligned(drawBounds) &&
790 drawBounds.width() > 256 && drawBounds.height() > 256) {
791 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
792 SkIRect scissorRect;
793 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400794 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000795 return QuadOptimization::kSubmitted;
796 }
797 }
798
799 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000800 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500801 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000802 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
803 // properly reflect the smooth edge of the clip.
804 *aa = GrAA::kYes;
805 }
806 // We intentionally do not downgrade AA here because we don't know if we need to
807 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
808 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
809
810 // deviceQuad is exactly the intersection of original quad and clip, so it can be
811 // drawn with no clip (submitted by caller)
812 return QuadOptimization::kClipApplied;
813 } else {
814 // The quads have been updated to better fit the clip bounds, but can't get rid of
815 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500816 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000817 return QuadOptimization::kCropped;
818 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700819 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000820 // Rounded corners and constant filled color (limit ourselves to solid colors because
821 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500822 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
823 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
824 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000825 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
826 // we can draw the rrect directly and ignore the edge flags
827 GrPaint paint;
828 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400829 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000830 clipRRect, GrStyle::SimpleFill());
831 return QuadOptimization::kSubmitted;
832 } else {
833 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500834 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000835 return QuadOptimization::kCropped;
836 }
Brian Salomon7694b902019-06-18 21:00:21 +0000837 }
Brian Salomon7694b902019-06-18 21:00:21 +0000838 }
839
Michael Ludwig61328202019-06-19 14:48:58 +0000840 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400841 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000842
843 // One final check for discarding, since we may have gone here directly due to a complex clip
844 if (!clipBounds.intersects(drawBounds)) {
845 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000846 }
847
Michael Ludwig61328202019-06-19 14:48:58 +0000848 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
849 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500850 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
851 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000852
853 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000854}
855
Michael Ludwig7c12e282020-05-29 09:54:07 -0400856void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500857 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500858 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500859 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400860 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000861 ASSERT_SINGLE_OWNER
862 RETURN_IF_ABANDONED
863 SkDEBUGCODE(this->validate();)
864 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500865
Michael Ludwig61328202019-06-19 14:48:58 +0000866 AutoCheckFlush acf(this->drawingManager());
867
868 SkPMColor4f* constColor = nullptr;
869 SkPMColor4f paintColor;
870 if (!ss && !paint.numCoverageFragmentProcessors() &&
871 paint.isConstantBlendedColor(&paintColor)) {
872 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
873 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500874 }
875
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500876 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000877 if (opt >= QuadOptimization::kClipApplied) {
878 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400879 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000880 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
881 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500882 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
883 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700884 }
Michael Ludwig61328202019-06-19 14:48:58 +0000885 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800886}
887
Michael Ludwig7c12e282020-05-29 09:54:07 -0400888void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400889 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500890 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000891 sk_sp<GrColorSpaceXform> textureXform,
892 GrSamplerState::Filter filter,
893 const SkPMColor4f& color,
894 SkBlendMode blendMode,
895 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500896 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400897 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000898 ASSERT_SINGLE_OWNER
899 RETURN_IF_ABANDONED
900 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400901 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000902 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
903
904 AutoCheckFlush acf(this->drawingManager());
905
906 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
907 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500908 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000909
910 SkASSERT(opt != QuadOptimization::kSubmitted);
911 if (opt != QuadOptimization::kDiscarded) {
912 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400913 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000914 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400915 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400916 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
917 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400918 // Use the provided subset, although hypothetically we could detect that the cropped local
919 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500920 this->addDrawOp(finalClip,
921 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
922 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400923 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000924 }
925}
926
Michael Ludwig7c12e282020-05-29 09:54:07 -0400927void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500928 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500929 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400930 const SkMatrix& viewMatrix,
931 const SkRect& rect,
932 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700933 if (!style) {
934 style = &GrStyle::SimpleFill();
935 }
joshualitt1de610a2016-01-06 08:26:09 -0800936 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700937 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700938 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400939 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700940
bsalomon6663acf2016-05-10 09:14:17 -0700941 // Path effects should've been devolved to a path in SkGpuDevice
942 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700943
Robert Phillips72152832017-01-25 17:31:35 -0500944 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700945
bsalomon6663acf2016-05-10 09:14:17 -0700946 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400947 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000948 // Fills the rect, using rect as its own local coordinates
949 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500950 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400951 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
952 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
953 (rect.width() && rect.height())) {
954 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
955 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400956 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700957
Chris Dalton7d6748e2019-03-13 00:34:52 -0600958 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500959 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
960 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
961 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500962 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400963 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700964 return;
robertphillips4bc31812016-03-01 12:22:49 -0800965 }
robertphillips4bc31812016-03-01 12:22:49 -0800966 }
Mike Klein16885072018-12-11 09:54:31 -0500967 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000968 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
969 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700970}
971
Michael Ludwig7c12e282020-05-29 09:54:07 -0400972void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500973 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
974 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600975 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500976
977 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
978 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500979}
980
Robert Phillipsec2249f2016-11-09 08:54:35 -0500981int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500982 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400983 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500984}
985
Greg Danielf41b2bd2019-08-22 16:19:24 -0400986GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600987 ) const {
988#if GR_TEST_UTILS
989 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400990 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600991 }
992#endif
993 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
994 // would normally be overwritten. The one exception is if the render target context is marked as
995 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
996 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
997 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
998 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400999 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -06001000}
1001
Chris Dalton858cf232019-10-14 16:20:00 -06001002void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -06001003 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -06001004 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -06001005 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -06001006
Chris Daltoneffee202019-07-01 22:28:03 -06001007 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -06001008 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -05001009 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -06001010 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -04001011 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -06001012 }
1013 SkASSERT(numRequiredSamples > 0);
1014
1015 if (numRequiredSamples > fNumStencilSamples) {
1016 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -05001017 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -06001018 }
Chris Dalton6b982802019-06-27 13:53:46 -06001019
Chris Dalton674f77a2019-09-30 20:49:39 -06001020 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -06001021 if (this->caps()->performStencilClearsAsDraws()) {
1022 // There is a driver bug with clearing stencil. We must use an op to manually clear the
1023 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -04001024 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -06001025 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -06001026 this->getOpsTask()->setInitialStencilContent(
1027 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -06001028 }
1029 }
1030}
1031
Michael Ludwig81d41722020-05-26 16:57:38 -04001032void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001033 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1034
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001035 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
1036 if (scissor && !scissorState.set(*scissor)) {
1037 // The requested clear region is off screen, so nothing to do.
1038 return;
1039 }
1040
Michael Ludwig1e632792020-05-21 12:45:31 -04001041 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001042 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -04001043 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001044 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001045
1046 // Configure the paint to have no impact on the color buffer
1047 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001048 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001049 this->addDrawOp(nullptr,
1050 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1051 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001052 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -04001053 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -05001054 }
robertphillips976f5f02016-06-03 10:59:20 -07001055}
1056
Michael Ludwig7c12e282020-05-29 09:54:07 -04001057void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001058 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001059 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001060 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001061 ASSERT_SINGLE_OWNER_PRIV
1062 RETURN_IF_ABANDONED_PRIV
1063 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001064 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1065 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001066
Brian Salomon467921e2017-03-06 16:17:12 -05001067 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1068 SkASSERT(path);
1069 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1070
1071 // FIXME: Use path bounds instead of this WAR once
1072 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1073 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1074
Michael Ludwig7c12e282020-05-29 09:54:07 -04001075 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
1076 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
1077 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001078 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
1079 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
1080
Michael Ludwige06a8972020-06-11 10:29:00 -04001081 if (clip && !clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001082 return;
1083 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001084 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1085 // but as it is, we're just using the full render target so intersecting the two bounds would
1086 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001087
Robert Phillips7c525e62018-06-12 10:11:12 -04001088 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1089 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001090 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001091 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001092 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001093 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001094 if (!op) {
1095 return;
1096 }
Brian Salomon97180af2017-03-14 13:42:58 -04001097 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001098
Chris Daltoneffee202019-07-01 22:28:03 -06001099 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001100 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001101}
1102
Michael Ludwig7c12e282020-05-29 09:54:07 -04001103void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001104 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001105 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001106 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1107 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001108 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001109 ASSERT_SINGLE_OWNER
1110 RETURN_IF_ABANDONED
1111 SkDEBUGCODE(this->validate();)
1112 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001113
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001114 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1115 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1116 AutoCheckFlush acf(this->drawingManager());
1117 GrAAType aaType = this->chooseAAType(aa);
1118 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1119 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1120 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001121 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1122 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001123}
1124
Michael Ludwig7c12e282020-05-29 09:54:07 -04001125void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001126 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001127 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001128 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001129 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001130 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001131 ASSERT_SINGLE_OWNER
1132 RETURN_IF_ABANDONED
1133 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001134 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001135
1136 AutoCheckFlush acf(this->drawingManager());
1137
1138 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001139 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001140 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001141 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001142 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001143 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001144 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001145}
1146
1147///////////////////////////////////////////////////////////////////////////////
1148
Michael Ludwig7c12e282020-05-29 09:54:07 -04001149void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001150 GrPaint&& paint,
1151 const SkMatrix& viewMatrix,
1152 int spriteCount,
1153 const SkRSXform xform[],
1154 const SkRect texRect[],
1155 const SkColor colors[]) {
1156 ASSERT_SINGLE_OWNER
1157 RETURN_IF_ABANDONED
1158 SkDEBUGCODE(this->validate();)
1159 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1160
1161 AutoCheckFlush acf(this->drawingManager());
1162
1163 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1164 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1165 aaType, spriteCount, xform, texRect, colors);
1166 this->addDrawOp(clip, std::move(op));
1167}
1168
1169///////////////////////////////////////////////////////////////////////////////
1170
Michael Ludwig7c12e282020-05-29 09:54:07 -04001171void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001172 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001173 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001174 const SkMatrix& viewMatrix,
1175 const SkRRect& rrect,
1176 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001177 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001178 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001179 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001180 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001181
1182 const SkStrokeRec& stroke = style.strokeRec();
1183 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001184 return;
1185 }
1186
Michael Ludwig7c12e282020-05-29 09:54:07 -04001187 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001188#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1189 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001190 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001191 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1192 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001193 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1194 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001195 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001196 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1197 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1198 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001199 }
1200#endif
bsalomon6663acf2016-05-10 09:14:17 -07001201 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001202
Robert Phillips72152832017-01-25 17:31:35 -05001203 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001204
Chris Dalton7d6748e2019-03-13 00:34:52 -06001205 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001206
Chris Dalton0dffbab2019-03-27 13:08:50 -06001207 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001208 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1209 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1210 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1211 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1212 // to avoid perf regressions on some platforms.
1213 assert_alive(paint);
1214 op = GrOvalOpFactory::MakeCircularRRectOp(
1215 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1216 }
1217 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001218 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001219 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001220 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001221 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001222 assert_alive(paint);
1223 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001224 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001225 }
1226 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001227 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001228 return;
robertphillipsea461502015-05-26 11:38:03 -07001229 }
robertphillipsb56f9272016-02-25 11:03:52 -08001230
Mike Klein16885072018-12-11 09:54:31 -05001231 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001232 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001233 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001234}
1235
Jim Van Verthc5903412016-11-17 15:27:09 -05001236///////////////////////////////////////////////////////////////////////////////
1237
Jim Van Verth3af1af92017-05-18 15:06:54 -04001238static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1239 SkPoint3 result;
1240 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1241 result.fZ = pt.fZ;
1242 return result;
1243}
1244
Michael Ludwig7c12e282020-05-29 09:54:07 -04001245bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001246 const SkMatrix& viewMatrix,
1247 const SkPath& path,
1248 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001249 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001250 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001251 return true;
1252 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001253 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001254 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255
1256 // check z plane
1257 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1258 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1259 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1260 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1261 return false;
1262 }
1263
1264 SkRRect rrect;
1265 SkRect rect;
1266 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001267 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001268 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1269 if (!isRRect &&
1270 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1271 rect.width() > SK_ScalarNearlyZero) {
1272 rrect.setOval(rect);
1273 isRRect = true;
1274 }
1275 if (!isRRect && path.isRect(&rect)) {
1276 rrect.setRect(rect);
1277 isRRect = true;
1278 }
1279
1280 if (!isRRect) {
1281 return false;
1282 }
1283
Jim Van Verthc5903412016-11-17 15:27:09 -05001284 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001285 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001286 }
1287
Robert Phillips72152832017-01-25 17:31:35 -05001288 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001289
Jim Van Verth3af1af92017-05-18 15:06:54 -04001290 // transform light
1291 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1292
1293 // 1/scale
1294 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001295 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001296 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1297 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1298
1299 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001300 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1301
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001302 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001303 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1304 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1305 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001306
1307 // Outset the shadow rrect to the border of the penumbra
1308 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1309 SkRRect ambientRRect;
1310 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1311 // If the rrect was an oval then its outset will also be one.
1312 // We set it explicitly to avoid errors.
1313 if (rrect.isOval()) {
1314 ambientRRect = SkRRect::MakeOval(outsetRect);
1315 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001316 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001317 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1318 }
1319
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001320 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001321 if (transparent) {
1322 // set a large inset to force a fill
1323 devSpaceInsetWidth = ambientRRect.width();
1324 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001325
Robert Phillips7c525e62018-06-12 10:11:12 -04001326 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1327 ambientColor,
1328 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001329 ambientRRect,
1330 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001331 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001332 if (op) {
1333 this->addDrawOp(clip, std::move(op));
1334 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001335 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001336
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001337 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001338 SkScalar devSpaceSpotBlur;
1339 SkScalar spotScale;
1340 SkVector spotOffset;
1341 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1342 devLightPos.fZ, rec.fLightRadius,
1343 &devSpaceSpotBlur, &spotScale, &spotOffset);
1344 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001345 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1346
Jim Van Verth3af1af92017-05-18 15:06:54 -04001347 // Adjust translate for the effect of the scale.
1348 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1349 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1350 // This offset is in dev space, need to transform it into source space.
1351 SkMatrix ctmInverse;
1352 if (viewMatrix.invert(&ctmInverse)) {
1353 ctmInverse.mapPoints(&spotOffset, 1);
1354 } else {
1355 // Since the matrix is a similarity, this should never happen, but just in case...
1356 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1357 SkASSERT(false);
1358 }
1359
1360 // Compute the transformed shadow rrect
1361 SkRRect spotShadowRRect;
1362 SkMatrix shadowTransform;
1363 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1364 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001365 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366
1367 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001368 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001369 SkScalar insetWidth = blurOutset;
1370 if (transparent) {
1371 // If transparent, just do a fill
1372 insetWidth += spotShadowRRect.width();
1373 } else {
1374 // For shadows, instead of using a stroke we specify an inset from the penumbra
1375 // border. We want to extend this inset area so that it meets up with the caster
1376 // geometry. The inset geometry will by default already be inset by the blur width.
1377 //
1378 // We compare the min and max corners inset by the radius between the original
1379 // rrect and the shadow rrect. The distance between the two plus the difference
1380 // between the scaled radius and the original radius gives the distance from the
1381 // transformed shadow shape to the original shape in that corner. The max
1382 // of these gives the maximum distance we need to cover.
1383 //
1384 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1385 // that to get the full insetWidth.
1386 SkScalar maxOffset;
1387 if (rrect.isRect()) {
1388 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001389 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001390 rrect.rect().fLeft),
1391 SkTAbs(spotShadowRRect.rect().fTop -
1392 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001393 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001394 rrect.rect().fRight),
1395 SkTAbs(spotShadowRRect.rect().fBottom -
1396 rrect.rect().fBottom)));
1397 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001398 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001399 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1400 rrect.rect().fLeft + dr,
1401 spotShadowRRect.rect().fTop -
1402 rrect.rect().fTop + dr);
1403 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1404 rrect.rect().fRight - dr,
1405 spotShadowRRect.rect().fBottom -
1406 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001407 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001408 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001409 }
Brian Osman788b9162020-02-07 10:36:46 -05001410 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001411 }
1412
1413 // Outset the shadow rrect to the border of the penumbra
1414 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1415 if (spotShadowRRect.isOval()) {
1416 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1417 } else {
1418 SkScalar outsetRad = spotRadius + blurOutset;
1419 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1420 }
1421
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001422 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001423
Robert Phillips7c525e62018-06-12 10:11:12 -04001424 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1425 spotColor,
1426 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001427 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001428 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001429 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001430 if (op) {
1431 this->addDrawOp(clip, std::move(op));
1432 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001433 }
1434
1435 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001436}
1437
1438///////////////////////////////////////////////////////////////////////////////
1439
Michael Ludwig7c12e282020-05-29 09:54:07 -04001440bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001441 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001442 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001443 const SkMatrix& viewMatrix,
1444 const SkRRect& origOuter,
1445 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001446 SkASSERT(!origInner.isEmpty());
1447 SkASSERT(!origOuter.isEmpty());
1448
Brian Salomon65749212017-12-01 16:01:47 -05001449 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1450
Chris Dalton7d6748e2019-03-13 00:34:52 -06001451 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001452
1453 if (GrAAType::kMSAA == aaType) {
1454 return false;
1455 }
1456
Greg Daniel2655ede2019-04-10 00:49:28 +00001457 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1458 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001459 auto outerR = outer->width() / 2.f;
1460 auto innerR = inner->width() / 2.f;
1461 auto cx = outer->getBounds().fLeft + outerR;
1462 auto cy = outer->getBounds().fTop + outerR;
1463 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1464 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1465 auto avgR = (innerR + outerR) / 2.f;
1466 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1467 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1468 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001469 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001470 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001471 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001472 if (op) {
1473 this->addDrawOp(clip, std::move(op));
1474 return true;
1475 }
Mike Klein16885072018-12-11 09:54:31 -05001476 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001477 }
1478 }
1479
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001480 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001481 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001482 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1483 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001484 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001485 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1486 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001487 }
robertphillips00095892016-02-29 13:50:40 -08001488
robertphillips00095892016-02-29 13:50:40 -08001489 SkMatrix inverseVM;
1490 if (!viewMatrix.isIdentity()) {
1491 if (!origInner.transform(viewMatrix, inner.writable())) {
1492 return false;
1493 }
1494 if (!origOuter.transform(viewMatrix, outer.writable())) {
1495 return false;
1496 }
1497 if (!viewMatrix.invert(&inverseVM)) {
1498 return false;
1499 }
1500 } else {
1501 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001502 }
robertphillips00095892016-02-29 13:50:40 -08001503
Ethan Nicholaseace9352018-10-15 20:09:54 +00001504 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001505 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001506 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001507 if (!innerEffect) {
1508 return false;
1509 }
1510
Ethan Nicholaseace9352018-10-15 20:09:54 +00001511 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001512 if (!outerEffect) {
1513 return false;
1514 }
1515
Brian Salomon82f44312017-01-11 13:42:54 -05001516 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1517 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001518
1519 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001520 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001521 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1522 }
halcanary9d524f22016-03-29 09:03:52 -07001523
Brian Salomon82f44312017-01-11 13:42:54 -05001524 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1525 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001526 return true;
1527}
1528
Michael Ludwig7c12e282020-05-29 09:54:07 -04001529void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001530 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001531 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001532 const SkMatrix& viewMatrix,
1533 const SkRRect& outer,
1534 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001535 ASSERT_SINGLE_OWNER
1536 RETURN_IF_ABANDONED
1537 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001538 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001539
1540 SkASSERT(!outer.isEmpty());
1541 SkASSERT(!inner.isEmpty());
1542
Robert Phillips72152832017-01-25 17:31:35 -05001543 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001544
Brian Salomon82f44312017-01-11 13:42:54 -05001545 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001546 return;
1547 }
Mike Klein16885072018-12-11 09:54:31 -05001548 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001549
1550 SkPath path;
1551 path.setIsVolatile(true);
1552 path.addRRect(inner);
1553 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001554 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001555 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001556}
1557
robertphillipsea461502015-05-26 11:38:03 -07001558///////////////////////////////////////////////////////////////////////////////
1559
Michael Ludwig7c12e282020-05-29 09:54:07 -04001560void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001561 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001562 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001563 const SkMatrix& viewMatrix,
1564 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001565 const GrStyle& style,
1566 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001567 ASSERT_SINGLE_OWNER
1568 RETURN_IF_ABANDONED
1569 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001570 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001571
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001572 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001573 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001574 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001575 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001576 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1577 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001578 aa = GrAA::kNo;
1579 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001580 }
msarettcc319b92016-08-25 18:07:18 -07001581 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001582 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001583 SkPath path;
1584 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001585 path.setIsVolatile(true);
1586
Brian Salomon82f44312017-01-11 13:42:54 -05001587 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001588 }
1589
Chris Dalton7d6748e2019-03-13 00:34:52 -06001590 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001591 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1592 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001593 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001594}
1595
Michael Ludwig7c12e282020-05-29 09:54:07 -04001596void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001597 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001598 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001599 const SkMatrix& viewMatrix,
1600 const SkRect& oval,
1601 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001602 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001603 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001604 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001605 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001606
Robert Phillips7484d202018-07-03 09:09:08 -04001607 const SkStrokeRec& stroke = style.strokeRec();
1608
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001609 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001610 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1611 return;
1612 }
1613
1614 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001615 return;
robertphillipsea461502015-05-26 11:38:03 -07001616 }
1617
Robert Phillips72152832017-01-25 17:31:35 -05001618 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001619
Chris Dalton7d6748e2019-03-13 00:34:52 -06001620 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001621
1622 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001623 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1624 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001625 // We don't draw true circles as round rects in coverage mode, because it can
1626 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1627 assert_alive(paint);
1628 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1629 this->caps()->shaderCaps());
1630 }
1631 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001632 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1633 // the arc equation. This same special geometry and fragment branch also turn out to be a
1634 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1635 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1636 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001637 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001638 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1639 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001640 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001641 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001642 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001643 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1644 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001645 }
1646 if (op) {
1647 this->addDrawOp(clip, std::move(op));
1648 return;
robertphillipsea461502015-05-26 11:38:03 -07001649 }
robertphillipsb56f9272016-02-25 11:03:52 -08001650
Mike Klein16885072018-12-11 09:54:31 -05001651 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001652 this->drawShapeUsingPathRenderer(
1653 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001654 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001655}
1656
Michael Ludwig7c12e282020-05-29 09:54:07 -04001657void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001658 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001660 const SkMatrix& viewMatrix,
1661 const SkRect& oval,
1662 SkScalar startAngle,
1663 SkScalar sweepAngle,
1664 bool useCenter,
1665 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001666 ASSERT_SINGLE_OWNER
1667 RETURN_IF_ABANDONED
1668 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001669 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001670
1671 AutoCheckFlush acf(this->drawingManager());
1672
Chris Dalton7d6748e2019-03-13 00:34:52 -06001673 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001674 if (GrAAType::kCoverage == aaType) {
1675 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1676 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1677 std::move(paint),
1678 viewMatrix,
1679 oval,
1680 startAngle,
1681 sweepAngle,
1682 useCenter,
1683 style,
1684 shaderCaps);
1685 if (op) {
1686 this->addDrawOp(clip, std::move(op));
1687 return;
1688 }
1689 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001690 }
Brian Salomone4949402018-04-26 15:22:04 -04001691 this->drawShapeUsingPathRenderer(
1692 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001693 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001694}
1695
Michael Ludwig7c12e282020-05-29 09:54:07 -04001696void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001697 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001698 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001699 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001700 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001701 sk_sp<GrColorSpaceXform> csxf,
1702 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001703 std::unique_ptr<SkLatticeIter> iter,
1704 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001705 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001706 RETURN_IF_ABANDONED
1707 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001708 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001709
Robert Phillips72152832017-01-25 17:31:35 -05001710 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001711
Brian Salomon2a943df2018-05-04 13:43:19 -04001712 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001713 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001714 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001715 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001716}
1717
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001718void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1719 const SkRect& bounds) {
1720 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1721 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001722 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001723}
1724
Brian Salomon031b0ba2019-05-23 11:05:26 -04001725void GrRenderTargetContext::asyncRescaleAndReadPixels(
1726 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1727 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1728 auto direct = fContext->priv().asDirectContext();
1729 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001730 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001731 return;
1732 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001733 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001734 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001736 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001737 if (this->asRenderTargetProxy()->framebufferOnly()) {
1738 callback(context, nullptr);
1739 return;
1740 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001741 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001742 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001743 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001744 return;
1745 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001747 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001748 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001749 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001750 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001751 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1752 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001753 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001754 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001755 backendFormatOfFinalContext, dstCT);
1756 // Fail if we can't read from the source surface's color type.
1757 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001758 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001759 return;
1760 }
Brian Salomon624f9062019-07-02 14:23:00 -04001761 // Fail if read color type does not have all of dstCT's color channels and those missing color
1762 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001763 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1764 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1765 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1766 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001767 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001768 return;
1769 }
1770
Brian Salomonbf6b9792019-08-21 09:38:10 -04001771 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001772 int x = srcRect.fLeft;
1773 int y = srcRect.fTop;
1774 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001775 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1776 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001777 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001778 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001779 return;
1780 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001781 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001782 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001783 x = y = 0;
1784 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001785 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1786 this->colorInfo().alphaType(),
1787 info.colorSpace(),
1788 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001789 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1790 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001791 // We flip or color convert by drawing and we don't currently support drawing to
1792 // kPremul.
1793 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001794 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001795 return;
1796 }
Greg Daniel40903af2020-01-30 14:55:05 -05001797 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001798 SkRect srcRectToDraw = SkRect::Make(srcRect);
1799 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001800 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001801 texProxyView =
1802 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1803 SkBackingFit::kApprox, SkBudgeted::kNo);
1804 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001805 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001806 return;
1807 }
Brian Salomonc5243782020-04-02 12:50:34 -04001808 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001809 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1810 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001811 tempRTC = GrRenderTargetContext::Make(
1812 direct, this->colorInfo().colorType(), info.refColorSpace(),
1813 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001814 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001815 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001816 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001817 return;
1818 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001819 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001820 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1821 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001822 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1823 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1824 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001825 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001826 }
1827 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001828 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001829 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1830 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001831}
1832
Brian Salomon9241a6d2019-10-03 13:26:54 -04001833class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1834public:
1835 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1836 ~AsyncReadResult() override {
1837 for (int i = 0; i < fPlanes.count(); ++i) {
1838 if (!fPlanes[i].fMappedBuffer) {
1839 delete[] static_cast<const char*>(fPlanes[i].fData);
1840 } else {
1841 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1842 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1843 }
1844 }
1845 }
1846
1847 int count() const override { return fPlanes.count(); }
1848 const void* data(int i) const override { return fPlanes[i].fData; }
1849 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1850
1851 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001852 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001853 size_t rowBytes,
1854 GrClientMappedBufferManager* manager) {
1855 SkASSERT(!result.fTransferBuffer->isMapped());
1856 const void* mappedData = result.fTransferBuffer->map();
1857 if (!mappedData) {
1858 return false;
1859 }
1860 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001861 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001862 result.fPixelConverter(convertedData.get(), mappedData);
1863 this->addCpuPlane(std::move(convertedData), rowBytes);
1864 result.fTransferBuffer->unmap();
1865 } else {
1866 manager->insert(result.fTransferBuffer);
1867 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1868 }
1869 return true;
1870 }
1871
1872 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1873 SkASSERT(data);
1874 SkASSERT(rowBytes > 0);
1875 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1876 }
1877
1878private:
1879 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1880 SkASSERT(data);
1881 SkASSERT(rowBytes > 0);
1882 SkASSERT(mappedBuffer);
1883 SkASSERT(mappedBuffer->isMapped());
1884 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1885 }
1886
1887 struct Plane {
1888 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1889 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1890 const void* fData;
1891 size_t fRowBytes;
1892 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1893 sk_sp<GrGpuBuffer> fMappedBuffer;
1894 };
1895 SkSTArray<3, Plane> fPlanes;
1896 uint32_t fInboxID;
1897};
1898
Brian Salomon024bd002019-06-11 11:38:16 -04001899void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1900 ReadPixelsCallback callback,
1901 ReadPixelsContext context) {
1902 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1903 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1904
Greg Danielca9dbe22020-01-02 13:44:30 -05001905 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001906 callback(context, nullptr);
1907 return;
1908 }
1909
Brian Salomon9241a6d2019-10-03 13:26:54 -04001910 auto directContext = fContext->priv().asDirectContext();
1911 SkASSERT(directContext);
1912 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1913
Brian Salomon024bd002019-06-11 11:38:16 -04001914 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1915
1916 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001917 auto ii = SkImageInfo::Make(rect.size(), colorType,
1918 this->colorInfo().alphaType(),
1919 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001920 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001921 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1922 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1923 result->addCpuPlane(std::move(data), pm.rowBytes());
1924
Brian Salomon1d435302019-07-01 13:05:28 -04001925 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001926 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001927 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001928 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001929 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001930 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001931 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001932
Brian Salomonab32f652019-05-10 14:24:50 -04001933 struct FinishContext {
1934 ReadPixelsCallback* fClientCallback;
1935 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001937 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001938 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001939 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001940 };
1941 // Assumption is that the caller would like to flush. We could take a parameter or require an
1942 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1943 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001944 auto* finishContext = new FinishContext{callback,
1945 context,
1946 rect.size(),
1947 colorType,
1948 mappedBufferManager,
1949 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001950 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001951 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001952 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001953 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1954 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1955 context->fMappedBufferManager)) {
1956 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001957 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001958 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001959 delete context;
1960 };
1961 GrFlushInfo flushInfo;
1962 flushInfo.fFinishedContext = finishContext;
1963 flushInfo.fFinishedProc = finishCallback;
1964 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1965}
1966
Brian Salomon9241a6d2019-10-03 13:26:54 -04001967void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1968 sk_sp<SkColorSpace> dstColorSpace,
1969 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001970 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001971 RescaleGamma rescaleGamma,
1972 SkFilterQuality rescaleQuality,
1973 ReadPixelsCallback callback,
1974 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001975 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1976 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001977 SkASSERT(!dstSize.isZero());
1978 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1979
Brian Salomon024bd002019-06-11 11:38:16 -04001980 auto direct = fContext->priv().asDirectContext();
1981 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001982 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001983 return;
1984 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001985 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001986 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001987 return;
1988 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001989 if (this->asRenderTargetProxy()->framebufferOnly()) {
1990 callback(context, nullptr);
1991 return;
1992 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001993 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001994 callback(context, nullptr);
1995 return;
1996 }
Brian Salomon024bd002019-06-11 11:38:16 -04001997 int x = srcRect.fLeft;
1998 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001999 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05002000 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04002001 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04002002 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04002003 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04002004 dstColorSpace);
2005 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04002006 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
2007 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002008 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002009 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002010 return;
Brian Salomonab32f652019-05-10 14:24:50 -04002011 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002012 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04002013 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002014 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05002015 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04002016 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05002017 srcView = this->readSurfaceView();
2018 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04002019 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
2020 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
2021 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002022 // If we can't get a texture copy of the contents then give up.
2023 callback(context, nullptr);
2024 return;
2025 }
Brian Salomonc5243782020-04-02 12:50:34 -04002026 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002027 x = y = 0;
2028 }
Brian Salomon4d036892019-07-02 15:10:58 -04002029 // We assume the caller wants kPremul. There is no way to indicate a preference.
2030 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002031 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
2032 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04002033 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002034 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2035 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002036 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2037 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002038 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002039 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002040 return;
2041 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002042 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05002043 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2044 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2045 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2046 SkMatrix::I(), std::move(xform));
2047 srcView = tempRTC->readSurfaceView();
2048 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002049 x = y = 0;
2050 }
2051 }
Greg Danielc594e622019-10-15 14:01:49 -04002052
Greg Daniele20fcad2020-01-08 11:52:34 -05002053 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2054 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2055 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002056 int halfW = dstSize.width()/2;
2057 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002058 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2059 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2060 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2061 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2062 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2063 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002064 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002065 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002066 return;
2067 }
2068
Brian Osman28ba5282019-10-30 14:18:07 -04002069 float baseM[20];
2070 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2071
Brian Salomon024bd002019-06-11 11:38:16 -04002072 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2073
Mike Reed1f607332020-05-21 12:11:27 -04002074 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002075
Brian Salomon9241a6d2019-10-03 13:26:54 -04002076 SkRect dstRectY = SkRect::Make(dstSize);
2077 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002078
Brian Salomon6aa65052020-01-28 12:16:53 -05002079 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2080 PixelTransferResult yTransfer, uTransfer, vTransfer;
2081
Brian Salomon024bd002019-06-11 11:38:16 -04002082 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2083 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002084 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002085 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002086 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002087 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
2088 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
2089 /*unpremulInput=*/false,
2090 /*clampRGBOutput=*/true,
2091 /*premulOutput=*/false);
2092 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002093 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002094 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002095 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002096 if (!doSynchronousRead) {
2097 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2098 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2099 if (!yTransfer.fTransferBuffer) {
2100 callback(context, nullptr);
2101 return;
2102 }
Brian Salomon024bd002019-06-11 11:38:16 -04002103 }
2104
2105 texMatrix.preScale(2.f, 2.f);
2106 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2107 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002108 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002109 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002110 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002111 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2112 GrSamplerState::Filter::kBilerp);
2113 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2114 /*unpremulInput=*/false,
2115 /*clampRGBOutput=*/true,
2116 /*premulOutput=*/false);
2117 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002118 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002119 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002120 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002121 if (!doSynchronousRead) {
2122 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2123 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2124 if (!uTransfer.fTransferBuffer) {
2125 callback(context, nullptr);
2126 return;
2127 }
Brian Salomon024bd002019-06-11 11:38:16 -04002128 }
2129
2130 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2131 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002132 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002133 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002134 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002135 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2136 texMatrix, GrSamplerState::Filter::kBilerp);
2137 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2138 /*unpremulInput=*/false,
2139 /*clampRGBOutput=*/true,
2140 /*premulOutput=*/false);
2141 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002142 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002143 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002144 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002145 if (!doSynchronousRead) {
2146 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2147 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2148 if (!vTransfer.fTransferBuffer) {
2149 callback(context, nullptr);
2150 return;
2151 }
2152 }
2153
2154 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002155 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2156 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2157 size_t yRB = yInfo.minRowBytes();
2158 size_t uvRB = uvInfo.minRowBytes();
2159 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2160 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2161 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2162 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2163 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2164 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002165 callback(context, nullptr);
2166 return;
2167 }
2168 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002169 result->addCpuPlane(std::move(y), yRB );
2170 result->addCpuPlane(std::move(u), uvRB);
2171 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002172 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002173 return;
2174 }
2175
2176 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002177 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002178 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002179 GrClientMappedBufferManager* fMappedBufferManager;
2180 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002181 PixelTransferResult fYTransfer;
2182 PixelTransferResult fUTransfer;
2183 PixelTransferResult fVTransfer;
2184 };
2185 // Assumption is that the caller would like to flush. We could take a parameter or require an
2186 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2187 // callback to GrGpu until after the next flush that flushes our op list, though.
2188 auto* finishContext = new FinishContext{callback,
2189 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002190 direct->priv().clientMappedBufferManager(),
2191 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002192 std::move(yTransfer),
2193 std::move(uTransfer),
2194 std::move(vTransfer)};
2195 auto finishCallback = [](GrGpuFinishedContext c) {
2196 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002197 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002198 auto manager = context->fMappedBufferManager;
2199 size_t rowBytes = SkToSizeT(context->fSize.width());
2200 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2201 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002202 delete context;
2203 return;
2204 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002205 rowBytes /= 2;
2206 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2207 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2208 (*context->fClientCallback)(context->fClientContext, nullptr);
2209 delete context;
2210 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002211 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002212 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2213 (*context->fClientCallback)(context->fClientContext, nullptr);
2214 delete context;
2215 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002216 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002217 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002218 delete context;
2219 };
2220 GrFlushInfo flushInfo;
2221 flushInfo.fFinishedContext = finishContext;
2222 flushInfo.fFinishedProc = finishCallback;
2223 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002224}
2225
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002226GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2227 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002228 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002229 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002230 if (info.fSubmittedProc) {
2231 info.fSubmittedProc(info.fSubmittedContext, false);
2232 }
2233 if (info.fFinishedProc) {
2234 info.fFinishedProc(info.fFinishedContext);
2235 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002236 return GrSemaphoresSubmitted::kNo;
2237 }
robertphillips8c523e02016-07-26 07:41:00 -07002238 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002239 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002240
Greg Daniel55f040b2020-02-13 15:38:32 +00002241 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002242}
2243
Greg Danielc64ee462017-06-15 16:59:49 -04002244bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002245 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002246 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002247 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002248 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002249 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002250
2251 AutoCheckFlush acf(this->drawingManager());
2252
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002253 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002254 return false;
2255 }
2256
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002257 auto direct = fContext->priv().asDirectContext();
2258 if (!direct) {
2259 return false;
2260 }
2261
2262 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002263
Greg Daniel301015c2019-11-18 14:06:46 -05002264 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2265 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002266 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002267 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002268 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2269 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002270 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002271 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2272 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002273 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002274}
joshualitt33a5fce2015-11-18 13:28:51 -08002275
Michael Ludwig7c12e282020-05-29 09:54:07 -04002276void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002277 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002278 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002279 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002280 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002281 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002282 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002283 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002284 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002285 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2286
Michael Ludwig2686d692020-04-17 20:21:37 +00002287 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002288
Robert Phillips27927a52018-08-20 13:18:12 -04002289 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002290}
2291
Michael Ludwig7c12e282020-05-29 09:54:07 -04002292void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002293 GrPaint&& paint,
2294 GrAA aa,
2295 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002296 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002297 ASSERT_SINGLE_OWNER
2298 RETURN_IF_ABANDONED
2299 SkDEBUGCODE(this->validate();)
2300 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2301
Brian Salomon2fad74a2017-12-20 13:28:55 -05002302 if (shape.isEmpty()) {
2303 if (shape.inverseFilled()) {
2304 this->drawPaint(clip, std::move(paint), viewMatrix);
2305 }
2306 return;
robertphillipsea461502015-05-26 11:38:03 -07002307 }
2308
Robert Phillips72152832017-01-25 17:31:35 -05002309 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002310
Brian Salomon2fad74a2017-12-20 13:28:55 -05002311 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002312 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002313 SkRRect rrect;
2314 // We can ignore the starting point and direction since there is no path effect.
2315 bool inverted;
2316 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2317 if (rrect.isRect()) {
2318 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2319 &shape.style());
2320 return;
2321 } else if (rrect.isOval()) {
2322 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002323 return;
2324 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002325 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2326 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002327 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2328 viewMatrix.rectStaysRect()) {
2329 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2330 // the matrix to all the points individually rather than just to the rect
2331 SkRect rects[2];
2332 if (shape.asNestedRects(rects)) {
2333 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002334 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002335 fContext, std::move(paint), viewMatrix, rects);
2336 if (op) {
2337 this->addDrawOp(clip, std::move(op));
2338 }
2339 // Returning here indicates that there is nothing to draw in this case.
2340 return;
2341 }
robertphillipsea461502015-05-26 11:38:03 -07002342 }
2343 }
robertphillips4bc31812016-03-01 12:22:49 -08002344
Michael Ludwig6a6de652020-04-30 20:16:36 -04002345 // If we get here in drawShape(), we definitely need to use path rendering
2346 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2347 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002348}
2349
Michael Ludwig7c12e282020-05-29 09:54:07 -04002350bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002351 const GrUserStencilSettings* ss,
2352 SkRegion::Op op,
2353 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002354 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002355 const SkMatrix& viewMatrix,
2356 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002357 ASSERT_SINGLE_OWNER_PRIV
2358 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002359 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002360 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2361 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002362
2363 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002364 GrPaint paint;
2365 paint.setCoverageSetOpXPFactory(op, invert);
2366 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2367 SkRect::MakeIWH(fRenderTargetContext->width(),
2368 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002369 return true;
2370 }
2371
Robert Phillips72152832017-01-25 17:31:35 -05002372 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002373
2374 // An Assumption here is that path renderer would use some form of tweaking
2375 // the src color (either the input alpha or in the frag shader) to implement
2376 // aa. If we have some future driver-mojo path AA that can do the right
2377 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002378 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002379 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002380
Michael Ludwig7c12e282020-05-29 09:54:07 -04002381 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002382
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002383 GrPaint paint;
2384 paint.setCoverageSetOpXPFactory(op, invert);
2385
Michael Ludwig2686d692020-04-17 20:21:37 +00002386 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002387 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002388 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002389 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002390 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002391 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002392 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002393 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002394 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002395 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2396 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002397 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002398
2399 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002400 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002401 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002402 if (!pr) {
2403 return false;
2404 }
2405
Brian Salomonf3569f02017-10-24 12:52:33 -04002406 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2407 std::move(paint),
2408 ss,
2409 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002410 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002411 &clipConservativeBounds,
2412 &viewMatrix,
2413 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002414 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002415 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002416 pr->drawPath(args);
2417 return true;
2418}
2419
Brian Osman11052242016-10-27 14:47:55 -04002420SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002421 ASSERT_SINGLE_OWNER_PRIV
2422
Robert Phillips6a6de562019-02-15 15:19:15 -05002423 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002424 return SkBudgeted::kNo;
2425 }
2426
Brian Osman11052242016-10-27 14:47:55 -04002427 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002428
Greg Daniel3912a4b2020-01-14 09:56:04 -05002429 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002430}
2431
Michael Ludwig7c12e282020-05-29 09:54:07 -04002432void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002433 GrPaint&& paint,
2434 GrAA aa,
2435 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002436 const GrStyledShape& originalShape,
2437 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002438 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002439 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002440 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2441
Jim Van Verthf86073a2018-10-02 13:05:38 -04002442 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2443 return;
2444 }
2445
Michael Ludwig6a6de652020-04-30 20:16:36 -04002446 if (attemptShapeFallback && originalShape.simplified()) {
2447 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2448 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2449 // we ought to try again instead of going right to path rendering.
2450 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2451 return;
2452 }
2453
Michael Ludwig7c12e282020-05-29 09:54:07 -04002454 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002455
Michael Ludwig2686d692020-04-17 20:21:37 +00002456 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002457 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002458
robertphillips68737822015-10-29 12:12:21 -07002459 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002460 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002461 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002462 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002463 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002464 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002465 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002466 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002467 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002468
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002469 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002470 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002471 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002472 return;
2473 }
2474
Chris Dalton6ce447a2019-06-23 18:07:38 -06002475 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002476
2477 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2478 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2479 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2480
Brian Salomon2fad74a2017-12-20 13:28:55 -05002481 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002482 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002483 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2484 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002485 return;
2486 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002487 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002488 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002489 }
2490 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002491 if (canDrawArgs.fShape->style().applies()) {
2492 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2493 styleScale);
2494 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002495 return;
2496 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002497 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002498 // This time, allow SW renderer
2499 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2500 } else {
2501 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002502#if GR_PATH_RENDERER_SPEW
2503 SkDebugf("falling back to: %s\n", pr->name());
2504#endif
bsalomon6663acf2016-05-10 09:14:17 -07002505 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002506 }
robertphillipsea461502015-05-26 11:38:03 -07002507
bsalomon8acedde2016-06-24 10:42:16 -07002508 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002509#ifdef SK_DEBUG
2510 SkDebugf("Unable to find path renderer compatible with path.\n");
2511#endif
2512 return;
2513 }
2514
Robert Phillips256c37b2017-03-01 14:32:46 -05002515 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002516 std::move(paint),
2517 &GrUserStencilSettings::kUnused,
2518 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002519 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002520 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002521 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002522 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002523 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002524 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002525 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002526}
2527
Brian Salomon467921e2017-03-06 16:17:12 -05002528static void op_bounds(SkRect* bounds, const GrOp* op) {
2529 *bounds = op->bounds();
2530 if (op->hasZeroArea()) {
2531 if (op->hasAABloat()) {
2532 bounds->outset(0.5f, 0.5f);
2533 } else {
2534 // We don't know which way the particular GPU will snap lines or points at integer
2535 // coords. So we ensure that the bounds is large enough for either snap.
2536 SkRect before = *bounds;
2537 bounds->roundOut(bounds);
2538 if (bounds->fLeft == before.fLeft) {
2539 bounds->fLeft -= 1;
2540 }
2541 if (bounds->fTop == before.fTop) {
2542 bounds->fTop -= 1;
2543 }
2544 if (bounds->fRight == before.fRight) {
2545 bounds->fRight += 1;
2546 }
2547 if (bounds->fBottom == before.fBottom) {
2548 bounds->fBottom += 1;
2549 }
2550 }
2551 }
2552}
2553
Chris Dalton08755122019-08-05 16:13:47 -06002554void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002555 GrDrawingManager* drawingMgr = this->drawingManager();
2556 this->getOpsTask()->addOp(drawingMgr,
2557 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06002558}
2559
Michael Ludwig7c12e282020-05-29 09:54:07 -04002560void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002561 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002562 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002563 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002564 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002565 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002566 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002567 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002568 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002569 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002570
Brian Salomon467921e2017-03-06 16:17:12 -05002571 // Setup clip
2572 SkRect bounds;
2573 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002574 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002575 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002576 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002577 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002578
Chris Dalton9acf6822019-11-12 11:52:40 -07002579 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002580 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002581 }
2582
Michael Ludwig7c12e282020-05-29 09:54:07 -04002583 bool skipDraw = false;
2584 if (clip) {
2585 // Have a complex clip, so defer to its early clip culling
2586 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2587 skipDraw = true;
2588 }
2589 } else {
2590 // No clipping, so just clip the bounds against the logical render target dimensions
2591 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2592 skipDraw = true;
2593 }
2594 }
2595
2596 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002597 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002598 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002599 }
2600
Chris Dalton9acf6822019-11-12 11:52:40 -07002601 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2602 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2603
2604 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2605 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2606 // that uses a stencil test when the stencil buffer is multisampled.)
2607 bool hasMixedSampledCoverage = (
2608 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002609 SkASSERT(!hasMixedSampledCoverage ||
2610 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002611
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002612 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002613 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002614 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2615
Michael Ludwig28e5f112020-06-09 10:57:24 -04002616 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2617 op->setClippedBounds(bounds);
2618
Greg Daniel524e28b2019-11-01 11:48:53 -04002619 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002620 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002621 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002622 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002623 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002624 }
2625 }
2626
Greg Danielf41b2bd2019-08-22 16:19:24 -04002627 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002628 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002629 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002630 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002631 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
2632 dstProxyView,GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002633}
2634
Michael Ludwig28e5f112020-06-09 10:57:24 -04002635bool GrRenderTargetContext::setupDstProxyView(const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002636 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002637 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2638 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2639 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002640 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002641 return false;
2642 }
2643
Greg Daniel3912a4b2020-01-14 09:56:04 -05002644 if (this->caps()->textureBarrierSupport() &&
2645 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002646 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002647 // The render target is a texture, so we can read from it directly in the shader. The XP
2648 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002649 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002650 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002651 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002652 }
2653 }
2654
Greg Daniel3155f7f2020-01-16 16:54:26 -05002655 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002656 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2657 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002658 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002659 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002660
Michael Ludwig28e5f112020-06-09 10:57:24 -04002661 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002662 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002663 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2664 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2665 // centers during rasterization.
2666 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2667 conservativeDrawBounds.outset(1, 1);
2668 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002669 }
Brian Salomon467921e2017-03-06 16:17:12 -05002670
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002671 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002672 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002673 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002674 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002675 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002676 } else {
Eric Karl74480882017-04-03 14:49:05 -07002677 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002678 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002679 }
Brian Salomonc5243782020-04-02 12:50:34 -04002680 auto copy =
2681 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2682 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2683 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002684
Brian Salomonc5243782020-04-02 12:50:34 -04002685 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002686 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002687 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002688}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002689
Greg Daniel573312e2020-02-07 17:22:35 -05002690bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002691 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002692 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002693 SkIRect clippedSrcRect;
2694 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002695 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2696 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002697 return false;
2698 }
2699
2700 GrPaint paint;
2701 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002702
Greg Danield2ccbb52020-02-05 10:45:39 -05002703 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002704 if (!fp) {
2705 return false;
2706 }
2707 paint.addColorFragmentProcessor(std::move(fp));
2708
2709 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002710 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002711 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2712 clippedSrcRect.height()),
2713 SkRect::Make(clippedSrcRect));
2714 return true;
2715}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002716
2717void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2718 SkASSERT(&task == fOpsTask.get());
2719 fOpsTask.reset();
2720}