blob: b4617c8ba3d1fd178e317880ba4bf51f502850d3 [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 Derbya08bde62020-06-12 15:46:06 -040073#include "src/gpu/text/GrSDFTOptions.h"
Herb Derby64688222020-06-08 14:19:57 -040074#include "src/gpu/text/GrTextBlobCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050075#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,
Robert Phillipsf0754142020-06-24 15:38:26 -0400295 sk_sp<GrRefCntedCallback> releaseHelper) {
Greg Danielba0ff782020-01-07 15:42:57 -0500296 SkASSERT(sampleCnt > 0);
297 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillipsf0754142020-06-24 15:38:26 -0400298 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
299 std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500300 if (!proxy) {
301 return nullptr;
302 }
303
304 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
305 origin, surfaceProps);
306}
307
308std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
309 GrRecordingContext* context,
310 GrColorType colorType,
311 sk_sp<SkColorSpace> colorSpace,
312 const GrBackendTexture& tex,
313 int sampleCnt,
314 GrSurfaceOrigin origin,
315 const SkSurfaceProps* surfaceProps) {
316 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400317 sk_sp<GrSurfaceProxy> proxy(
318 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500319 if (!proxy) {
320 return nullptr;
321 }
322
323 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
324 origin, surfaceProps);
325}
326
327std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
328 GrRecordingContext* context,
329 GrColorType colorType,
330 sk_sp<SkColorSpace> colorSpace,
331 const GrBackendRenderTarget& rt,
332 GrSurfaceOrigin origin,
333 const SkSurfaceProps* surfaceProps,
334 ReleaseProc releaseProc,
335 ReleaseContext releaseCtx) {
Robert Phillipsf0754142020-06-24 15:38:26 -0400336 sk_sp<GrRefCntedCallback> releaseHelper;
337 if (releaseProc) {
338 releaseHelper.reset(new GrRefCntedCallback(releaseProc, releaseCtx));
339 }
340
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400341 sk_sp<GrSurfaceProxy> proxy(
Robert Phillipsf0754142020-06-24 15:38:26 -0400342 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
Greg Danielba0ff782020-01-07 15:42:57 -0500343 if (!proxy) {
344 return nullptr;
345 }
346
347 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
348 origin, surfaceProps);
349}
350
351std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
352 GrRecordingContext* context,
353 const SkImageInfo& imageInfo,
354 const GrVkDrawableInfo& vkInfo,
355 const SkSurfaceProps* props) {
356 sk_sp<GrSurfaceProxy> proxy(
357 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
358 vkInfo));
359 if (!proxy) {
360 return nullptr;
361 }
362
Greg Danielba0ff782020-01-07 15:42:57 -0500363 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
364 imageInfo.refColorSpace(), std::move(proxy),
365 kTopLeft_GrSurfaceOrigin, props);
366}
367
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
369// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
370// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
371// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500372GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500373 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400374 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400375 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400376 sk_sp<SkColorSpace> colorSpace,
377 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400378 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500379 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
380 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400381 , fWriteView(std::move(writeView))
Brian Salomonf3569f02017-10-24 12:52:33 -0400382 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400383 , fManagedOpsTask(managedOpsTask)
384 , fGlyphPainter(*this) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400385 fOpsTask = sk_ref_sp(context->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
Brian Salomon3b8486a2020-04-21 12:43:26 -0400386 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400387 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400388 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400389 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
390 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500391
Brian Salomonf18b1d82017-10-27 11:30:49 -0400392 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700393 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700394}
395
robertphillips2e1e51f2015-10-15 08:01:48 -0700396#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500397void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400398 if (fOpsTask && !fOpsTask->isClosed()) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400399 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700400 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700401}
402#endif
403
Brian Osman11052242016-10-27 14:47:55 -0400404GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800405 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400406 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400407 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400408 }
robertphillipsa106c622015-10-16 09:07:06 -0700409}
410
Chris Dalton7d6748e2019-03-13 00:34:52 -0600411inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600412 if (GrAA::kNo == aa) {
413 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
414 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600415 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600416 return GrAAType::kMSAA;
417 }
418 return GrAAType::kNone;
419 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600420 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600421}
422
Greg Daniele252f082017-10-23 16:05:23 -0400423GrMipMapped GrRenderTargetContext::mipMapped() const {
424 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
425 return proxy->mipMapped();
426 }
427 return GrMipMapped::kNo;
428}
429
Greg Danielf41b2bd2019-08-22 16:19:24 -0400430GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800431 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700432 SkDEBUGCODE(this->validate();)
433
Brian Salomon3b8486a2020-04-21 12:43:26 -0400434 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600435 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400436 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600437 if (fOpsTask && fNumStencilSamples > 0) {
438 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600439 fOpsTask->setMustPreserveStencil();
440 // Reload the stencil buffer content at the beginning of newOpsTask.
441 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
442 // values?
443 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
444 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400445 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600446 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700447 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400448 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400449 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700450}
451
Herb Derby64688222020-06-08 14:19:57 -0400452static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
453 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
454 if (lcd) {
455 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
456 // can be modified. For now we just regenerate if any run in a textblob has LCD.
457 // TODO figure out where all of these modifications are and see if we can incorporate that
458 // logic at a higher level *OR* use sRGB
459 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
460
461 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
462 // see the note above. We pick a dummy value for LCD text to ensure we always match the
463 // same key
464 return SK_ColorTRANSPARENT;
465 } else {
466 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
467 // gamma corrected masks anyways, nor color
468 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
469 SkColorGetG(canonicalColor),
470 SkColorGetB(canonicalColor));
471 // reduce to our finite number of bits
472 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
473 }
474 return canonicalColor;
475}
476
Michael Ludwig7c12e282020-05-29 09:54:07 -0400477void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400478 const SkMatrixProvider& matrixProvider,
Herb Derby64688222020-06-08 14:19:57 -0400479 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800480 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700481 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700482 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400483 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700484
Greg Danielbe7fc462019-01-03 16:40:42 -0500485 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
486 // secondary command buffers because it would require stopping and starting a render pass which
487 // we don't have access to.
488 if (this->wrapsVkSecondaryCB()) {
489 return;
490 }
491
Herb Derbya08bde62020-06-12 15:46:06 -0400492 GrSDFTOptions options = fContext->priv().SDFTOptions();
Herb Derby64688222020-06-08 14:19:57 -0400493 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
494
495 // Get the first paint to use as the key paint.
496 const SkPaint& blobPaint = glyphRunList.paint();
497
498 SkPoint drawOrigin = glyphRunList.origin();
499
500 SkMaskFilterBase::BlurRec blurRec;
501 // It might be worth caching these things, but its not clear at this time
502 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
503 const SkMaskFilter* mf = blobPaint.getMaskFilter();
504 bool canCache = glyphRunList.canCache() &&
505 !(blobPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
506
507 // If we're doing linear blending, then we can disable the gamma hacks.
508 // Otherwise, leave them on. In either case, we still want the contrast boost:
509 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
510 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
511 ? SkScalerContextFlags::kBoostContrast
512 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
513
514 sk_sp<GrTextBlob> blob;
515 GrTextBlob::Key key;
516 if (canCache) {
517 bool hasLCD = glyphRunList.anyRunsLCD();
518
519 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
520 SkPixelGeometry pixelGeometry =
521 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
522
523 GrColor canonicalColor = compute_canonical_color(blobPaint, hasLCD);
524
525 key.fPixelGeometry = pixelGeometry;
526 key.fUniqueID = glyphRunList.uniqueID();
527 key.fStyle = blobPaint.getStyle();
528 key.fHasBlur = SkToBool(mf);
529 key.fCanonicalColor = canonicalColor;
530 key.fScalerContextFlags = scalerContextFlags;
531 blob = textBlobCache->find(key);
532 }
533
534 const SkMatrix& drawMatrix(matrixProvider.localToDevice());
535 if (blob != nullptr && blob->canReuse(blobPaint, blurRec, drawMatrix, drawOrigin)) {
536 // Reusing the blob. Move it to the front of LRU cache.
537 textBlobCache->makeMRU(blob.get());
538 } else {
539 // Build or Rebuild the GrTextBlob
540 if (blob != nullptr) {
541 // We have to remake the blob because changes may invalidate our masks.
542 // TODO we could probably get away with reuse most of the time if the pointer is unique,
543 // but we'd have to clear the SubRun information
544 textBlobCache->remove(blob.get());
545 }
546 if (canCache) {
547 blob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, drawMatrix);
548 } else {
549 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
550 }
551 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
552 fGlyphPainter.processGlyphRunList(
553 glyphRunList, drawMatrix, fSurfaceProps, supportsSDFT, options, blob.get());
554 }
555
556 blob->insertOpsIntoTarget(
557 fTextTarget.get(), fSurfaceProps, blobPaint, clip, matrixProvider, drawOrigin);
robertphillipsea461502015-05-26 11:38:03 -0700558}
559
Brian Osman11052242016-10-27 14:47:55 -0400560void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800561 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700562 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700563 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400564 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700565
Robert Phillips72152832017-01-25 17:31:35 -0500566 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400567
Greg Danielf41b2bd2019-08-22 16:19:24 -0400568 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700569}
570
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500571static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
572 paint->setColor4f(color);
573 if (color.isOpaque()) {
574 // Can just rely on the src-over blend mode to do the right thing
575 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
576 } else {
577 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
578 // were src blended
579 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
580 }
581}
582
Michael Ludwig81d41722020-05-26 16:57:38 -0400583// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
584// premul alpha type. If we ever support different alpha type render targets, this function should
585// transform the color as appropriate.
586void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500587 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400588 bool upgradePartialToFull) {
589 ASSERT_SINGLE_OWNER
590 RETURN_IF_ABANDONED
591 SkDEBUGCODE(this->validate();)
592 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
593
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400594 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
595 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
596 // supports then. Drawing an axis-aligned rect is the fallback path.
597 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
598 if (scissor && !scissorState.set(*scissor)) {
599 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
600 // except clear ops are not draw ops).
601 return;
602 }
603
604 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
605 // the test. We only do this when the clear would be handled by a load op or natively.
606 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
607 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
608 this->caps()->shouldInitializeTextures())) {
609 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
610 // only clear the entire target if we knew it had not been cleared before. As
611 // is this could end up doing a lot of redundant clears.
612 scissorState.setDisabled();
613 } else {
614 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
615 // render target to overflow into any approx-fit padding of the backing store dimensions
616 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400617 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500618 }
619
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400620 if (!scissorState.enabled()) {
621 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
622 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400623 GrOpsTask* opsTask = this->getOpsTask();
624 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500625 !this->caps()->performColorClearsAsDraws()) {
626 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400627 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500628 return;
629 } else {
630 // Will use an op for the clear, reset the load op to discard since the op will
631 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400632 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500633 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400634 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500635
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400636 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
637 // as a load op.
638 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
639 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
640 if (clearAsDraw) {
641 GrPaint paint;
642 clear_to_grpaint(color, &paint);
643 this->addDrawOp(nullptr,
644 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
645 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500646 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400647 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500648 }
649}
650
Michael Ludwig7c12e282020-05-29 09:54:07 -0400651void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500652 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400653 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000654 // Start with the render target, since that is the maximum content we could possibly fill.
655 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500656 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400657 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000658 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
659 // inverse view matrix.
660 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
661 } else {
662 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
663 SkMatrix localMatrix;
664 if (!viewMatrix.invert(&localMatrix)) {
665 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400666 }
Michael Ludwig61328202019-06-19 14:48:58 +0000667 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
668 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700669 }
robertphillipsea461502015-05-26 11:38:03 -0700670}
671
Michael Ludwig61328202019-06-19 14:48:58 +0000672enum class GrRenderTargetContext::QuadOptimization {
673 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
674 kDiscarded,
675 // The rect to draw was converted to some other op and appended to the oplist, so no additional
676 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
677 // a constColor is provided.
678 kSubmitted,
679 // The clip was folded into the device quad, with updated edge flags and local coords, and
680 // caller is responsible for adding an appropriate op.
681 kClipApplied,
682 // No change to clip, but quad updated to better fit clip/render target, and caller is
683 // responsible for adding an appropriate op.
684 kCropped
685};
Michael Ludwig61a16512019-01-15 11:15:13 -0500686
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000687static bool make_vertex_finite(float* value) {
688 if (SkScalarIsNaN(*value)) {
689 return false;
690 }
691
692 if (!SkScalarIsFinite(*value)) {
693 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
694 // left when calculating crops.
695 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
696 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
697 }
698
699 return true;
700}
701
702static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
703 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
704}
705
Michael Ludwig61328202019-06-19 14:48:58 +0000706GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400707 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500708 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000709 // Optimization requirements:
710 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500711 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
712 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
713 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
714 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
715 // 5. kCropped in all other scenarios (although a crop may be a no-op)
716
717 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
718 // better to just keep the old flags instead of introducing mixed edge flags.
719 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000720
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400721 // Use the logical size of the render target, which allows for "fullscreen" clears even if
722 // the render target has an approximate backing fit
723 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400724
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500725 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000726 if (constColor) {
727 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
728 // will be cropped to the finite 'clip' or render target and there is no local space mapping
729 if (!quad->fDevice.isFinite()) {
730 for (int i = 0; i < 4; ++i) {
731 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
732 !make_vertex_finite(quad->fDevice.ys() + i) ||
733 !make_vertex_finite(quad->fDevice.ws() + i)) {
734 // Discard if we see a nan
735 return QuadOptimization::kDiscarded;
Michael Ludwig97164142020-06-24 10:33:43 -0400736 }
Michael Ludwig61328202019-06-19 14:48:58 +0000737 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000738 SkASSERT(quad->fDevice.isFinite());
Michael Ludwig97164142020-06-24 10:33:43 -0400739 }
740 } else {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000741 // CropToRect requires the quads to be finite. If they are not finite and we have local
742 // coordinates, the mapping from local space to device space is poorly defined so drop it
743 if (!quad->fDevice.isFinite()) {
744 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000745 }
Brian Salomon7694b902019-06-18 21:00:21 +0000746 }
747
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000748 // If the quad is entirely off screen, it doesn't matter what the clip does
749 if (!rtRect.intersects(drawBounds)) {
750 return QuadOptimization::kDiscarded;
751 }
752
753 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
754 // the render target).
755 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
756 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
757 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
758 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
759 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
760 bool axisAlignedClip = true;
761 if (clip && !clip->quickContains(rtRect)) {
762 if (!clip->isRRect(&clipRRect, &clipAA)) {
763 axisAlignedClip = false;
764 }
765 }
766
767 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
768 // draw geometry so that no clip is needed when drawing.
769 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
770 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
771 // of the render target and the clip rect)
772 SkRect clipBounds = rtRect;
773 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
774 return QuadOptimization::kDiscarded;
775 }
776
777 if (clipRRect.isRect()) {
778 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
779 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
780 if (!stencilSettings && constColor &&
781 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
782 // Clear optimization is possible
783 drawBounds = quad->fDevice.bounds();
784 if (drawBounds.contains(rtRect)) {
785 // Fullscreen clear
786 this->clear(*constColor);
787 return QuadOptimization::kSubmitted;
788 } else if (GrClip::IsPixelAligned(drawBounds) &&
789 drawBounds.width() > 256 && drawBounds.height() > 256) {
790 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
791 SkIRect scissorRect;
792 drawBounds.round(&scissorRect);
793 this->clear(scissorRect, *constColor);
794 return QuadOptimization::kSubmitted;
795 }
796 }
797
798 // Update overall AA setting.
799 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
800 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
801 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
802 // properly reflect the smooth edge of the clip.
803 *aa = GrAA::kYes;
804 }
805 // We intentionally do not downgrade AA here because we don't know if we need to
806 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
807 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
808
809 // deviceQuad is exactly the intersection of original quad and clip, so it can be
810 // drawn with no clip (submitted by caller)
811 return QuadOptimization::kClipApplied;
812 } else {
813 // The quads have been updated to better fit the clip bounds, but can't get rid of
814 // the clip entirely
815 quad->fEdgeFlags = oldFlags;
816 return QuadOptimization::kCropped;
817 }
818 } else if (!stencilSettings && constColor) {
819 // Rounded corners and constant filled color (limit ourselves to solid colors because
820 // there is no way to use custom local coordinates with drawRRect).
821 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
822 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
823 quad->fDevice.bounds().contains(clipBounds)) {
824 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
825 // we can draw the rrect directly and ignore the edge flags
826 GrPaint paint;
827 clear_to_grpaint(*constColor, &paint);
828 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
829 clipRRect, GrStyle::SimpleFill());
830 return QuadOptimization::kSubmitted;
831 } else {
832 // The quad has been updated to better fit clip bounds, but can't remove the clip
833 quad->fEdgeFlags = oldFlags;
834 return QuadOptimization::kCropped;
835 }
836 }
837 }
838
839 // Crop the quad to the conservative bounds of the clip.
840 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
841
842 // One final check for discarding, since we may have gone here directly due to a complex clip
843 if (!clipBounds.intersects(drawBounds)) {
844 return QuadOptimization::kDiscarded;
845 }
846
847 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
848 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
849 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500850 quad->fEdgeFlags = oldFlags;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000851
Michael Ludwig61328202019-06-19 14:48:58 +0000852 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000853}
854
Michael Ludwig7c12e282020-05-29 09:54:07 -0400855void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500856 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500857 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500858 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400859 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000860 ASSERT_SINGLE_OWNER
861 RETURN_IF_ABANDONED
862 SkDEBUGCODE(this->validate();)
863 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500864
Michael Ludwig61328202019-06-19 14:48:58 +0000865 AutoCheckFlush acf(this->drawingManager());
866
867 SkPMColor4f* constColor = nullptr;
868 SkPMColor4f paintColor;
869 if (!ss && !paint.numCoverageFragmentProcessors() &&
870 paint.isConstantBlendedColor(&paintColor)) {
871 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
872 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500873 }
874
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500875 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000876 if (opt >= QuadOptimization::kClipApplied) {
877 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400878 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000879 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
880 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500881 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
882 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700883 }
Michael Ludwig61328202019-06-19 14:48:58 +0000884 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800885}
886
Michael Ludwig7c12e282020-05-29 09:54:07 -0400887void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400888 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500889 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000890 sk_sp<GrColorSpaceXform> textureXform,
891 GrSamplerState::Filter filter,
892 const SkPMColor4f& color,
893 SkBlendMode blendMode,
894 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500895 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400896 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000897 ASSERT_SINGLE_OWNER
898 RETURN_IF_ABANDONED
899 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400900 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000901 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
902
903 AutoCheckFlush acf(this->drawingManager());
904
905 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
906 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500907 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000908
909 SkASSERT(opt != QuadOptimization::kSubmitted);
910 if (opt != QuadOptimization::kDiscarded) {
911 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400912 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000913 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400914 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400915 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
916 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400917 // Use the provided subset, although hypothetically we could detect that the cropped local
918 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500919 this->addDrawOp(finalClip,
920 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
921 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400922 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000923 }
924}
925
Michael Ludwig7c12e282020-05-29 09:54:07 -0400926void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500927 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500928 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400929 const SkMatrix& viewMatrix,
930 const SkRect& rect,
931 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700932 if (!style) {
933 style = &GrStyle::SimpleFill();
934 }
joshualitt1de610a2016-01-06 08:26:09 -0800935 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700936 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700937 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400938 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700939
bsalomon6663acf2016-05-10 09:14:17 -0700940 // Path effects should've been devolved to a path in SkGpuDevice
941 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700942
Robert Phillips72152832017-01-25 17:31:35 -0500943 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700944
bsalomon6663acf2016-05-10 09:14:17 -0700945 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400946 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000947 // Fills the rect, using rect as its own local coordinates
948 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500949 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400950 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
951 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
952 (rect.width() && rect.height())) {
953 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
954 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400955 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700956
Chris Dalton7d6748e2019-03-13 00:34:52 -0600957 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500958 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
959 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
960 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500961 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400962 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700963 return;
robertphillips4bc31812016-03-01 12:22:49 -0800964 }
robertphillips4bc31812016-03-01 12:22:49 -0800965 }
Mike Klein16885072018-12-11 09:54:31 -0500966 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000967 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
968 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700969}
970
Michael Ludwig7c12e282020-05-29 09:54:07 -0400971void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500972 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
973 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600974 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500975
976 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
977 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500978}
979
Robert Phillipsec2249f2016-11-09 08:54:35 -0500980int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500981 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400982 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500983}
984
Greg Danielf41b2bd2019-08-22 16:19:24 -0400985GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600986 ) const {
987#if GR_TEST_UTILS
988 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400989 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600990 }
991#endif
992 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
993 // would normally be overwritten. The one exception is if the render target context is marked as
994 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
995 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
996 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
997 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400998 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600999}
1000
Chris Dalton858cf232019-10-14 16:20:00 -06001001void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -06001002 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -06001003 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -06001004 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -06001005
Chris Daltoneffee202019-07-01 22:28:03 -06001006 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -06001007 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -05001008 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -06001009 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -04001010 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -06001011 }
1012 SkASSERT(numRequiredSamples > 0);
1013
1014 if (numRequiredSamples > fNumStencilSamples) {
1015 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -05001016 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -06001017 }
Chris Dalton6b982802019-06-27 13:53:46 -06001018
Chris Dalton674f77a2019-09-30 20:49:39 -06001019 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -06001020 if (this->caps()->performStencilClearsAsDraws()) {
1021 // There is a driver bug with clearing stencil. We must use an op to manually clear the
1022 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -04001023 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -06001024 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -06001025 this->getOpsTask()->setInitialStencilContent(
1026 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -06001027 }
1028 }
1029}
1030
Michael Ludwig81d41722020-05-26 16:57:38 -04001031void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001032 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1033
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001034 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
1035 if (scissor && !scissorState.set(*scissor)) {
1036 // The requested clear region is off screen, so nothing to do.
1037 return;
1038 }
1039
Michael Ludwig1e632792020-05-21 12:45:31 -04001040 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001041 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -04001042 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001043 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001044
1045 // Configure the paint to have no impact on the color buffer
1046 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001047 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001048 this->addDrawOp(nullptr,
1049 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1050 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001051 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -04001052 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -05001053 }
robertphillips976f5f02016-06-03 10:59:20 -07001054}
1055
Michael Ludwig7c12e282020-05-29 09:54:07 -04001056void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001057 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001058 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001059 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001060 ASSERT_SINGLE_OWNER_PRIV
1061 RETURN_IF_ABANDONED_PRIV
1062 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001063 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1064 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001065
Brian Salomon467921e2017-03-06 16:17:12 -05001066 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1067 SkASSERT(path);
1068 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1069
1070 // FIXME: Use path bounds instead of this WAR once
1071 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1072 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1073
Michael Ludwig7c12e282020-05-29 09:54:07 -04001074 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
1075 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
1076 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001077 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
1078 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
1079
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001080 if (clip && !clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001081 return;
1082 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001083 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1084 // but as it is, we're just using the full render target so intersecting the two bounds would
1085 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001086
Robert Phillips7c525e62018-06-12 10:11:12 -04001087 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1088 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001089 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001090 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001091 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001092 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001093 if (!op) {
1094 return;
1095 }
Brian Salomon97180af2017-03-14 13:42:58 -04001096 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001097
Chris Daltoneffee202019-07-01 22:28:03 -06001098 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001099 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001100}
1101
Michael Ludwig7c12e282020-05-29 09:54:07 -04001102void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001103 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001104 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001105 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1106 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001107 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001108 ASSERT_SINGLE_OWNER
1109 RETURN_IF_ABANDONED
1110 SkDEBUGCODE(this->validate();)
1111 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001112
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001113 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1114 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1115 AutoCheckFlush acf(this->drawingManager());
1116 GrAAType aaType = this->chooseAAType(aa);
1117 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1118 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1119 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001120 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1121 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001122}
1123
Michael Ludwig7c12e282020-05-29 09:54:07 -04001124void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001125 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001126 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001127 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001128 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001129 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001130 ASSERT_SINGLE_OWNER
1131 RETURN_IF_ABANDONED
1132 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001133 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001134
1135 AutoCheckFlush acf(this->drawingManager());
1136
1137 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001138 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001139 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001140 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001141 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001142 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001143 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001144}
1145
1146///////////////////////////////////////////////////////////////////////////////
1147
Michael Ludwig7c12e282020-05-29 09:54:07 -04001148void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001149 GrPaint&& paint,
1150 const SkMatrix& viewMatrix,
1151 int spriteCount,
1152 const SkRSXform xform[],
1153 const SkRect texRect[],
1154 const SkColor colors[]) {
1155 ASSERT_SINGLE_OWNER
1156 RETURN_IF_ABANDONED
1157 SkDEBUGCODE(this->validate();)
1158 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1159
1160 AutoCheckFlush acf(this->drawingManager());
1161
1162 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1163 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1164 aaType, spriteCount, xform, texRect, colors);
1165 this->addDrawOp(clip, std::move(op));
1166}
1167
1168///////////////////////////////////////////////////////////////////////////////
1169
Michael Ludwig7c12e282020-05-29 09:54:07 -04001170void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001171 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001172 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001173 const SkMatrix& viewMatrix,
1174 const SkRRect& rrect,
1175 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001176 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001177 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001178 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001179 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001180
1181 const SkStrokeRec& stroke = style.strokeRec();
1182 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001183 return;
1184 }
1185
Michael Ludwig7c12e282020-05-29 09:54:07 -04001186 const GrClip* clip = origClip;
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001187#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1188 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
1189 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
1190 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1191 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1192 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1193 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001194 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001195 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001196 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1197 clip = nullptr;
Michael Ludwig97164142020-06-24 10:33:43 -04001198 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001199#endif
1200 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001201
Robert Phillips72152832017-01-25 17:31:35 -05001202 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001203
Chris Dalton7d6748e2019-03-13 00:34:52 -06001204 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001205
Chris Dalton0dffbab2019-03-27 13:08:50 -06001206 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001207 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1208 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1209 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1210 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1211 // to avoid perf regressions on some platforms.
1212 assert_alive(paint);
1213 op = GrOvalOpFactory::MakeCircularRRectOp(
1214 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1215 }
1216 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001217 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001218 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001219 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001220 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001221 assert_alive(paint);
1222 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001223 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001224 }
1225 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001226 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001227 return;
robertphillipsea461502015-05-26 11:38:03 -07001228 }
robertphillipsb56f9272016-02-25 11:03:52 -08001229
Mike Klein16885072018-12-11 09:54:31 -05001230 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001231 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001232 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001233}
1234
Jim Van Verthc5903412016-11-17 15:27:09 -05001235///////////////////////////////////////////////////////////////////////////////
1236
Jim Van Verth3af1af92017-05-18 15:06:54 -04001237static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1238 SkPoint3 result;
1239 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1240 result.fZ = pt.fZ;
1241 return result;
1242}
1243
Michael Ludwig7c12e282020-05-29 09:54:07 -04001244bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001245 const SkMatrix& viewMatrix,
1246 const SkPath& path,
1247 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001248 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001249 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001250 return true;
1251 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001252 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001253 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254
1255 // check z plane
1256 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1257 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1258 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1259 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1260 return false;
1261 }
1262
1263 SkRRect rrect;
1264 SkRect rect;
1265 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001266 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001267 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1268 if (!isRRect &&
1269 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1270 rect.width() > SK_ScalarNearlyZero) {
1271 rrect.setOval(rect);
1272 isRRect = true;
1273 }
1274 if (!isRRect && path.isRect(&rect)) {
1275 rrect.setRect(rect);
1276 isRRect = true;
1277 }
1278
1279 if (!isRRect) {
1280 return false;
1281 }
1282
Jim Van Verthc5903412016-11-17 15:27:09 -05001283 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001284 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001285 }
1286
Robert Phillips72152832017-01-25 17:31:35 -05001287 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001288
Jim Van Verth3af1af92017-05-18 15:06:54 -04001289 // transform light
1290 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1291
1292 // 1/scale
1293 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001294 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001295 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1296 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1297
1298 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001299 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1300
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001301 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001302 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1303 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1304 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001305
1306 // Outset the shadow rrect to the border of the penumbra
1307 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1308 SkRRect ambientRRect;
1309 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1310 // If the rrect was an oval then its outset will also be one.
1311 // We set it explicitly to avoid errors.
1312 if (rrect.isOval()) {
1313 ambientRRect = SkRRect::MakeOval(outsetRect);
1314 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001315 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001316 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1317 }
1318
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001319 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001320 if (transparent) {
1321 // set a large inset to force a fill
1322 devSpaceInsetWidth = ambientRRect.width();
1323 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001324
Robert Phillips7c525e62018-06-12 10:11:12 -04001325 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1326 ambientColor,
1327 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001328 ambientRRect,
1329 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001330 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001331 if (op) {
1332 this->addDrawOp(clip, std::move(op));
1333 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001334 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001335
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001336 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001337 SkScalar devSpaceSpotBlur;
1338 SkScalar spotScale;
1339 SkVector spotOffset;
1340 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1341 devLightPos.fZ, rec.fLightRadius,
1342 &devSpaceSpotBlur, &spotScale, &spotOffset);
1343 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001344 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1345
Jim Van Verth3af1af92017-05-18 15:06:54 -04001346 // Adjust translate for the effect of the scale.
1347 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1348 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1349 // This offset is in dev space, need to transform it into source space.
1350 SkMatrix ctmInverse;
1351 if (viewMatrix.invert(&ctmInverse)) {
1352 ctmInverse.mapPoints(&spotOffset, 1);
1353 } else {
1354 // Since the matrix is a similarity, this should never happen, but just in case...
1355 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1356 SkASSERT(false);
1357 }
1358
1359 // Compute the transformed shadow rrect
1360 SkRRect spotShadowRRect;
1361 SkMatrix shadowTransform;
1362 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1363 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001364 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001365
1366 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001367 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001368 SkScalar insetWidth = blurOutset;
1369 if (transparent) {
1370 // If transparent, just do a fill
1371 insetWidth += spotShadowRRect.width();
1372 } else {
1373 // For shadows, instead of using a stroke we specify an inset from the penumbra
1374 // border. We want to extend this inset area so that it meets up with the caster
1375 // geometry. The inset geometry will by default already be inset by the blur width.
1376 //
1377 // We compare the min and max corners inset by the radius between the original
1378 // rrect and the shadow rrect. The distance between the two plus the difference
1379 // between the scaled radius and the original radius gives the distance from the
1380 // transformed shadow shape to the original shape in that corner. The max
1381 // of these gives the maximum distance we need to cover.
1382 //
1383 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1384 // that to get the full insetWidth.
1385 SkScalar maxOffset;
1386 if (rrect.isRect()) {
1387 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001388 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001389 rrect.rect().fLeft),
1390 SkTAbs(spotShadowRRect.rect().fTop -
1391 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001392 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001393 rrect.rect().fRight),
1394 SkTAbs(spotShadowRRect.rect().fBottom -
1395 rrect.rect().fBottom)));
1396 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001397 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001398 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1399 rrect.rect().fLeft + dr,
1400 spotShadowRRect.rect().fTop -
1401 rrect.rect().fTop + dr);
1402 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1403 rrect.rect().fRight - dr,
1404 spotShadowRRect.rect().fBottom -
1405 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001406 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001407 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001408 }
Brian Osman788b9162020-02-07 10:36:46 -05001409 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001410 }
1411
1412 // Outset the shadow rrect to the border of the penumbra
1413 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1414 if (spotShadowRRect.isOval()) {
1415 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1416 } else {
1417 SkScalar outsetRad = spotRadius + blurOutset;
1418 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1419 }
1420
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001421 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001422
Robert Phillips7c525e62018-06-12 10:11:12 -04001423 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1424 spotColor,
1425 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001426 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001427 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001428 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001429 if (op) {
1430 this->addDrawOp(clip, std::move(op));
1431 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001432 }
1433
1434 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001435}
1436
1437///////////////////////////////////////////////////////////////////////////////
1438
Michael Ludwig7c12e282020-05-29 09:54:07 -04001439bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001440 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001441 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001442 const SkMatrix& viewMatrix,
1443 const SkRRect& origOuter,
1444 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001445 SkASSERT(!origInner.isEmpty());
1446 SkASSERT(!origOuter.isEmpty());
1447
Brian Salomon65749212017-12-01 16:01:47 -05001448 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1449
Chris Dalton7d6748e2019-03-13 00:34:52 -06001450 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001451
1452 if (GrAAType::kMSAA == aaType) {
1453 return false;
1454 }
1455
Greg Daniel2655ede2019-04-10 00:49:28 +00001456 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1457 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001458 auto outerR = outer->width() / 2.f;
1459 auto innerR = inner->width() / 2.f;
1460 auto cx = outer->getBounds().fLeft + outerR;
1461 auto cy = outer->getBounds().fTop + outerR;
1462 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1463 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1464 auto avgR = (innerR + outerR) / 2.f;
1465 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1466 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1467 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001468 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001469 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001470 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001471 if (op) {
1472 this->addDrawOp(clip, std::move(op));
1473 return true;
1474 }
Mike Klein16885072018-12-11 09:54:31 -05001475 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001476 }
1477 }
1478
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001479 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001480 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001481 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1482 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001483 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001484 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1485 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001486 }
robertphillips00095892016-02-29 13:50:40 -08001487
robertphillips00095892016-02-29 13:50:40 -08001488 SkMatrix inverseVM;
1489 if (!viewMatrix.isIdentity()) {
1490 if (!origInner.transform(viewMatrix, inner.writable())) {
1491 return false;
1492 }
1493 if (!origOuter.transform(viewMatrix, outer.writable())) {
1494 return false;
1495 }
1496 if (!viewMatrix.invert(&inverseVM)) {
1497 return false;
1498 }
1499 } else {
1500 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001501 }
robertphillips00095892016-02-29 13:50:40 -08001502
Ethan Nicholaseace9352018-10-15 20:09:54 +00001503 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001504 // TODO these need to be a geometry processors
John Stiles851b90e2020-06-17 13:53:37 -04001505 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, innerEdgeType, *inner, caps);
1506 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001507 return false;
1508 }
1509
John Stiles851b90e2020-06-17 13:53:37 -04001510 std::tie(success, fp) = GrRRectEffect::Make(std::move(fp), outerEdgeType, *outer, caps);
1511 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001512 return false;
1513 }
1514
John Stiles851b90e2020-06-17 13:53:37 -04001515 paint.addCoverageFragmentProcessor(std::move(fp));
robertphillips00095892016-02-29 13:50:40 -08001516
1517 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001518 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001519 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1520 }
halcanary9d524f22016-03-29 09:03:52 -07001521
Brian Salomon82f44312017-01-11 13:42:54 -05001522 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1523 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001524 return true;
1525}
1526
Michael Ludwig7c12e282020-05-29 09:54:07 -04001527void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001528 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001529 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001530 const SkMatrix& viewMatrix,
1531 const SkRRect& outer,
1532 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001533 ASSERT_SINGLE_OWNER
1534 RETURN_IF_ABANDONED
1535 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001536 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001537
1538 SkASSERT(!outer.isEmpty());
1539 SkASSERT(!inner.isEmpty());
1540
Robert Phillips72152832017-01-25 17:31:35 -05001541 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001542
Brian Salomon82f44312017-01-11 13:42:54 -05001543 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001544 return;
1545 }
Mike Klein16885072018-12-11 09:54:31 -05001546 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001547
1548 SkPath path;
1549 path.setIsVolatile(true);
1550 path.addRRect(inner);
1551 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001552 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001553 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001554}
1555
robertphillipsea461502015-05-26 11:38:03 -07001556///////////////////////////////////////////////////////////////////////////////
1557
Michael Ludwig7c12e282020-05-29 09:54:07 -04001558void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001559 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001560 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001561 const SkMatrix& viewMatrix,
1562 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001563 const GrStyle& style,
1564 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001565 ASSERT_SINGLE_OWNER
1566 RETURN_IF_ABANDONED
1567 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001568 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001569
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001570 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001571 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001572 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001573 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001574 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1575 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001576 aa = GrAA::kNo;
1577 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001578 }
msarettcc319b92016-08-25 18:07:18 -07001579 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001580 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001581 SkPath path;
1582 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001583 path.setIsVolatile(true);
1584
Brian Salomon82f44312017-01-11 13:42:54 -05001585 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001586 }
1587
Chris Dalton7d6748e2019-03-13 00:34:52 -06001588 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001589 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1590 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001591 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001592}
1593
Michael Ludwig7c12e282020-05-29 09:54:07 -04001594void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001595 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001596 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001597 const SkMatrix& viewMatrix,
1598 const SkRect& oval,
1599 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001600 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001601 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001602 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001603 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001604
Robert Phillips7484d202018-07-03 09:09:08 -04001605 const SkStrokeRec& stroke = style.strokeRec();
1606
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001607 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001608 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1609 return;
1610 }
1611
1612 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001613 return;
robertphillipsea461502015-05-26 11:38:03 -07001614 }
1615
Robert Phillips72152832017-01-25 17:31:35 -05001616 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001617
Chris Dalton7d6748e2019-03-13 00:34:52 -06001618 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001619
1620 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001621 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1622 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001623 // We don't draw true circles as round rects in coverage mode, because it can
1624 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1625 assert_alive(paint);
1626 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1627 this->caps()->shaderCaps());
1628 }
1629 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001630 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1631 // the arc equation. This same special geometry and fragment branch also turn out to be a
1632 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1633 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1634 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001635 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001636 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1637 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001638 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001639 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001640 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001641 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1642 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001643 }
1644 if (op) {
1645 this->addDrawOp(clip, std::move(op));
1646 return;
robertphillipsea461502015-05-26 11:38:03 -07001647 }
robertphillipsb56f9272016-02-25 11:03:52 -08001648
Mike Klein16885072018-12-11 09:54:31 -05001649 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001650 this->drawShapeUsingPathRenderer(
1651 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001652 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001653}
1654
Michael Ludwig7c12e282020-05-29 09:54:07 -04001655void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001656 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001658 const SkMatrix& viewMatrix,
1659 const SkRect& oval,
1660 SkScalar startAngle,
1661 SkScalar sweepAngle,
1662 bool useCenter,
1663 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001664 ASSERT_SINGLE_OWNER
1665 RETURN_IF_ABANDONED
1666 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001667 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001668
1669 AutoCheckFlush acf(this->drawingManager());
1670
Chris Dalton7d6748e2019-03-13 00:34:52 -06001671 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001672 if (GrAAType::kCoverage == aaType) {
1673 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1674 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1675 std::move(paint),
1676 viewMatrix,
1677 oval,
1678 startAngle,
1679 sweepAngle,
1680 useCenter,
1681 style,
1682 shaderCaps);
1683 if (op) {
1684 this->addDrawOp(clip, std::move(op));
1685 return;
1686 }
1687 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001688 }
Brian Salomone4949402018-04-26 15:22:04 -04001689 this->drawShapeUsingPathRenderer(
1690 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001691 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001692}
1693
Michael Ludwig7c12e282020-05-29 09:54:07 -04001694void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001695 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001696 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001697 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001698 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001699 sk_sp<GrColorSpaceXform> csxf,
1700 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001701 std::unique_ptr<SkLatticeIter> iter,
1702 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001703 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001704 RETURN_IF_ABANDONED
1705 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001706 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001707
Robert Phillips72152832017-01-25 17:31:35 -05001708 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001709
Brian Salomon2a943df2018-05-04 13:43:19 -04001710 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001711 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001712 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001713 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001714}
1715
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001716void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1717 const SkRect& bounds) {
1718 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1719 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001720 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001721}
1722
Brian Salomon031b0ba2019-05-23 11:05:26 -04001723void GrRenderTargetContext::asyncRescaleAndReadPixels(
1724 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1725 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1726 auto direct = fContext->priv().asDirectContext();
1727 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001728 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001729 return;
1730 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001731 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001732 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001733 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001734 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001735 if (this->asRenderTargetProxy()->framebufferOnly()) {
1736 callback(context, nullptr);
1737 return;
1738 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001739 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001740 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001741 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001742 return;
1743 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001744 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001745 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001746 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001747 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001748 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001749 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1750 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001751 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001752 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001753 backendFormatOfFinalContext, dstCT);
1754 // Fail if we can't read from the source surface's color type.
1755 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001756 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001757 return;
1758 }
Brian Salomon624f9062019-07-02 14:23:00 -04001759 // Fail if read color type does not have all of dstCT's color channels and those missing color
1760 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001761 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1762 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1763 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1764 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001765 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001766 return;
1767 }
1768
Brian Salomonbf6b9792019-08-21 09:38:10 -04001769 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001770 int x = srcRect.fLeft;
1771 int y = srcRect.fTop;
1772 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001773 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1774 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001775 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001776 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001777 return;
1778 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001779 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001780 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001781 x = y = 0;
1782 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001783 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1784 this->colorInfo().alphaType(),
1785 info.colorSpace(),
1786 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001787 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1788 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001789 // We flip or color convert by drawing and we don't currently support drawing to
1790 // kPremul.
1791 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001792 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001793 return;
1794 }
Greg Daniel40903af2020-01-30 14:55:05 -05001795 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001796 SkRect srcRectToDraw = SkRect::Make(srcRect);
1797 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001798 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001799 texProxyView =
1800 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1801 SkBackingFit::kApprox, SkBudgeted::kNo);
1802 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001803 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001804 return;
1805 }
Brian Salomonc5243782020-04-02 12:50:34 -04001806 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001807 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1808 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001809 tempRTC = GrRenderTargetContext::Make(
1810 direct, this->colorInfo().colorType(), info.refColorSpace(),
1811 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001812 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001813 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001814 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001815 return;
1816 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001817 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001818 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1819 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001820 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1821 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1822 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001823 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001824 }
1825 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001826 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001827 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1828 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001829}
1830
Brian Salomon9241a6d2019-10-03 13:26:54 -04001831class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1832public:
1833 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1834 ~AsyncReadResult() override {
1835 for (int i = 0; i < fPlanes.count(); ++i) {
1836 if (!fPlanes[i].fMappedBuffer) {
1837 delete[] static_cast<const char*>(fPlanes[i].fData);
1838 } else {
1839 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1840 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1841 }
1842 }
1843 }
1844
1845 int count() const override { return fPlanes.count(); }
1846 const void* data(int i) const override { return fPlanes[i].fData; }
1847 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1848
1849 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001850 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001851 size_t rowBytes,
1852 GrClientMappedBufferManager* manager) {
1853 SkASSERT(!result.fTransferBuffer->isMapped());
1854 const void* mappedData = result.fTransferBuffer->map();
1855 if (!mappedData) {
1856 return false;
1857 }
1858 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001859 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001860 result.fPixelConverter(convertedData.get(), mappedData);
1861 this->addCpuPlane(std::move(convertedData), rowBytes);
1862 result.fTransferBuffer->unmap();
1863 } else {
1864 manager->insert(result.fTransferBuffer);
1865 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1866 }
1867 return true;
1868 }
1869
1870 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1871 SkASSERT(data);
1872 SkASSERT(rowBytes > 0);
1873 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1874 }
1875
1876private:
1877 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1878 SkASSERT(data);
1879 SkASSERT(rowBytes > 0);
1880 SkASSERT(mappedBuffer);
1881 SkASSERT(mappedBuffer->isMapped());
1882 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1883 }
1884
1885 struct Plane {
1886 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1887 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1888 const void* fData;
1889 size_t fRowBytes;
1890 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1891 sk_sp<GrGpuBuffer> fMappedBuffer;
1892 };
1893 SkSTArray<3, Plane> fPlanes;
1894 uint32_t fInboxID;
1895};
1896
Brian Salomon024bd002019-06-11 11:38:16 -04001897void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1898 ReadPixelsCallback callback,
1899 ReadPixelsContext context) {
1900 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1901 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1902
Greg Danielca9dbe22020-01-02 13:44:30 -05001903 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001904 callback(context, nullptr);
1905 return;
1906 }
1907
Brian Salomon9241a6d2019-10-03 13:26:54 -04001908 auto directContext = fContext->priv().asDirectContext();
1909 SkASSERT(directContext);
1910 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1911
Brian Salomon024bd002019-06-11 11:38:16 -04001912 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1913
1914 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001915 auto ii = SkImageInfo::Make(rect.size(), colorType,
1916 this->colorInfo().alphaType(),
1917 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001918 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1920 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1921 result->addCpuPlane(std::move(data), pm.rowBytes());
1922
Brian Salomon1d435302019-07-01 13:05:28 -04001923 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001924 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001925 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001926 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001927 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001928 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001929 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001930
Brian Salomonab32f652019-05-10 14:24:50 -04001931 struct FinishContext {
1932 ReadPixelsCallback* fClientCallback;
1933 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001934 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001935 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001937 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001938 };
1939 // Assumption is that the caller would like to flush. We could take a parameter or require an
1940 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1941 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001942 auto* finishContext = new FinishContext{callback,
1943 context,
1944 rect.size(),
1945 colorType,
1946 mappedBufferManager,
1947 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001948 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001949 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001950 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001951 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1952 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1953 context->fMappedBufferManager)) {
1954 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001955 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001956 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001957 delete context;
1958 };
1959 GrFlushInfo flushInfo;
1960 flushInfo.fFinishedContext = finishContext;
1961 flushInfo.fFinishedProc = finishCallback;
Greg Daniel9efe3862020-06-11 11:51:06 -04001962 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001963}
1964
Brian Salomon9241a6d2019-10-03 13:26:54 -04001965void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1966 sk_sp<SkColorSpace> dstColorSpace,
1967 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001968 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001969 RescaleGamma rescaleGamma,
1970 SkFilterQuality rescaleQuality,
1971 ReadPixelsCallback callback,
1972 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001973 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1974 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001975 SkASSERT(!dstSize.isZero());
1976 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1977
Brian Salomon024bd002019-06-11 11:38:16 -04001978 auto direct = fContext->priv().asDirectContext();
1979 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001980 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001981 return;
1982 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001983 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001984 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001985 return;
1986 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001987 if (this->asRenderTargetProxy()->framebufferOnly()) {
1988 callback(context, nullptr);
1989 return;
1990 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001991 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001992 callback(context, nullptr);
1993 return;
1994 }
Brian Salomon024bd002019-06-11 11:38:16 -04001995 int x = srcRect.fLeft;
1996 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001997 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001998 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001999 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04002000 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04002001 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04002002 dstColorSpace);
2003 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04002004 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
2005 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002006 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002007 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002008 return;
Brian Salomonab32f652019-05-10 14:24:50 -04002009 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002010 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04002011 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002012 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05002013 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04002014 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05002015 srcView = this->readSurfaceView();
2016 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04002017 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
2018 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
2019 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002020 // If we can't get a texture copy of the contents then give up.
2021 callback(context, nullptr);
2022 return;
2023 }
Brian Salomonc5243782020-04-02 12:50:34 -04002024 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002025 x = y = 0;
2026 }
Brian Salomon4d036892019-07-02 15:10:58 -04002027 // We assume the caller wants kPremul. There is no way to indicate a preference.
2028 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002029 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
2030 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04002031 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002032 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2033 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002034 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2035 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002036 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002037 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002038 return;
2039 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002040 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05002041 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2042 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2043 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2044 SkMatrix::I(), std::move(xform));
2045 srcView = tempRTC->readSurfaceView();
2046 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002047 x = y = 0;
2048 }
2049 }
Greg Danielc594e622019-10-15 14:01:49 -04002050
Greg Daniele20fcad2020-01-08 11:52:34 -05002051 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2052 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2053 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002054 int halfW = dstSize.width()/2;
2055 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002056 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2057 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2058 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2059 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2060 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2061 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002062 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002063 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002064 return;
2065 }
2066
Brian Osman28ba5282019-10-30 14:18:07 -04002067 float baseM[20];
2068 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2069
Brian Salomon024bd002019-06-11 11:38:16 -04002070 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2071
Mike Reed1f607332020-05-21 12:11:27 -04002072 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002073
Brian Salomon9241a6d2019-10-03 13:26:54 -04002074 SkRect dstRectY = SkRect::Make(dstSize);
2075 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002076
Brian Salomon6aa65052020-01-28 12:16:53 -05002077 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2078 PixelTransferResult yTransfer, uTransfer, vTransfer;
2079
Brian Salomon024bd002019-06-11 11:38:16 -04002080 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2081 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002082 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002083 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002084 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002085 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
2086 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
2087 /*unpremulInput=*/false,
2088 /*clampRGBOutput=*/true,
2089 /*premulOutput=*/false);
2090 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002091 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002092 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002093 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002094 if (!doSynchronousRead) {
2095 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2096 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2097 if (!yTransfer.fTransferBuffer) {
2098 callback(context, nullptr);
2099 return;
2100 }
Brian Salomon024bd002019-06-11 11:38:16 -04002101 }
2102
2103 texMatrix.preScale(2.f, 2.f);
2104 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2105 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002106 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002107 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002108 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002109 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2110 GrSamplerState::Filter::kBilerp);
2111 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2112 /*unpremulInput=*/false,
2113 /*clampRGBOutput=*/true,
2114 /*premulOutput=*/false);
2115 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002116 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002117 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002118 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002119 if (!doSynchronousRead) {
2120 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2121 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2122 if (!uTransfer.fTransferBuffer) {
2123 callback(context, nullptr);
2124 return;
2125 }
Brian Salomon024bd002019-06-11 11:38:16 -04002126 }
2127
2128 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2129 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002130 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002131 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002132 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002133 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2134 texMatrix, GrSamplerState::Filter::kBilerp);
2135 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2136 /*unpremulInput=*/false,
2137 /*clampRGBOutput=*/true,
2138 /*premulOutput=*/false);
2139 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002140 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002141 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002142 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002143 if (!doSynchronousRead) {
2144 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2145 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2146 if (!vTransfer.fTransferBuffer) {
2147 callback(context, nullptr);
2148 return;
2149 }
2150 }
2151
2152 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002153 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2154 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2155 size_t yRB = yInfo.minRowBytes();
2156 size_t uvRB = uvInfo.minRowBytes();
2157 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2158 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2159 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2160 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2161 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2162 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002163 callback(context, nullptr);
2164 return;
2165 }
2166 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002167 result->addCpuPlane(std::move(y), yRB );
2168 result->addCpuPlane(std::move(u), uvRB);
2169 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002170 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002171 return;
2172 }
2173
2174 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002175 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002176 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002177 GrClientMappedBufferManager* fMappedBufferManager;
2178 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002179 PixelTransferResult fYTransfer;
2180 PixelTransferResult fUTransfer;
2181 PixelTransferResult fVTransfer;
2182 };
2183 // Assumption is that the caller would like to flush. We could take a parameter or require an
2184 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2185 // callback to GrGpu until after the next flush that flushes our op list, though.
2186 auto* finishContext = new FinishContext{callback,
2187 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002188 direct->priv().clientMappedBufferManager(),
2189 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002190 std::move(yTransfer),
2191 std::move(uTransfer),
2192 std::move(vTransfer)};
2193 auto finishCallback = [](GrGpuFinishedContext c) {
2194 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002195 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002196 auto manager = context->fMappedBufferManager;
2197 size_t rowBytes = SkToSizeT(context->fSize.width());
2198 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2199 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002200 delete context;
2201 return;
2202 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002203 rowBytes /= 2;
2204 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2205 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2206 (*context->fClientCallback)(context->fClientContext, nullptr);
2207 delete context;
2208 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002209 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002210 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2211 (*context->fClientCallback)(context->fClientContext, nullptr);
2212 delete context;
2213 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002214 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002215 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002216 delete context;
2217 };
2218 GrFlushInfo flushInfo;
2219 flushInfo.fFinishedContext = finishContext;
2220 flushInfo.fFinishedProc = finishCallback;
Greg Daniel9efe3862020-06-11 11:51:06 -04002221 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
Brian Salomonab32f652019-05-10 14:24:50 -04002222}
2223
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002224GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -04002225 const GrFlushInfo& info,
2226 const GrBackendSurfaceMutableState* newState) {
robertphillips8c523e02016-07-26 07:41:00 -07002227 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002228 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002229 if (info.fSubmittedProc) {
2230 info.fSubmittedProc(info.fSubmittedContext, false);
2231 }
2232 if (info.fFinishedProc) {
2233 info.fFinishedProc(info.fFinishedContext);
2234 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002235 return GrSemaphoresSubmitted::kNo;
2236 }
robertphillips8c523e02016-07-26 07:41:00 -07002237 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002238 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002239
Greg Daniel9efe3862020-06-11 11:51:06 -04002240 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState);
Greg Daniela5cb7812017-06-16 09:45:32 -04002241}
2242
Greg Danielc64ee462017-06-15 16:59:49 -04002243bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002244 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002245 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002246 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002247 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002248 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002249
2250 AutoCheckFlush acf(this->drawingManager());
2251
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002252 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002253 return false;
2254 }
2255
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002256 auto direct = fContext->priv().asDirectContext();
2257 if (!direct) {
2258 return false;
2259 }
2260
2261 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002262
Greg Daniel301015c2019-11-18 14:06:46 -05002263 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2264 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002265 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002266 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002267 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2268 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002269 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002270 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2271 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002272 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002273}
joshualitt33a5fce2015-11-18 13:28:51 -08002274
Michael Ludwig7c12e282020-05-29 09:54:07 -04002275void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002276 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002277 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002278 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002279 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002280 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002281 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002282 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002283 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002284 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2285
Michael Ludwig2686d692020-04-17 20:21:37 +00002286 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002287
Robert Phillips27927a52018-08-20 13:18:12 -04002288 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002289}
2290
Michael Ludwig7c12e282020-05-29 09:54:07 -04002291void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002292 GrPaint&& paint,
2293 GrAA aa,
2294 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002295 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002296 ASSERT_SINGLE_OWNER
2297 RETURN_IF_ABANDONED
2298 SkDEBUGCODE(this->validate();)
2299 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2300
Brian Salomon2fad74a2017-12-20 13:28:55 -05002301 if (shape.isEmpty()) {
2302 if (shape.inverseFilled()) {
2303 this->drawPaint(clip, std::move(paint), viewMatrix);
2304 }
2305 return;
robertphillipsea461502015-05-26 11:38:03 -07002306 }
2307
Robert Phillips72152832017-01-25 17:31:35 -05002308 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002309
Brian Salomon2fad74a2017-12-20 13:28:55 -05002310 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002311 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002312 SkRRect rrect;
2313 // We can ignore the starting point and direction since there is no path effect.
2314 bool inverted;
2315 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2316 if (rrect.isRect()) {
2317 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2318 &shape.style());
2319 return;
2320 } else if (rrect.isOval()) {
2321 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002322 return;
2323 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002324 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2325 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002326 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2327 viewMatrix.rectStaysRect()) {
2328 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2329 // the matrix to all the points individually rather than just to the rect
2330 SkRect rects[2];
2331 if (shape.asNestedRects(rects)) {
2332 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002333 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002334 fContext, std::move(paint), viewMatrix, rects);
2335 if (op) {
2336 this->addDrawOp(clip, std::move(op));
2337 }
2338 // Returning here indicates that there is nothing to draw in this case.
2339 return;
2340 }
robertphillipsea461502015-05-26 11:38:03 -07002341 }
2342 }
robertphillips4bc31812016-03-01 12:22:49 -08002343
Michael Ludwig6a6de652020-04-30 20:16:36 -04002344 // If we get here in drawShape(), we definitely need to use path rendering
2345 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2346 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002347}
2348
Michael Ludwig7c12e282020-05-29 09:54:07 -04002349bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002350 const GrUserStencilSettings* ss,
2351 SkRegion::Op op,
2352 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002353 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002354 const SkMatrix& viewMatrix,
2355 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002356 ASSERT_SINGLE_OWNER_PRIV
2357 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002358 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002359 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2360 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002361
2362 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002363 GrPaint paint;
2364 paint.setCoverageSetOpXPFactory(op, invert);
2365 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2366 SkRect::MakeIWH(fRenderTargetContext->width(),
2367 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002368 return true;
2369 }
2370
Robert Phillips72152832017-01-25 17:31:35 -05002371 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002372
2373 // An Assumption here is that path renderer would use some form of tweaking
2374 // the src color (either the input alpha or in the frag shader) to implement
2375 // aa. If we have some future driver-mojo path AA that can do the right
2376 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002377 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002378 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002379
Michael Ludwig7c12e282020-05-29 09:54:07 -04002380 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002381
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002382 GrPaint paint;
2383 paint.setCoverageSetOpXPFactory(op, invert);
2384
Michael Ludwig2686d692020-04-17 20:21:37 +00002385 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002386 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002387 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002388 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002389 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002390 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002391 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002392 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002393 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002394 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2395 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002396 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002397
2398 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002399 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002400 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002401 if (!pr) {
2402 return false;
2403 }
2404
Brian Salomonf3569f02017-10-24 12:52:33 -04002405 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2406 std::move(paint),
2407 ss,
2408 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002409 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002410 &clipConservativeBounds,
2411 &viewMatrix,
2412 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002413 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002414 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002415 pr->drawPath(args);
2416 return true;
2417}
2418
Brian Osman11052242016-10-27 14:47:55 -04002419SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002420 ASSERT_SINGLE_OWNER_PRIV
2421
Robert Phillips6a6de562019-02-15 15:19:15 -05002422 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002423 return SkBudgeted::kNo;
2424 }
2425
Brian Osman11052242016-10-27 14:47:55 -04002426 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002427
Greg Daniel3912a4b2020-01-14 09:56:04 -05002428 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002429}
2430
Michael Ludwig7c12e282020-05-29 09:54:07 -04002431void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002432 GrPaint&& paint,
2433 GrAA aa,
2434 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002435 const GrStyledShape& originalShape,
2436 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002437 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002438 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002439 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2440
Jim Van Verthf86073a2018-10-02 13:05:38 -04002441 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2442 return;
2443 }
2444
Michael Ludwig6a6de652020-04-30 20:16:36 -04002445 if (attemptShapeFallback && originalShape.simplified()) {
2446 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2447 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2448 // we ought to try again instead of going right to path rendering.
2449 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2450 return;
2451 }
2452
Michael Ludwig7c12e282020-05-29 09:54:07 -04002453 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002454
Michael Ludwig2686d692020-04-17 20:21:37 +00002455 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002456 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002457
robertphillips68737822015-10-29 12:12:21 -07002458 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002459 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002460 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002461 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002462 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002463 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002464 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002465 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002466 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002467
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002468 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002469 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002470 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002471 return;
2472 }
2473
Chris Dalton6ce447a2019-06-23 18:07:38 -06002474 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002475
2476 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2477 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2478 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2479
Brian Salomon2fad74a2017-12-20 13:28:55 -05002480 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002481 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002482 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2483 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002484 return;
2485 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002486 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002487 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002488 }
2489 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002490 if (canDrawArgs.fShape->style().applies()) {
2491 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2492 styleScale);
2493 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002494 return;
2495 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002496 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002497 // This time, allow SW renderer
2498 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2499 } else {
2500 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002501#if GR_PATH_RENDERER_SPEW
2502 SkDebugf("falling back to: %s\n", pr->name());
2503#endif
bsalomon6663acf2016-05-10 09:14:17 -07002504 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002505 }
robertphillipsea461502015-05-26 11:38:03 -07002506
bsalomon8acedde2016-06-24 10:42:16 -07002507 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002508#ifdef SK_DEBUG
2509 SkDebugf("Unable to find path renderer compatible with path.\n");
2510#endif
2511 return;
2512 }
2513
Robert Phillips256c37b2017-03-01 14:32:46 -05002514 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002515 std::move(paint),
2516 &GrUserStencilSettings::kUnused,
2517 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002518 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002519 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002520 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002521 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002522 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002523 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002524 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002525}
2526
Brian Salomon467921e2017-03-06 16:17:12 -05002527static void op_bounds(SkRect* bounds, const GrOp* op) {
2528 *bounds = op->bounds();
2529 if (op->hasZeroArea()) {
2530 if (op->hasAABloat()) {
2531 bounds->outset(0.5f, 0.5f);
2532 } else {
2533 // We don't know which way the particular GPU will snap lines or points at integer
2534 // coords. So we ensure that the bounds is large enough for either snap.
2535 SkRect before = *bounds;
2536 bounds->roundOut(bounds);
2537 if (bounds->fLeft == before.fLeft) {
2538 bounds->fLeft -= 1;
2539 }
2540 if (bounds->fTop == before.fTop) {
2541 bounds->fTop -= 1;
2542 }
2543 if (bounds->fRight == before.fRight) {
2544 bounds->fRight += 1;
2545 }
2546 if (bounds->fBottom == before.fBottom) {
2547 bounds->fBottom += 1;
2548 }
2549 }
2550 }
2551}
2552
Chris Dalton08755122019-08-05 16:13:47 -06002553void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002554 GrDrawingManager* drawingMgr = this->drawingManager();
2555 this->getOpsTask()->addOp(drawingMgr,
2556 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06002557}
2558
Michael Ludwig7c12e282020-05-29 09:54:07 -04002559void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002560 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002561 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002562 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002563 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002564 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002565 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002566 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002567 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002568 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002569
Brian Salomon467921e2017-03-06 16:17:12 -05002570 // Setup clip
2571 SkRect bounds;
2572 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002573 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002574 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002575 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002576 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002577
Chris Dalton9acf6822019-11-12 11:52:40 -07002578 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002579 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002580 }
2581
Michael Ludwig7c12e282020-05-29 09:54:07 -04002582 bool skipDraw = false;
2583 if (clip) {
2584 // Have a complex clip, so defer to its early clip culling
Michael Ludwigd8f4f422020-06-24 18:58:18 +00002585 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2586 skipDraw = true;
2587 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002588 } else {
2589 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwigd8f4f422020-06-24 18:58:18 +00002590 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2591 skipDraw = true;
2592 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002593 }
2594
2595 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002596 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002597 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002598 }
2599
Chris Dalton9acf6822019-11-12 11:52:40 -07002600 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2601 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2602
2603 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2604 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2605 // that uses a stencil test when the stencil buffer is multisampled.)
2606 bool hasMixedSampledCoverage = (
2607 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002608 SkASSERT(!hasMixedSampledCoverage ||
2609 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002610
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002611 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002612 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002613 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2614
Michael Ludwig28e5f112020-06-09 10:57:24 -04002615 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2616 op->setClippedBounds(bounds);
2617
Greg Daniel524e28b2019-11-01 11:48:53 -04002618 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002619 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002620 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002621 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002622 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002623 }
2624 }
2625
Greg Danielf41b2bd2019-08-22 16:19:24 -04002626 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002627 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002628 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002629 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002630 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
2631 dstProxyView,GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002632}
2633
Michael Ludwig28e5f112020-06-09 10:57:24 -04002634bool GrRenderTargetContext::setupDstProxyView(const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002635 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002636 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2637 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2638 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002639 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002640 return false;
2641 }
2642
Greg Daniel3912a4b2020-01-14 09:56:04 -05002643 if (this->caps()->textureBarrierSupport() &&
2644 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002645 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002646 // The render target is a texture, so we can read from it directly in the shader. The XP
2647 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002648 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002649 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002650 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002651 }
2652 }
2653
Greg Daniel3155f7f2020-01-16 16:54:26 -05002654 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002655 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2656 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002657 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002658 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002659
Michael Ludwig28e5f112020-06-09 10:57:24 -04002660 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002661 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002662 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2663 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2664 // centers during rasterization.
2665 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2666 conservativeDrawBounds.outset(1, 1);
2667 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002668 }
Brian Salomon467921e2017-03-06 16:17:12 -05002669
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002670 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002671 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002672 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002673 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002674 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002675 } else {
Eric Karl74480882017-04-03 14:49:05 -07002676 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002677 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002678 }
Brian Salomonc5243782020-04-02 12:50:34 -04002679 auto copy =
2680 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2681 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2682 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002683
Brian Salomonc5243782020-04-02 12:50:34 -04002684 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002685 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002686 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002687}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002688
Greg Daniel573312e2020-02-07 17:22:35 -05002689bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002690 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002691 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002692 SkIRect clippedSrcRect;
2693 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002694 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2695 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002696 return false;
2697 }
2698
2699 GrPaint paint;
2700 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002701
Greg Danield2ccbb52020-02-05 10:45:39 -05002702 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002703 if (!fp) {
2704 return false;
2705 }
2706 paint.addColorFragmentProcessor(std::move(fp));
2707
2708 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002709 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002710 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2711 clippedSrcRect.height()),
2712 SkRect::Make(clippedSrcRect));
2713 return true;
2714}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002715
2716void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2717 SkASSERT(&task == fOpsTask.get());
2718 fOpsTask.reset();
2719}