blob: c4e3a1341f71116af1c4ea9135fe4281ec3ccaf5 [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 Phillips49f94372020-06-25 02:46:18 +0000295 ReleaseProc releaseProc,
296 ReleaseContext releaseCtx) {
Greg Danielba0ff782020-01-07 15:42:57 -0500297 SkASSERT(sampleCnt > 0);
298 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Robert Phillips49f94372020-06-25 02:46:18 +0000299 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
300 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500301 if (!proxy) {
302 return nullptr;
303 }
304
305 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
306 origin, surfaceProps);
307}
308
309std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
310 GrRecordingContext* context,
311 GrColorType colorType,
312 sk_sp<SkColorSpace> colorSpace,
313 const GrBackendTexture& tex,
314 int sampleCnt,
315 GrSurfaceOrigin origin,
316 const SkSurfaceProps* surfaceProps) {
317 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400318 sk_sp<GrSurfaceProxy> proxy(
319 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500320 if (!proxy) {
321 return nullptr;
322 }
323
324 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
325 origin, surfaceProps);
326}
327
328std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
329 GrRecordingContext* context,
330 GrColorType colorType,
331 sk_sp<SkColorSpace> colorSpace,
332 const GrBackendRenderTarget& rt,
333 GrSurfaceOrigin origin,
334 const SkSurfaceProps* surfaceProps,
335 ReleaseProc releaseProc,
336 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400337 sk_sp<GrSurfaceProxy> proxy(
Robert Phillips49f94372020-06-25 02:46:18 +0000338 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500339 if (!proxy) {
340 return nullptr;
341 }
342
343 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
344 origin, surfaceProps);
345}
346
347std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
348 GrRecordingContext* context,
349 const SkImageInfo& imageInfo,
350 const GrVkDrawableInfo& vkInfo,
351 const SkSurfaceProps* props) {
352 sk_sp<GrSurfaceProxy> proxy(
353 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
354 vkInfo));
355 if (!proxy) {
356 return nullptr;
357 }
358
Greg Danielba0ff782020-01-07 15:42:57 -0500359 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
360 imageInfo.refColorSpace(), std::move(proxy),
361 kTopLeft_GrSurfaceOrigin, props);
362}
363
Greg Danielf41b2bd2019-08-22 16:19:24 -0400364// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
365// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
366// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
367// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500368GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400370 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400371 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400372 sk_sp<SkColorSpace> colorSpace,
373 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400374 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500375 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
376 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400377 , fWriteView(std::move(writeView))
Brian Salomonf3569f02017-10-24 12:52:33 -0400378 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Herb Derbyd29207a2020-06-08 13:50:19 -0400379 , fManagedOpsTask(managedOpsTask)
380 , fGlyphPainter(*this) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400381 fOpsTask = sk_ref_sp(context->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
Brian Salomon3b8486a2020-04-21 12:43:26 -0400382 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400383 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400384 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400385 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
386 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500387
Brian Salomonf18b1d82017-10-27 11:30:49 -0400388 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700389 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700390}
391
robertphillips2e1e51f2015-10-15 08:01:48 -0700392#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500393void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400394 if (fOpsTask && !fOpsTask->isClosed()) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400395 SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700396 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700397}
398#endif
399
Brian Osman11052242016-10-27 14:47:55 -0400400GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800401 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400402 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400403 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400404 }
robertphillipsa106c622015-10-16 09:07:06 -0700405}
406
Chris Dalton7d6748e2019-03-13 00:34:52 -0600407inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408 if (GrAA::kNo == aa) {
409 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
410 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600411 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600412 return GrAAType::kMSAA;
413 }
414 return GrAAType::kNone;
415 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600416 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600417}
418
Greg Daniele252f082017-10-23 16:05:23 -0400419GrMipMapped GrRenderTargetContext::mipMapped() const {
420 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
421 return proxy->mipMapped();
422 }
423 return GrMipMapped::kNo;
424}
425
Greg Danielf41b2bd2019-08-22 16:19:24 -0400426GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800427 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700428 SkDEBUGCODE(this->validate();)
429
Brian Salomon3b8486a2020-04-21 12:43:26 -0400430 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600431 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400432 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600433 if (fOpsTask && fNumStencilSamples > 0) {
434 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600435 fOpsTask->setMustPreserveStencil();
436 // Reload the stencil buffer content at the beginning of newOpsTask.
437 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
438 // values?
439 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
440 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400441 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600442 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700443 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400444 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400445 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700446}
447
Herb Derby64688222020-06-08 14:19:57 -0400448static SkColor compute_canonical_color(const SkPaint& paint, bool lcd) {
449 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
450 if (lcd) {
451 // This is the correct computation for canonicalColor, but there are tons of cases where LCD
452 // can be modified. For now we just regenerate if any run in a textblob has LCD.
453 // TODO figure out where all of these modifications are and see if we can incorporate that
454 // logic at a higher level *OR* use sRGB
455 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
456
457 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
458 // see the note above. We pick a dummy value for LCD text to ensure we always match the
459 // same key
460 return SK_ColorTRANSPARENT;
461 } else {
462 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
463 // gamma corrected masks anyways, nor color
464 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
465 SkColorGetG(canonicalColor),
466 SkColorGetB(canonicalColor));
467 // reduce to our finite number of bits
468 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
469 }
470 return canonicalColor;
471}
472
Michael Ludwig7c12e282020-05-29 09:54:07 -0400473void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400474 const SkMatrixProvider& matrixProvider,
Herb Derby64688222020-06-08 14:19:57 -0400475 const SkGlyphRunList& glyphRunList) {
joshualitt1de610a2016-01-06 08:26:09 -0800476 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700477 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700478 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400479 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700480
Greg Danielbe7fc462019-01-03 16:40:42 -0500481 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
482 // secondary command buffers because it would require stopping and starting a render pass which
483 // we don't have access to.
484 if (this->wrapsVkSecondaryCB()) {
485 return;
486 }
487
Herb Derbya08bde62020-06-12 15:46:06 -0400488 GrSDFTOptions options = fContext->priv().SDFTOptions();
Herb Derby64688222020-06-08 14:19:57 -0400489 GrTextBlobCache* textBlobCache = fContext->priv().getTextBlobCache();
490
491 // Get the first paint to use as the key paint.
492 const SkPaint& blobPaint = glyphRunList.paint();
493
494 SkPoint drawOrigin = glyphRunList.origin();
495
496 SkMaskFilterBase::BlurRec blurRec;
497 // It might be worth caching these things, but its not clear at this time
498 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
499 const SkMaskFilter* mf = blobPaint.getMaskFilter();
500 bool canCache = glyphRunList.canCache() &&
501 !(blobPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
502
503 // If we're doing linear blending, then we can disable the gamma hacks.
504 // Otherwise, leave them on. In either case, we still want the contrast boost:
505 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
506 SkScalerContextFlags scalerContextFlags = this->colorInfo().isLinearlyBlended()
507 ? SkScalerContextFlags::kBoostContrast
508 : SkScalerContextFlags::kFakeGammaAndBoostContrast;
509
510 sk_sp<GrTextBlob> blob;
511 GrTextBlob::Key key;
512 if (canCache) {
513 bool hasLCD = glyphRunList.anyRunsLCD();
514
515 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
516 SkPixelGeometry pixelGeometry =
517 hasLCD ? fSurfaceProps.pixelGeometry() : kUnknown_SkPixelGeometry;
518
519 GrColor canonicalColor = compute_canonical_color(blobPaint, hasLCD);
520
521 key.fPixelGeometry = pixelGeometry;
522 key.fUniqueID = glyphRunList.uniqueID();
523 key.fStyle = blobPaint.getStyle();
524 key.fHasBlur = SkToBool(mf);
525 key.fCanonicalColor = canonicalColor;
526 key.fScalerContextFlags = scalerContextFlags;
527 blob = textBlobCache->find(key);
528 }
529
530 const SkMatrix& drawMatrix(matrixProvider.localToDevice());
531 if (blob != nullptr && blob->canReuse(blobPaint, blurRec, drawMatrix, drawOrigin)) {
532 // Reusing the blob. Move it to the front of LRU cache.
533 textBlobCache->makeMRU(blob.get());
534 } else {
535 // Build or Rebuild the GrTextBlob
536 if (blob != nullptr) {
537 // We have to remake the blob because changes may invalidate our masks.
538 // TODO we could probably get away with reuse most of the time if the pointer is unique,
539 // but we'd have to clear the SubRun information
540 textBlobCache->remove(blob.get());
541 }
542 if (canCache) {
543 blob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, drawMatrix);
544 } else {
545 blob = GrTextBlob::Make(glyphRunList, drawMatrix);
546 }
547 bool supportsSDFT = fContext->priv().caps()->shaderCaps()->supportsDistanceFieldText();
548 fGlyphPainter.processGlyphRunList(
549 glyphRunList, drawMatrix, fSurfaceProps, supportsSDFT, options, blob.get());
550 }
551
552 blob->insertOpsIntoTarget(
553 fTextTarget.get(), fSurfaceProps, blobPaint, clip, matrixProvider, drawOrigin);
robertphillipsea461502015-05-26 11:38:03 -0700554}
555
Brian Osman11052242016-10-27 14:47:55 -0400556void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800557 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700558 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700559 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400560 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700561
Robert Phillips72152832017-01-25 17:31:35 -0500562 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400563
Greg Danielf41b2bd2019-08-22 16:19:24 -0400564 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700565}
566
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500567static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
568 paint->setColor4f(color);
569 if (color.isOpaque()) {
570 // Can just rely on the src-over blend mode to do the right thing
571 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
572 } else {
573 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
574 // were src blended
575 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
576 }
577}
578
Michael Ludwig81d41722020-05-26 16:57:38 -0400579// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
580// premul alpha type. If we ever support different alpha type render targets, this function should
581// transform the color as appropriate.
582void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500583 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400584 bool upgradePartialToFull) {
585 ASSERT_SINGLE_OWNER
586 RETURN_IF_ABANDONED
587 SkDEBUGCODE(this->validate();)
588 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
589
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400590 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
591 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
592 // supports then. Drawing an axis-aligned rect is the fallback path.
593 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
594 if (scissor && !scissorState.set(*scissor)) {
595 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
596 // except clear ops are not draw ops).
597 return;
598 }
599
600 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
601 // the test. We only do this when the clear would be handled by a load op or natively.
602 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
603 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
604 this->caps()->shouldInitializeTextures())) {
605 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
606 // only clear the entire target if we knew it had not been cleared before. As
607 // is this could end up doing a lot of redundant clears.
608 scissorState.setDisabled();
609 } else {
610 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
611 // render target to overflow into any approx-fit padding of the backing store dimensions
612 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400613 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500614 }
615
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400616 if (!scissorState.enabled()) {
617 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
618 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400619 GrOpsTask* opsTask = this->getOpsTask();
620 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500621 !this->caps()->performColorClearsAsDraws()) {
622 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400623 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500624 return;
625 } else {
626 // Will use an op for the clear, reset the load op to discard since the op will
627 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400628 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500629 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400630 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500631
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400632 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
633 // as a load op.
634 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
635 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
636 if (clearAsDraw) {
637 GrPaint paint;
638 clear_to_grpaint(color, &paint);
639 this->addDrawOp(nullptr,
640 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
641 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500642 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -0400643 this->addOp(GrClearOp::MakeColor(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500644 }
645}
646
Michael Ludwig7c12e282020-05-29 09:54:07 -0400647void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500648 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400649 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000650 // Start with the render target, since that is the maximum content we could possibly fill.
651 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500652 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400653 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000654 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
655 // inverse view matrix.
656 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
657 } else {
658 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
659 SkMatrix localMatrix;
660 if (!viewMatrix.invert(&localMatrix)) {
661 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400662 }
Michael Ludwig61328202019-06-19 14:48:58 +0000663 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
664 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700665 }
robertphillipsea461502015-05-26 11:38:03 -0700666}
667
Michael Ludwig61328202019-06-19 14:48:58 +0000668enum class GrRenderTargetContext::QuadOptimization {
669 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
670 kDiscarded,
671 // The rect to draw was converted to some other op and appended to the oplist, so no additional
672 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
673 // a constColor is provided.
674 kSubmitted,
675 // The clip was folded into the device quad, with updated edge flags and local coords, and
676 // caller is responsible for adding an appropriate op.
677 kClipApplied,
678 // No change to clip, but quad updated to better fit clip/render target, and caller is
679 // responsible for adding an appropriate op.
680 kCropped
681};
Michael Ludwig61a16512019-01-15 11:15:13 -0500682
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000683static bool make_vertex_finite(float* value) {
684 if (SkScalarIsNaN(*value)) {
685 return false;
686 }
687
688 if (!SkScalarIsFinite(*value)) {
689 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
690 // left when calculating crops.
691 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
692 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
693 }
694
695 return true;
696}
697
698static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
699 return clip ? clip->getConservativeBounds() : SkIRect::MakeWH(rtc->width(), rtc->height());
700}
701
Michael Ludwig61328202019-06-19 14:48:58 +0000702GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400703 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500704 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000705 // Optimization requirements:
706 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500707 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
708 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
709 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
710 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
711 // 5. kCropped in all other scenarios (although a crop may be a no-op)
712
713 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
714 // better to just keep the old flags instead of introducing mixed edge flags.
715 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000716
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400717 // Use the logical size of the render target, which allows for "fullscreen" clears even if
718 // the render target has an approximate backing fit
719 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400720
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500721 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000722 if (constColor) {
723 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
724 // will be cropped to the finite 'clip' or render target and there is no local space mapping
725 if (!quad->fDevice.isFinite()) {
726 for (int i = 0; i < 4; ++i) {
727 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
728 !make_vertex_finite(quad->fDevice.ys() + i) ||
729 !make_vertex_finite(quad->fDevice.ws() + i)) {
730 // Discard if we see a nan
731 return QuadOptimization::kDiscarded;
Michael Ludwig97164142020-06-24 10:33:43 -0400732 }
Michael Ludwig61328202019-06-19 14:48:58 +0000733 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000734 SkASSERT(quad->fDevice.isFinite());
Michael Ludwig97164142020-06-24 10:33:43 -0400735 }
736 } else {
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000737 // CropToRect requires the quads to be finite. If they are not finite and we have local
738 // coordinates, the mapping from local space to device space is poorly defined so drop it
739 if (!quad->fDevice.isFinite()) {
740 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000741 }
Brian Salomon7694b902019-06-18 21:00:21 +0000742 }
743
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000744 // If the quad is entirely off screen, it doesn't matter what the clip does
745 if (!rtRect.intersects(drawBounds)) {
746 return QuadOptimization::kDiscarded;
747 }
748
749 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
750 // the render target).
751 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
752 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
753 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
754 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
755 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
756 bool axisAlignedClip = true;
757 if (clip && !clip->quickContains(rtRect)) {
758 if (!clip->isRRect(&clipRRect, &clipAA)) {
759 axisAlignedClip = false;
760 }
761 }
762
763 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
764 // draw geometry so that no clip is needed when drawing.
765 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
766 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
767 // of the render target and the clip rect)
768 SkRect clipBounds = rtRect;
769 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
770 return QuadOptimization::kDiscarded;
771 }
772
773 if (clipRRect.isRect()) {
774 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
775 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
776 if (!stencilSettings && constColor &&
777 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
778 // Clear optimization is possible
779 drawBounds = quad->fDevice.bounds();
780 if (drawBounds.contains(rtRect)) {
781 // Fullscreen clear
782 this->clear(*constColor);
783 return QuadOptimization::kSubmitted;
784 } else if (GrClip::IsPixelAligned(drawBounds) &&
785 drawBounds.width() > 256 && drawBounds.height() > 256) {
786 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
787 SkIRect scissorRect;
788 drawBounds.round(&scissorRect);
789 this->clear(scissorRect, *constColor);
790 return QuadOptimization::kSubmitted;
791 }
792 }
793
794 // Update overall AA setting.
795 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
796 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
797 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
798 // properly reflect the smooth edge of the clip.
799 *aa = GrAA::kYes;
800 }
801 // We intentionally do not downgrade AA here because we don't know if we need to
802 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
803 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
804
805 // deviceQuad is exactly the intersection of original quad and clip, so it can be
806 // drawn with no clip (submitted by caller)
807 return QuadOptimization::kClipApplied;
808 } else {
809 // The quads have been updated to better fit the clip bounds, but can't get rid of
810 // the clip entirely
811 quad->fEdgeFlags = oldFlags;
812 return QuadOptimization::kCropped;
813 }
814 } else if (!stencilSettings && constColor) {
815 // Rounded corners and constant filled color (limit ourselves to solid colors because
816 // there is no way to use custom local coordinates with drawRRect).
817 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
818 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
819 quad->fDevice.bounds().contains(clipBounds)) {
820 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
821 // we can draw the rrect directly and ignore the edge flags
822 GrPaint paint;
823 clear_to_grpaint(*constColor, &paint);
824 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
825 clipRRect, GrStyle::SimpleFill());
826 return QuadOptimization::kSubmitted;
827 } else {
828 // The quad has been updated to better fit clip bounds, but can't remove the clip
829 quad->fEdgeFlags = oldFlags;
830 return QuadOptimization::kCropped;
831 }
832 }
833 }
834
835 // Crop the quad to the conservative bounds of the clip.
836 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
837
838 // One final check for discarding, since we may have gone here directly due to a complex clip
839 if (!clipBounds.intersects(drawBounds)) {
840 return QuadOptimization::kDiscarded;
841 }
842
843 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
844 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
845 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500846 quad->fEdgeFlags = oldFlags;
Michael Ludwigd8f4f422020-06-24 18:58:18 +0000847
Michael Ludwig61328202019-06-19 14:48:58 +0000848 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000849}
850
Michael Ludwig7c12e282020-05-29 09:54:07 -0400851void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500852 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500853 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500854 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400855 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000856 ASSERT_SINGLE_OWNER
857 RETURN_IF_ABANDONED
858 SkDEBUGCODE(this->validate();)
859 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500860
Michael Ludwig61328202019-06-19 14:48:58 +0000861 AutoCheckFlush acf(this->drawingManager());
862
863 SkPMColor4f* constColor = nullptr;
864 SkPMColor4f paintColor;
865 if (!ss && !paint.numCoverageFragmentProcessors() &&
866 paint.isConstantBlendedColor(&paintColor)) {
867 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
868 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500869 }
870
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500871 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000872 if (opt >= QuadOptimization::kClipApplied) {
873 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400874 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000875 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
876 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500877 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
878 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700879 }
Michael Ludwig61328202019-06-19 14:48:58 +0000880 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800881}
882
Michael Ludwig7c12e282020-05-29 09:54:07 -0400883void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400884 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500885 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000886 sk_sp<GrColorSpaceXform> textureXform,
887 GrSamplerState::Filter filter,
888 const SkPMColor4f& color,
889 SkBlendMode blendMode,
890 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500891 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400892 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000893 ASSERT_SINGLE_OWNER
894 RETURN_IF_ABANDONED
895 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400896 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000897 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
898
899 AutoCheckFlush acf(this->drawingManager());
900
901 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
902 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500903 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000904
905 SkASSERT(opt != QuadOptimization::kSubmitted);
906 if (opt != QuadOptimization::kDiscarded) {
907 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400908 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000909 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400910 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400911 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
912 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400913 // Use the provided subset, although hypothetically we could detect that the cropped local
914 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500915 this->addDrawOp(finalClip,
916 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
917 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400918 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000919 }
920}
921
Michael Ludwig7c12e282020-05-29 09:54:07 -0400922void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500923 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500924 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400925 const SkMatrix& viewMatrix,
926 const SkRect& rect,
927 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700928 if (!style) {
929 style = &GrStyle::SimpleFill();
930 }
joshualitt1de610a2016-01-06 08:26:09 -0800931 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700932 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700933 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400934 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700935
bsalomon6663acf2016-05-10 09:14:17 -0700936 // Path effects should've been devolved to a path in SkGpuDevice
937 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700938
Robert Phillips72152832017-01-25 17:31:35 -0500939 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700940
bsalomon6663acf2016-05-10 09:14:17 -0700941 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400942 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000943 // Fills the rect, using rect as its own local coordinates
944 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500945 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400946 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
947 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
948 (rect.width() && rect.height())) {
949 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
950 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400951 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700952
Chris Dalton7d6748e2019-03-13 00:34:52 -0600953 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500954 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
955 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
956 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500957 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400958 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700959 return;
robertphillips4bc31812016-03-01 12:22:49 -0800960 }
robertphillips4bc31812016-03-01 12:22:49 -0800961 }
Mike Klein16885072018-12-11 09:54:31 -0500962 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000963 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
964 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700965}
966
Michael Ludwig7c12e282020-05-29 09:54:07 -0400967void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500968 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
969 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600970 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500971
972 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
973 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500974}
975
Robert Phillipsec2249f2016-11-09 08:54:35 -0500976int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500977 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400978 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500979}
980
Greg Danielf41b2bd2019-08-22 16:19:24 -0400981GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600982 ) const {
983#if GR_TEST_UTILS
984 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400985 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600986 }
987#endif
988 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
989 // would normally be overwritten. The one exception is if the render target context is marked as
990 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
991 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
992 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
993 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400994 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600995}
996
Chris Dalton858cf232019-10-14 16:20:00 -0600997void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600998 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600999 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -06001000 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -06001001
Chris Daltoneffee202019-07-01 22:28:03 -06001002 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -06001003 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -05001004 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -06001005 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -04001006 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -06001007 }
1008 SkASSERT(numRequiredSamples > 0);
1009
1010 if (numRequiredSamples > fNumStencilSamples) {
1011 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -05001012 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -06001013 }
Chris Dalton6b982802019-06-27 13:53:46 -06001014
Chris Dalton674f77a2019-09-30 20:49:39 -06001015 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -06001016 if (this->caps()->performStencilClearsAsDraws()) {
1017 // There is a driver bug with clearing stencil. We must use an op to manually clear the
1018 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -04001019 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -06001020 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -06001021 this->getOpsTask()->setInitialStencilContent(
1022 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -06001023 }
1024 }
1025}
1026
Michael Ludwig81d41722020-05-26 16:57:38 -04001027void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -06001028 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
1029
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001030 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
1031 if (scissor && !scissorState.set(*scissor)) {
1032 // The requested clear region is off screen, so nothing to do.
1033 return;
1034 }
1035
Michael Ludwig1e632792020-05-21 12:45:31 -04001036 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001037 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -04001038 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001039 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001040
1041 // Configure the paint to have no impact on the color buffer
1042 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001043 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001044 this->addDrawOp(nullptr,
1045 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1046 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001047 } else {
Michael Ludwig9cced932020-06-08 09:30:09 -04001048 this->addOp(GrClearOp::MakeStencilClip(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -05001049 }
robertphillips976f5f02016-06-03 10:59:20 -07001050}
1051
Michael Ludwig7c12e282020-05-29 09:54:07 -04001052void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001053 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001054 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001055 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001056 ASSERT_SINGLE_OWNER_PRIV
1057 RETURN_IF_ABANDONED_PRIV
1058 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001059 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1060 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001061
Brian Salomon467921e2017-03-06 16:17:12 -05001062 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1063 SkASSERT(path);
1064 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1065
1066 // FIXME: Use path bounds instead of this WAR once
1067 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1068 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1069
Michael Ludwig7c12e282020-05-29 09:54:07 -04001070 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
1071 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
1072 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001073 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
1074 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
1075
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001076 if (clip && !clip->apply(&appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001077 return;
1078 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001079 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
1080 // but as it is, we're just using the full render target so intersecting the two bounds would
1081 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -05001082
Robert Phillips7c525e62018-06-12 10:11:12 -04001083 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1084 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001085 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001086 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001087 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001088 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001089 if (!op) {
1090 return;
1091 }
Brian Salomon97180af2017-03-14 13:42:58 -04001092 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001093
Chris Daltoneffee202019-07-01 22:28:03 -06001094 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001095 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001096}
1097
Michael Ludwig7c12e282020-05-29 09:54:07 -04001098void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001099 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001100 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001101 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1102 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001103 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001104 ASSERT_SINGLE_OWNER
1105 RETURN_IF_ABANDONED
1106 SkDEBUGCODE(this->validate();)
1107 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001108
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001109 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1110 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1111 AutoCheckFlush acf(this->drawingManager());
1112 GrAAType aaType = this->chooseAAType(aa);
1113 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1114 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1115 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001116 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1117 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001118}
1119
Michael Ludwig7c12e282020-05-29 09:54:07 -04001120void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001121 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001122 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001123 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001124 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001125 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001126 ASSERT_SINGLE_OWNER
1127 RETURN_IF_ABANDONED
1128 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001129 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001130
1131 AutoCheckFlush acf(this->drawingManager());
1132
1133 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001134 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001135 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001136 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001137 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001138 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001139 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001140}
1141
1142///////////////////////////////////////////////////////////////////////////////
1143
Michael Ludwig7c12e282020-05-29 09:54:07 -04001144void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001145 GrPaint&& paint,
1146 const SkMatrix& viewMatrix,
1147 int spriteCount,
1148 const SkRSXform xform[],
1149 const SkRect texRect[],
1150 const SkColor colors[]) {
1151 ASSERT_SINGLE_OWNER
1152 RETURN_IF_ABANDONED
1153 SkDEBUGCODE(this->validate();)
1154 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1155
1156 AutoCheckFlush acf(this->drawingManager());
1157
1158 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1159 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1160 aaType, spriteCount, xform, texRect, colors);
1161 this->addDrawOp(clip, std::move(op));
1162}
1163
1164///////////////////////////////////////////////////////////////////////////////
1165
Michael Ludwig7c12e282020-05-29 09:54:07 -04001166void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001167 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001168 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001169 const SkMatrix& viewMatrix,
1170 const SkRRect& rrect,
1171 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001172 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001173 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001174 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001175 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001176
1177 const SkStrokeRec& stroke = style.strokeRec();
1178 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001179 return;
1180 }
1181
Michael Ludwig7c12e282020-05-29 09:54:07 -04001182 const GrClip* clip = origClip;
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001183#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1184 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
1185 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
1186 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1187 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1188 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1189 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001190 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001191 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001192 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1193 clip = nullptr;
Michael Ludwig97164142020-06-24 10:33:43 -04001194 }
Michael Ludwigd8f4f422020-06-24 18:58:18 +00001195#endif
1196 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001197
Robert Phillips72152832017-01-25 17:31:35 -05001198 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001199
Chris Dalton7d6748e2019-03-13 00:34:52 -06001200 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001201
Chris Dalton0dffbab2019-03-27 13:08:50 -06001202 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001203 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1204 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1205 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1206 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1207 // to avoid perf regressions on some platforms.
1208 assert_alive(paint);
1209 op = GrOvalOpFactory::MakeCircularRRectOp(
1210 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1211 }
1212 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001213 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001214 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001215 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001216 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001217 assert_alive(paint);
1218 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001219 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001220 }
1221 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001222 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001223 return;
robertphillipsea461502015-05-26 11:38:03 -07001224 }
robertphillipsb56f9272016-02-25 11:03:52 -08001225
Mike Klein16885072018-12-11 09:54:31 -05001226 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001227 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001228 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001229}
1230
Jim Van Verthc5903412016-11-17 15:27:09 -05001231///////////////////////////////////////////////////////////////////////////////
1232
Jim Van Verth3af1af92017-05-18 15:06:54 -04001233static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1234 SkPoint3 result;
1235 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1236 result.fZ = pt.fZ;
1237 return result;
1238}
1239
Michael Ludwig7c12e282020-05-29 09:54:07 -04001240bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001241 const SkMatrix& viewMatrix,
1242 const SkPath& path,
1243 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001244 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001245 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001246 return true;
1247 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001248 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001249 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001250
1251 // check z plane
1252 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1253 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1254 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1255 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1256 return false;
1257 }
1258
1259 SkRRect rrect;
1260 SkRect rect;
1261 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001262 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001263 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1264 if (!isRRect &&
1265 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1266 rect.width() > SK_ScalarNearlyZero) {
1267 rrect.setOval(rect);
1268 isRRect = true;
1269 }
1270 if (!isRRect && path.isRect(&rect)) {
1271 rrect.setRect(rect);
1272 isRRect = true;
1273 }
1274
1275 if (!isRRect) {
1276 return false;
1277 }
1278
Jim Van Verthc5903412016-11-17 15:27:09 -05001279 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001280 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001281 }
1282
Robert Phillips72152832017-01-25 17:31:35 -05001283 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001284
Jim Van Verth3af1af92017-05-18 15:06:54 -04001285 // transform light
1286 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1287
1288 // 1/scale
1289 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001290 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001291 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1292 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1293
1294 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001295 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1296
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001297 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001298 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1299 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1300 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001301
1302 // Outset the shadow rrect to the border of the penumbra
1303 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1304 SkRRect ambientRRect;
1305 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1306 // If the rrect was an oval then its outset will also be one.
1307 // We set it explicitly to avoid errors.
1308 if (rrect.isOval()) {
1309 ambientRRect = SkRRect::MakeOval(outsetRect);
1310 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001311 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001312 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1313 }
1314
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001315 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001316 if (transparent) {
1317 // set a large inset to force a fill
1318 devSpaceInsetWidth = ambientRRect.width();
1319 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001320
Robert Phillips7c525e62018-06-12 10:11:12 -04001321 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1322 ambientColor,
1323 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001324 ambientRRect,
1325 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001326 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001327 if (op) {
1328 this->addDrawOp(clip, std::move(op));
1329 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001330 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001331
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001332 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001333 SkScalar devSpaceSpotBlur;
1334 SkScalar spotScale;
1335 SkVector spotOffset;
1336 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1337 devLightPos.fZ, rec.fLightRadius,
1338 &devSpaceSpotBlur, &spotScale, &spotOffset);
1339 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001340 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1341
Jim Van Verth3af1af92017-05-18 15:06:54 -04001342 // Adjust translate for the effect of the scale.
1343 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1344 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1345 // This offset is in dev space, need to transform it into source space.
1346 SkMatrix ctmInverse;
1347 if (viewMatrix.invert(&ctmInverse)) {
1348 ctmInverse.mapPoints(&spotOffset, 1);
1349 } else {
1350 // Since the matrix is a similarity, this should never happen, but just in case...
1351 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1352 SkASSERT(false);
1353 }
1354
1355 // Compute the transformed shadow rrect
1356 SkRRect spotShadowRRect;
1357 SkMatrix shadowTransform;
1358 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1359 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001360 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001361
1362 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001363 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001364 SkScalar insetWidth = blurOutset;
1365 if (transparent) {
1366 // If transparent, just do a fill
1367 insetWidth += spotShadowRRect.width();
1368 } else {
1369 // For shadows, instead of using a stroke we specify an inset from the penumbra
1370 // border. We want to extend this inset area so that it meets up with the caster
1371 // geometry. The inset geometry will by default already be inset by the blur width.
1372 //
1373 // We compare the min and max corners inset by the radius between the original
1374 // rrect and the shadow rrect. The distance between the two plus the difference
1375 // between the scaled radius and the original radius gives the distance from the
1376 // transformed shadow shape to the original shape in that corner. The max
1377 // of these gives the maximum distance we need to cover.
1378 //
1379 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1380 // that to get the full insetWidth.
1381 SkScalar maxOffset;
1382 if (rrect.isRect()) {
1383 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001384 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001385 rrect.rect().fLeft),
1386 SkTAbs(spotShadowRRect.rect().fTop -
1387 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001388 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001389 rrect.rect().fRight),
1390 SkTAbs(spotShadowRRect.rect().fBottom -
1391 rrect.rect().fBottom)));
1392 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001393 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001394 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1395 rrect.rect().fLeft + dr,
1396 spotShadowRRect.rect().fTop -
1397 rrect.rect().fTop + dr);
1398 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1399 rrect.rect().fRight - dr,
1400 spotShadowRRect.rect().fBottom -
1401 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001402 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001403 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001404 }
Brian Osman788b9162020-02-07 10:36:46 -05001405 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001406 }
1407
1408 // Outset the shadow rrect to the border of the penumbra
1409 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1410 if (spotShadowRRect.isOval()) {
1411 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1412 } else {
1413 SkScalar outsetRad = spotRadius + blurOutset;
1414 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1415 }
1416
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001417 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001418
Robert Phillips7c525e62018-06-12 10:11:12 -04001419 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1420 spotColor,
1421 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001422 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001423 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001424 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001425 if (op) {
1426 this->addDrawOp(clip, std::move(op));
1427 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001428 }
1429
1430 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001431}
1432
1433///////////////////////////////////////////////////////////////////////////////
1434
Michael Ludwig7c12e282020-05-29 09:54:07 -04001435bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001436 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001437 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001438 const SkMatrix& viewMatrix,
1439 const SkRRect& origOuter,
1440 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001441 SkASSERT(!origInner.isEmpty());
1442 SkASSERT(!origOuter.isEmpty());
1443
Brian Salomon65749212017-12-01 16:01:47 -05001444 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1445
Chris Dalton7d6748e2019-03-13 00:34:52 -06001446 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001447
1448 if (GrAAType::kMSAA == aaType) {
1449 return false;
1450 }
1451
Greg Daniel2655ede2019-04-10 00:49:28 +00001452 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1453 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001454 auto outerR = outer->width() / 2.f;
1455 auto innerR = inner->width() / 2.f;
1456 auto cx = outer->getBounds().fLeft + outerR;
1457 auto cy = outer->getBounds().fTop + outerR;
1458 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1459 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1460 auto avgR = (innerR + outerR) / 2.f;
1461 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1462 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1463 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001464 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001465 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001466 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001467 if (op) {
1468 this->addDrawOp(clip, std::move(op));
1469 return true;
1470 }
Mike Klein16885072018-12-11 09:54:31 -05001471 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001472 }
1473 }
1474
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001475 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001476 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001477 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1478 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001479 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001480 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1481 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001482 }
robertphillips00095892016-02-29 13:50:40 -08001483
robertphillips00095892016-02-29 13:50:40 -08001484 SkMatrix inverseVM;
1485 if (!viewMatrix.isIdentity()) {
1486 if (!origInner.transform(viewMatrix, inner.writable())) {
1487 return false;
1488 }
1489 if (!origOuter.transform(viewMatrix, outer.writable())) {
1490 return false;
1491 }
1492 if (!viewMatrix.invert(&inverseVM)) {
1493 return false;
1494 }
1495 } else {
1496 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001497 }
robertphillips00095892016-02-29 13:50:40 -08001498
Ethan Nicholaseace9352018-10-15 20:09:54 +00001499 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001500 // TODO these need to be a geometry processors
John Stiles851b90e2020-06-17 13:53:37 -04001501 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, innerEdgeType, *inner, caps);
1502 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001503 return false;
1504 }
1505
John Stiles851b90e2020-06-17 13:53:37 -04001506 std::tie(success, fp) = GrRRectEffect::Make(std::move(fp), outerEdgeType, *outer, caps);
1507 if (!success) {
robertphillips00095892016-02-29 13:50:40 -08001508 return false;
1509 }
1510
John Stiles851b90e2020-06-17 13:53:37 -04001511 paint.addCoverageFragmentProcessor(std::move(fp));
robertphillips00095892016-02-29 13:50:40 -08001512
1513 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001514 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001515 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1516 }
halcanary9d524f22016-03-29 09:03:52 -07001517
Brian Salomon82f44312017-01-11 13:42:54 -05001518 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1519 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001520 return true;
1521}
1522
Michael Ludwig7c12e282020-05-29 09:54:07 -04001523void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001524 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001525 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001526 const SkMatrix& viewMatrix,
1527 const SkRRect& outer,
1528 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001529 ASSERT_SINGLE_OWNER
1530 RETURN_IF_ABANDONED
1531 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001532 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001533
1534 SkASSERT(!outer.isEmpty());
1535 SkASSERT(!inner.isEmpty());
1536
Robert Phillips72152832017-01-25 17:31:35 -05001537 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001538
Brian Salomon82f44312017-01-11 13:42:54 -05001539 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001540 return;
1541 }
Mike Klein16885072018-12-11 09:54:31 -05001542 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001543
1544 SkPath path;
1545 path.setIsVolatile(true);
1546 path.addRRect(inner);
1547 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001548 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001549 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001550}
1551
robertphillipsea461502015-05-26 11:38:03 -07001552///////////////////////////////////////////////////////////////////////////////
1553
Michael Ludwig7c12e282020-05-29 09:54:07 -04001554void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001555 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001556 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001557 const SkMatrix& viewMatrix,
1558 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001559 const GrStyle& style,
1560 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001561 ASSERT_SINGLE_OWNER
1562 RETURN_IF_ABANDONED
1563 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001564 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001565
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001566 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001567 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001568 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001569 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001570 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1571 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001572 aa = GrAA::kNo;
1573 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001574 }
msarettcc319b92016-08-25 18:07:18 -07001575 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001576 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001577 SkPath path;
1578 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001579 path.setIsVolatile(true);
1580
Brian Salomon82f44312017-01-11 13:42:54 -05001581 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001582 }
1583
Chris Dalton7d6748e2019-03-13 00:34:52 -06001584 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001585 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1586 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001587 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001588}
1589
Michael Ludwig7c12e282020-05-29 09:54:07 -04001590void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001591 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001592 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001593 const SkMatrix& viewMatrix,
1594 const SkRect& oval,
1595 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001596 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001597 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001598 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001599 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001600
Robert Phillips7484d202018-07-03 09:09:08 -04001601 const SkStrokeRec& stroke = style.strokeRec();
1602
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001603 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001604 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1605 return;
1606 }
1607
1608 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001609 return;
robertphillipsea461502015-05-26 11:38:03 -07001610 }
1611
Robert Phillips72152832017-01-25 17:31:35 -05001612 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001613
Chris Dalton7d6748e2019-03-13 00:34:52 -06001614 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001615
1616 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001617 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1618 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001619 // We don't draw true circles as round rects in coverage mode, because it can
1620 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1621 assert_alive(paint);
1622 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1623 this->caps()->shaderCaps());
1624 }
1625 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001626 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1627 // the arc equation. This same special geometry and fragment branch also turn out to be a
1628 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1629 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1630 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001631 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001632 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1633 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001634 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001635 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001636 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001637 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1638 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001639 }
1640 if (op) {
1641 this->addDrawOp(clip, std::move(op));
1642 return;
robertphillipsea461502015-05-26 11:38:03 -07001643 }
robertphillipsb56f9272016-02-25 11:03:52 -08001644
Mike Klein16885072018-12-11 09:54:31 -05001645 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001646 this->drawShapeUsingPathRenderer(
1647 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001648 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001649}
1650
Michael Ludwig7c12e282020-05-29 09:54:07 -04001651void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001652 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001653 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001654 const SkMatrix& viewMatrix,
1655 const SkRect& oval,
1656 SkScalar startAngle,
1657 SkScalar sweepAngle,
1658 bool useCenter,
1659 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001660 ASSERT_SINGLE_OWNER
1661 RETURN_IF_ABANDONED
1662 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001663 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001664
1665 AutoCheckFlush acf(this->drawingManager());
1666
Chris Dalton7d6748e2019-03-13 00:34:52 -06001667 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001668 if (GrAAType::kCoverage == aaType) {
1669 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1670 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1671 std::move(paint),
1672 viewMatrix,
1673 oval,
1674 startAngle,
1675 sweepAngle,
1676 useCenter,
1677 style,
1678 shaderCaps);
1679 if (op) {
1680 this->addDrawOp(clip, std::move(op));
1681 return;
1682 }
1683 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001684 }
Brian Salomone4949402018-04-26 15:22:04 -04001685 this->drawShapeUsingPathRenderer(
1686 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001687 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001688}
1689
Michael Ludwig7c12e282020-05-29 09:54:07 -04001690void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001691 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001692 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001693 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001694 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001695 sk_sp<GrColorSpaceXform> csxf,
1696 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001697 std::unique_ptr<SkLatticeIter> iter,
1698 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001699 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001700 RETURN_IF_ABANDONED
1701 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001702 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001703
Robert Phillips72152832017-01-25 17:31:35 -05001704 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001705
Brian Salomon2a943df2018-05-04 13:43:19 -04001706 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001707 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001708 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001709 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001710}
1711
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001712void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1713 const SkRect& bounds) {
1714 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1715 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001716 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001717}
1718
Brian Salomon031b0ba2019-05-23 11:05:26 -04001719void GrRenderTargetContext::asyncRescaleAndReadPixels(
1720 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1721 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1722 auto direct = fContext->priv().asDirectContext();
1723 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001724 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001725 return;
1726 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001727 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001728 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001729 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001730 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001731 if (this->asRenderTargetProxy()->framebufferOnly()) {
1732 callback(context, nullptr);
1733 return;
1734 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001736 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001737 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001738 return;
1739 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001740 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001741 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001742 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001743 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001744 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001745 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1746 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001747 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001748 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001749 backendFormatOfFinalContext, dstCT);
1750 // Fail if we can't read from the source surface's color type.
1751 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001752 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001753 return;
1754 }
Brian Salomon624f9062019-07-02 14:23:00 -04001755 // Fail if read color type does not have all of dstCT's color channels and those missing color
1756 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001757 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1758 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1759 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1760 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001761 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001762 return;
1763 }
1764
Brian Salomonbf6b9792019-08-21 09:38:10 -04001765 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001766 int x = srcRect.fLeft;
1767 int y = srcRect.fTop;
1768 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001769 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1770 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001771 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001772 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001773 return;
1774 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001775 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001776 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001777 x = y = 0;
1778 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001779 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1780 this->colorInfo().alphaType(),
1781 info.colorSpace(),
1782 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001783 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1784 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001785 // We flip or color convert by drawing and we don't currently support drawing to
1786 // kPremul.
1787 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001788 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001789 return;
1790 }
Greg Daniel40903af2020-01-30 14:55:05 -05001791 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001792 SkRect srcRectToDraw = SkRect::Make(srcRect);
1793 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001794 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001795 texProxyView =
1796 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1797 SkBackingFit::kApprox, SkBudgeted::kNo);
1798 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001799 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001800 return;
1801 }
Brian Salomonc5243782020-04-02 12:50:34 -04001802 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001803 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1804 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001805 tempRTC = GrRenderTargetContext::Make(
1806 direct, this->colorInfo().colorType(), info.refColorSpace(),
1807 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001808 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001809 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001810 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001811 return;
1812 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001813 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001814 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1815 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001816 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1817 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1818 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001819 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001820 }
1821 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001822 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001823 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1824 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001825}
1826
Brian Salomon9241a6d2019-10-03 13:26:54 -04001827class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1828public:
1829 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1830 ~AsyncReadResult() override {
1831 for (int i = 0; i < fPlanes.count(); ++i) {
1832 if (!fPlanes[i].fMappedBuffer) {
1833 delete[] static_cast<const char*>(fPlanes[i].fData);
1834 } else {
1835 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1836 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1837 }
1838 }
1839 }
1840
1841 int count() const override { return fPlanes.count(); }
1842 const void* data(int i) const override { return fPlanes[i].fData; }
1843 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1844
1845 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001846 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001847 size_t rowBytes,
1848 GrClientMappedBufferManager* manager) {
1849 SkASSERT(!result.fTransferBuffer->isMapped());
1850 const void* mappedData = result.fTransferBuffer->map();
1851 if (!mappedData) {
1852 return false;
1853 }
1854 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001855 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001856 result.fPixelConverter(convertedData.get(), mappedData);
1857 this->addCpuPlane(std::move(convertedData), rowBytes);
1858 result.fTransferBuffer->unmap();
1859 } else {
1860 manager->insert(result.fTransferBuffer);
1861 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1862 }
1863 return true;
1864 }
1865
1866 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1867 SkASSERT(data);
1868 SkASSERT(rowBytes > 0);
1869 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1870 }
1871
1872private:
1873 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1874 SkASSERT(data);
1875 SkASSERT(rowBytes > 0);
1876 SkASSERT(mappedBuffer);
1877 SkASSERT(mappedBuffer->isMapped());
1878 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1879 }
1880
1881 struct Plane {
1882 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1883 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1884 const void* fData;
1885 size_t fRowBytes;
1886 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1887 sk_sp<GrGpuBuffer> fMappedBuffer;
1888 };
1889 SkSTArray<3, Plane> fPlanes;
1890 uint32_t fInboxID;
1891};
1892
Brian Salomon024bd002019-06-11 11:38:16 -04001893void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1894 ReadPixelsCallback callback,
1895 ReadPixelsContext context) {
1896 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1897 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1898
Greg Danielca9dbe22020-01-02 13:44:30 -05001899 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001900 callback(context, nullptr);
1901 return;
1902 }
1903
Brian Salomon9241a6d2019-10-03 13:26:54 -04001904 auto directContext = fContext->priv().asDirectContext();
1905 SkASSERT(directContext);
1906 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1907
Brian Salomon024bd002019-06-11 11:38:16 -04001908 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1909
1910 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001911 auto ii = SkImageInfo::Make(rect.size(), colorType,
1912 this->colorInfo().alphaType(),
1913 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001914 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001915 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1916 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1917 result->addCpuPlane(std::move(data), pm.rowBytes());
1918
Brian Salomon1d435302019-07-01 13:05:28 -04001919 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001920 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001921 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001922 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001923 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001924 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001925 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001926
Brian Salomonab32f652019-05-10 14:24:50 -04001927 struct FinishContext {
1928 ReadPixelsCallback* fClientCallback;
1929 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001930 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001931 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001932 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001933 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001934 };
1935 // Assumption is that the caller would like to flush. We could take a parameter or require an
1936 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1937 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001938 auto* finishContext = new FinishContext{callback,
1939 context,
1940 rect.size(),
1941 colorType,
1942 mappedBufferManager,
1943 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001944 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001945 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001946 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001947 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1948 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1949 context->fMappedBufferManager)) {
1950 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001951 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001952 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001953 delete context;
1954 };
1955 GrFlushInfo flushInfo;
1956 flushInfo.fFinishedContext = finishContext;
1957 flushInfo.fFinishedProc = finishCallback;
Greg Daniel9efe3862020-06-11 11:51:06 -04001958 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001959}
1960
Brian Salomon9241a6d2019-10-03 13:26:54 -04001961void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1962 sk_sp<SkColorSpace> dstColorSpace,
1963 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001964 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001965 RescaleGamma rescaleGamma,
1966 SkFilterQuality rescaleQuality,
1967 ReadPixelsCallback callback,
1968 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001969 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1970 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001971 SkASSERT(!dstSize.isZero());
1972 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1973
Brian Salomon024bd002019-06-11 11:38:16 -04001974 auto direct = fContext->priv().asDirectContext();
1975 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001976 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001977 return;
1978 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001979 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001980 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001981 return;
1982 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001983 if (this->asRenderTargetProxy()->framebufferOnly()) {
1984 callback(context, nullptr);
1985 return;
1986 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001987 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001988 callback(context, nullptr);
1989 return;
1990 }
Brian Salomon024bd002019-06-11 11:38:16 -04001991 int x = srcRect.fLeft;
1992 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001993 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001994 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001995 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001996 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001997 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001998 dstColorSpace);
1999 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04002000 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
2001 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002002 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002003 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002004 return;
Brian Salomonab32f652019-05-10 14:24:50 -04002005 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002006 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04002007 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002008 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05002009 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04002010 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05002011 srcView = this->readSurfaceView();
2012 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04002013 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
2014 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
2015 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002016 // If we can't get a texture copy of the contents then give up.
2017 callback(context, nullptr);
2018 return;
2019 }
Brian Salomonc5243782020-04-02 12:50:34 -04002020 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002021 x = y = 0;
2022 }
Brian Salomon4d036892019-07-02 15:10:58 -04002023 // We assume the caller wants kPremul. There is no way to indicate a preference.
2024 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002025 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
2026 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04002027 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05002028 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
2029 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05002030 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
2031 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04002032 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002033 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002034 return;
2035 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002036 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05002037 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2038 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2039 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2040 SkMatrix::I(), std::move(xform));
2041 srcView = tempRTC->readSurfaceView();
2042 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002043 x = y = 0;
2044 }
2045 }
Greg Danielc594e622019-10-15 14:01:49 -04002046
Greg Daniele20fcad2020-01-08 11:52:34 -05002047 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2048 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2049 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002050 int halfW = dstSize.width()/2;
2051 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002052 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2053 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2054 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2055 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2056 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2057 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002058 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002059 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002060 return;
2061 }
2062
Brian Osman28ba5282019-10-30 14:18:07 -04002063 float baseM[20];
2064 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2065
Brian Salomon024bd002019-06-11 11:38:16 -04002066 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2067
Mike Reed1f607332020-05-21 12:11:27 -04002068 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002069
Brian Salomon9241a6d2019-10-03 13:26:54 -04002070 SkRect dstRectY = SkRect::Make(dstSize);
2071 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002072
Brian Salomon6aa65052020-01-28 12:16:53 -05002073 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2074 PixelTransferResult yTransfer, uTransfer, vTransfer;
2075
Brian Salomon024bd002019-06-11 11:38:16 -04002076 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2077 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002078 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002079 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002080 GrPaint yPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002081 auto yTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix);
2082 auto yColFP = GrColorMatrixFragmentProcessor::Make(std::move(yTexFP), yM,
2083 /*unpremulInput=*/false,
2084 /*clampRGBOutput=*/true,
2085 /*premulOutput=*/false);
2086 yPaint.addColorFragmentProcessor(std::move(yColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002087 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002088 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002089 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002090 if (!doSynchronousRead) {
2091 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2092 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2093 if (!yTransfer.fTransferBuffer) {
2094 callback(context, nullptr);
2095 return;
2096 }
Brian Salomon024bd002019-06-11 11:38:16 -04002097 }
2098
2099 texMatrix.preScale(2.f, 2.f);
2100 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2101 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002102 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002103 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002104 GrPaint uPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002105 auto uTexFP = GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix,
2106 GrSamplerState::Filter::kBilerp);
2107 auto uColFP = GrColorMatrixFragmentProcessor::Make(std::move(uTexFP), uM,
2108 /*unpremulInput=*/false,
2109 /*clampRGBOutput=*/true,
2110 /*premulOutput=*/false);
2111 uPaint.addColorFragmentProcessor(std::move(uColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002112 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002113 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002114 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002115 if (!doSynchronousRead) {
2116 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2117 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2118 if (!uTransfer.fTransferBuffer) {
2119 callback(context, nullptr);
2120 return;
2121 }
Brian Salomon024bd002019-06-11 11:38:16 -04002122 }
2123
2124 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2125 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002126 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002127 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002128 GrPaint vPaint;
John Stiles54cf42f2020-06-05 15:03:51 -04002129 auto vTexFP = GrTextureEffect::Make(std::move(srcView), this->colorInfo().alphaType(),
2130 texMatrix, GrSamplerState::Filter::kBilerp);
2131 auto vColFP = GrColorMatrixFragmentProcessor::Make(std::move(vTexFP), vM,
2132 /*unpremulInput=*/false,
2133 /*clampRGBOutput=*/true,
2134 /*premulOutput=*/false);
2135 vPaint.addColorFragmentProcessor(std::move(vColFP));
Brian Salomon024bd002019-06-11 11:38:16 -04002136 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002137 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002138 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002139 if (!doSynchronousRead) {
2140 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2141 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2142 if (!vTransfer.fTransferBuffer) {
2143 callback(context, nullptr);
2144 return;
2145 }
2146 }
2147
2148 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002149 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2150 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2151 size_t yRB = yInfo.minRowBytes();
2152 size_t uvRB = uvInfo.minRowBytes();
2153 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2154 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2155 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2156 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2157 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2158 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002159 callback(context, nullptr);
2160 return;
2161 }
2162 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002163 result->addCpuPlane(std::move(y), yRB );
2164 result->addCpuPlane(std::move(u), uvRB);
2165 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002166 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002167 return;
2168 }
2169
2170 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002171 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002172 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002173 GrClientMappedBufferManager* fMappedBufferManager;
2174 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002175 PixelTransferResult fYTransfer;
2176 PixelTransferResult fUTransfer;
2177 PixelTransferResult fVTransfer;
2178 };
2179 // Assumption is that the caller would like to flush. We could take a parameter or require an
2180 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2181 // callback to GrGpu until after the next flush that flushes our op list, though.
2182 auto* finishContext = new FinishContext{callback,
2183 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002184 direct->priv().clientMappedBufferManager(),
2185 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002186 std::move(yTransfer),
2187 std::move(uTransfer),
2188 std::move(vTransfer)};
2189 auto finishCallback = [](GrGpuFinishedContext c) {
2190 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002191 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002192 auto manager = context->fMappedBufferManager;
2193 size_t rowBytes = SkToSizeT(context->fSize.width());
2194 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2195 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002196 delete context;
2197 return;
2198 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002199 rowBytes /= 2;
2200 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2201 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2202 (*context->fClientCallback)(context->fClientContext, nullptr);
2203 delete context;
2204 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002205 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002206 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2207 (*context->fClientCallback)(context->fClientContext, nullptr);
2208 delete context;
2209 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002210 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002211 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002212 delete context;
2213 };
2214 GrFlushInfo flushInfo;
2215 flushInfo.fFinishedContext = finishContext;
2216 flushInfo.fFinishedProc = finishCallback;
Greg Daniel9efe3862020-06-11 11:51:06 -04002217 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo, nullptr);
Brian Salomonab32f652019-05-10 14:24:50 -04002218}
2219
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002220GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -04002221 const GrFlushInfo& info,
2222 const GrBackendSurfaceMutableState* newState) {
robertphillips8c523e02016-07-26 07:41:00 -07002223 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002224 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002225 if (info.fSubmittedProc) {
2226 info.fSubmittedProc(info.fSubmittedContext, false);
2227 }
2228 if (info.fFinishedProc) {
2229 info.fFinishedProc(info.fFinishedContext);
2230 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002231 return GrSemaphoresSubmitted::kNo;
2232 }
robertphillips8c523e02016-07-26 07:41:00 -07002233 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002234 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002235
Greg Daniel9efe3862020-06-11 11:51:06 -04002236 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info, newState);
Greg Daniela5cb7812017-06-16 09:45:32 -04002237}
2238
Greg Danielc64ee462017-06-15 16:59:49 -04002239bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002240 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002241 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002242 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002243 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002244 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002245
2246 AutoCheckFlush acf(this->drawingManager());
2247
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002248 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002249 return false;
2250 }
2251
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002252 auto direct = fContext->priv().asDirectContext();
2253 if (!direct) {
2254 return false;
2255 }
2256
2257 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002258
Greg Daniel301015c2019-11-18 14:06:46 -05002259 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2260 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002261 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002262 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002263 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2264 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002265 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002266 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2267 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002268 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002269}
joshualitt33a5fce2015-11-18 13:28:51 -08002270
Michael Ludwig7c12e282020-05-29 09:54:07 -04002271void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002272 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002273 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002274 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002275 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002276 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002277 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002278 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002279 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002280 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2281
Michael Ludwig2686d692020-04-17 20:21:37 +00002282 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002283
Robert Phillips27927a52018-08-20 13:18:12 -04002284 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002285}
2286
Michael Ludwig7c12e282020-05-29 09:54:07 -04002287void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002288 GrPaint&& paint,
2289 GrAA aa,
2290 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002291 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002292 ASSERT_SINGLE_OWNER
2293 RETURN_IF_ABANDONED
2294 SkDEBUGCODE(this->validate();)
2295 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2296
Brian Salomon2fad74a2017-12-20 13:28:55 -05002297 if (shape.isEmpty()) {
2298 if (shape.inverseFilled()) {
2299 this->drawPaint(clip, std::move(paint), viewMatrix);
2300 }
2301 return;
robertphillipsea461502015-05-26 11:38:03 -07002302 }
2303
Robert Phillips72152832017-01-25 17:31:35 -05002304 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002305
Brian Salomon2fad74a2017-12-20 13:28:55 -05002306 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002307 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002308 SkRRect rrect;
2309 // We can ignore the starting point and direction since there is no path effect.
2310 bool inverted;
2311 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2312 if (rrect.isRect()) {
2313 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2314 &shape.style());
2315 return;
2316 } else if (rrect.isOval()) {
2317 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002318 return;
2319 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002320 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2321 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002322 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2323 viewMatrix.rectStaysRect()) {
2324 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2325 // the matrix to all the points individually rather than just to the rect
2326 SkRect rects[2];
2327 if (shape.asNestedRects(rects)) {
2328 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002329 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002330 fContext, std::move(paint), viewMatrix, rects);
2331 if (op) {
2332 this->addDrawOp(clip, std::move(op));
2333 }
2334 // Returning here indicates that there is nothing to draw in this case.
2335 return;
2336 }
robertphillipsea461502015-05-26 11:38:03 -07002337 }
2338 }
robertphillips4bc31812016-03-01 12:22:49 -08002339
Michael Ludwig6a6de652020-04-30 20:16:36 -04002340 // If we get here in drawShape(), we definitely need to use path rendering
2341 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2342 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002343}
2344
Michael Ludwig7c12e282020-05-29 09:54:07 -04002345bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002346 const GrUserStencilSettings* ss,
2347 SkRegion::Op op,
2348 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002349 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002350 const SkMatrix& viewMatrix,
2351 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002352 ASSERT_SINGLE_OWNER_PRIV
2353 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002354 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002355 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2356 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002357
2358 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002359 GrPaint paint;
2360 paint.setCoverageSetOpXPFactory(op, invert);
2361 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2362 SkRect::MakeIWH(fRenderTargetContext->width(),
2363 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002364 return true;
2365 }
2366
Robert Phillips72152832017-01-25 17:31:35 -05002367 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002368
2369 // An Assumption here is that path renderer would use some form of tweaking
2370 // the src color (either the input alpha or in the frag shader) to implement
2371 // aa. If we have some future driver-mojo path AA that can do the right
2372 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002373 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002374 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002375
Michael Ludwig7c12e282020-05-29 09:54:07 -04002376 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002377
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002378 GrPaint paint;
2379 paint.setCoverageSetOpXPFactory(op, invert);
2380
Michael Ludwig2686d692020-04-17 20:21:37 +00002381 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002382 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002383 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002384 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002385 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002386 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002387 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002388 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002389 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002390 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2391 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002392 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002393
2394 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002395 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002396 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002397 if (!pr) {
2398 return false;
2399 }
2400
Brian Salomonf3569f02017-10-24 12:52:33 -04002401 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2402 std::move(paint),
2403 ss,
2404 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002405 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002406 &clipConservativeBounds,
2407 &viewMatrix,
2408 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002409 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002410 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002411 pr->drawPath(args);
2412 return true;
2413}
2414
Brian Osman11052242016-10-27 14:47:55 -04002415SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002416 ASSERT_SINGLE_OWNER_PRIV
2417
Robert Phillips6a6de562019-02-15 15:19:15 -05002418 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002419 return SkBudgeted::kNo;
2420 }
2421
Brian Osman11052242016-10-27 14:47:55 -04002422 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002423
Greg Daniel3912a4b2020-01-14 09:56:04 -05002424 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002425}
2426
Michael Ludwig7c12e282020-05-29 09:54:07 -04002427void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002428 GrPaint&& paint,
2429 GrAA aa,
2430 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002431 const GrStyledShape& originalShape,
2432 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002433 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002434 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002435 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2436
Jim Van Verthf86073a2018-10-02 13:05:38 -04002437 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2438 return;
2439 }
2440
Michael Ludwig6a6de652020-04-30 20:16:36 -04002441 if (attemptShapeFallback && originalShape.simplified()) {
2442 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2443 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2444 // we ought to try again instead of going right to path rendering.
2445 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2446 return;
2447 }
2448
Michael Ludwig7c12e282020-05-29 09:54:07 -04002449 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002450
Michael Ludwig2686d692020-04-17 20:21:37 +00002451 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002452 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002453
robertphillips68737822015-10-29 12:12:21 -07002454 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002455 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002456 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002457 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002458 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002459 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002460 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002461 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002462 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002463
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002464 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002465 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002466 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002467 return;
2468 }
2469
Chris Dalton6ce447a2019-06-23 18:07:38 -06002470 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002471
2472 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2473 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2474 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2475
Brian Salomon2fad74a2017-12-20 13:28:55 -05002476 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002477 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002478 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2479 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002480 return;
2481 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002482 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002483 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002484 }
2485 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002486 if (canDrawArgs.fShape->style().applies()) {
2487 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2488 styleScale);
2489 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002490 return;
2491 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002492 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002493 // This time, allow SW renderer
2494 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2495 } else {
2496 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002497#if GR_PATH_RENDERER_SPEW
2498 SkDebugf("falling back to: %s\n", pr->name());
2499#endif
bsalomon6663acf2016-05-10 09:14:17 -07002500 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002501 }
robertphillipsea461502015-05-26 11:38:03 -07002502
bsalomon8acedde2016-06-24 10:42:16 -07002503 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002504#ifdef SK_DEBUG
2505 SkDebugf("Unable to find path renderer compatible with path.\n");
2506#endif
2507 return;
2508 }
2509
Robert Phillips256c37b2017-03-01 14:32:46 -05002510 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002511 std::move(paint),
2512 &GrUserStencilSettings::kUnused,
2513 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002514 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002515 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002516 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002517 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002518 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002519 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002520 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002521}
2522
Brian Salomon467921e2017-03-06 16:17:12 -05002523static void op_bounds(SkRect* bounds, const GrOp* op) {
2524 *bounds = op->bounds();
2525 if (op->hasZeroArea()) {
2526 if (op->hasAABloat()) {
2527 bounds->outset(0.5f, 0.5f);
2528 } else {
2529 // We don't know which way the particular GPU will snap lines or points at integer
2530 // coords. So we ensure that the bounds is large enough for either snap.
2531 SkRect before = *bounds;
2532 bounds->roundOut(bounds);
2533 if (bounds->fLeft == before.fLeft) {
2534 bounds->fLeft -= 1;
2535 }
2536 if (bounds->fTop == before.fTop) {
2537 bounds->fTop -= 1;
2538 }
2539 if (bounds->fRight == before.fRight) {
2540 bounds->fRight += 1;
2541 }
2542 if (bounds->fBottom == before.fBottom) {
2543 bounds->fBottom += 1;
2544 }
2545 }
2546 }
2547}
2548
Chris Dalton08755122019-08-05 16:13:47 -06002549void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002550 GrDrawingManager* drawingMgr = this->drawingManager();
2551 this->getOpsTask()->addOp(drawingMgr,
2552 std::move(op), GrTextureResolveManager(drawingMgr), *this->caps());
Chris Dalton08755122019-08-05 16:13:47 -06002553}
2554
Michael Ludwig7c12e282020-05-29 09:54:07 -04002555void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002556 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002557 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002558 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002559 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002560 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002561 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002562 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002563 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002564 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002565
Brian Salomon467921e2017-03-06 16:17:12 -05002566 // Setup clip
2567 SkRect bounds;
2568 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002569 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002570 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002571 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002572 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002573
Chris Dalton9acf6822019-11-12 11:52:40 -07002574 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002575 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002576 }
2577
Michael Ludwig7c12e282020-05-29 09:54:07 -04002578 bool skipDraw = false;
2579 if (clip) {
2580 // Have a complex clip, so defer to its early clip culling
Michael Ludwigd8f4f422020-06-24 18:58:18 +00002581 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2582 skipDraw = true;
2583 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002584 } else {
2585 // No clipping, so just clip the bounds against the logical render target dimensions
Michael Ludwigd8f4f422020-06-24 18:58:18 +00002586 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2587 skipDraw = true;
2588 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04002589 }
2590
2591 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002592 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002593 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002594 }
2595
Chris Dalton9acf6822019-11-12 11:52:40 -07002596 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2597 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2598
2599 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2600 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2601 // that uses a stencil test when the stencil buffer is multisampled.)
2602 bool hasMixedSampledCoverage = (
2603 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002604 SkASSERT(!hasMixedSampledCoverage ||
2605 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002606
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002607 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002608 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002609 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2610
Michael Ludwig28e5f112020-06-09 10:57:24 -04002611 // Must be called before setDstProxyView so that it sees the final bounds of the op.
2612 op->setClippedBounds(bounds);
2613
Greg Daniel524e28b2019-11-01 11:48:53 -04002614 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002615 if (analysis.requiresDstTexture()) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002616 if (!this->setupDstProxyView(*op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002617 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002618 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002619 }
2620 }
2621
Greg Danielf41b2bd2019-08-22 16:19:24 -04002622 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002623 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002624 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002625 }
Adlai Hollerd71b7b02020-06-08 15:55:00 -04002626 opsTask->addDrawOp(this->drawingManager(), std::move(op), analysis, std::move(appliedClip),
2627 dstProxyView,GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002628}
2629
Michael Ludwig28e5f112020-06-09 10:57:24 -04002630bool GrRenderTargetContext::setupDstProxyView(const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002631 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002632 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2633 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2634 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002635 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002636 return false;
2637 }
2638
Greg Daniel3912a4b2020-01-14 09:56:04 -05002639 if (this->caps()->textureBarrierSupport() &&
2640 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002641 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002642 // The render target is a texture, so we can read from it directly in the shader. The XP
2643 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002644 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002645 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002646 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002647 }
2648 }
2649
Greg Daniel3155f7f2020-01-16 16:54:26 -05002650 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002651 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2652 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002653 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002654 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002655
Michael Ludwig28e5f112020-06-09 10:57:24 -04002656 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002657 if (!restrictions.fMustCopyWholeSrc) {
Michael Ludwig28e5f112020-06-09 10:57:24 -04002658 // If we don't need the whole source, restrict to the op's bounds. We add an extra pixel
2659 // of padding to account for AA bloat and the unpredictable rounding of coords near pixel
2660 // centers during rasterization.
2661 SkIRect conservativeDrawBounds = op.bounds().roundOut();
2662 conservativeDrawBounds.outset(1, 1);
2663 SkAssertResult(copyRect.intersect(conservativeDrawBounds));
Eric Karl74480882017-04-03 14:49:05 -07002664 }
Brian Salomon467921e2017-03-06 16:17:12 -05002665
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002666 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002667 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002668 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002669 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002670 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002671 } else {
Eric Karl74480882017-04-03 14:49:05 -07002672 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002673 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002674 }
Brian Salomonc5243782020-04-02 12:50:34 -04002675 auto copy =
2676 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2677 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2678 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002679
Brian Salomonc5243782020-04-02 12:50:34 -04002680 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002681 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002682 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002683}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002684
Greg Daniel573312e2020-02-07 17:22:35 -05002685bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002686 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002687 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002688 SkIRect clippedSrcRect;
2689 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002690 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2691 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002692 return false;
2693 }
2694
2695 GrPaint paint;
2696 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002697
Greg Danield2ccbb52020-02-05 10:45:39 -05002698 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002699 if (!fp) {
2700 return false;
2701 }
2702 paint.addColorFragmentProcessor(std::move(fp));
2703
2704 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002705 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002706 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2707 clippedSrcRect.height()),
2708 SkRect::Make(clippedSrcRect));
2709 return true;
2710}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002711
2712void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2713 SkASSERT(&task == fOpsTask.get());
2714 fOpsTask.reset();
2715}