blob: c9af7f8a696c4f0146ab98fc2c62e2a6e4f4e98f [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"
11#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
13#include "include/private/SkShadowFlags.h"
14#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040015#include "src/core/SkAutoPixmapStorage.h"
16#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkDrawShadowInfo.h"
18#include "src/core/SkGlyphRunPainter.h"
19#include "src/core/SkLatticeIter.h"
20#include "src/core/SkMatrixPriv.h"
21#include "src/core/SkRRectPriv.h"
22#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040023#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrBlurUtils.h"
27#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040028#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040029#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040031#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrDrawingManager.h"
33#include "src/gpu/GrFixedClip.h"
34#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040035#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrMemoryPool.h"
37#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040039#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrRenderTargetContextPriv.h"
41#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrStencilAttachment.h"
43#include "src/gpu/GrStyle.h"
44#include "src/gpu/GrTracing.h"
45#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040046#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040048#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040049#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000050#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040051#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/gpu/ops/GrAtlasTextOp.h"
53#include "src/gpu/ops/GrClearOp.h"
54#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050055#include "src/gpu/ops/GrDrawAtlasOp.h"
56#include "src/gpu/ops/GrDrawOp.h"
57#include "src/gpu/ops/GrDrawVerticesOp.h"
58#include "src/gpu/ops/GrDrawableOp.h"
59#include "src/gpu/ops/GrFillRRectOp.h"
60#include "src/gpu/ops/GrFillRectOp.h"
61#include "src/gpu/ops/GrLatticeOp.h"
62#include "src/gpu/ops/GrOp.h"
63#include "src/gpu/ops/GrOvalOpFactory.h"
64#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050065#include "src/gpu/ops/GrShadowRRectOp.h"
66#include "src/gpu/ops/GrStencilPathOp.h"
67#include "src/gpu/ops/GrStrokeRectOp.h"
68#include "src/gpu/ops/GrTextureOp.h"
69#include "src/gpu/text/GrTextContext.h"
70#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040071
Herb Derbyc1b482c2018-08-09 15:02:27 -040072class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040073public:
74 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040075 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040076 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040077 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040078 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040079
Robert Phillips7c525e62018-06-12 10:11:12 -040080 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040081 fRenderTargetContext->addDrawOp(clip, std::move(op));
82 }
83
Robert Phillips46a13382018-08-23 13:53:01 -040084 void drawShape(const GrClip& clip, const SkPaint& paint,
85 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040086 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
87 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040088 }
89
90 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040091 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050092 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040093 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040095 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040096 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040097 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040098 }
99 }
100
Robert Phillips69893702019-02-22 11:16:30 -0500101 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400102 return fRenderTargetContext->fContext;
103 }
104
Herb Derby65956872018-08-21 16:55:04 -0400105 SkGlyphRunListPainter* glyphPainter() override {
106 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400107 }
108
Brian Salomonf18b1d82017-10-27 11:30:49 -0400109private:
110 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400111 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400112
Brian Salomonf18b1d82017-10-27 11:30:49 -0400113};
joshualittbc907352016-01-13 06:45:40 -0800114
Robert Phillips72152832017-01-25 17:31:35 -0500115#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800116#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400117 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800118#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400119 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500120#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
121#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
122#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
123#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
124#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700125
Brian Salomone225b562017-06-14 13:00:03 -0400126//////////////////////////////////////////////////////////////////////////////
127
robertphillipsea461502015-05-26 11:38:03 -0700128class AutoCheckFlush {
129public:
halcanary9d524f22016-03-29 09:03:52 -0700130 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700131 SkASSERT(fDrawingManager);
132 }
bsalomonb77a9072016-09-07 10:02:04 -0700133 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700134
135private:
robertphillips77a2e522015-10-17 07:43:27 -0700136 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700137};
138
Greg Daniele20fcad2020-01-08 11:52:34 -0500139std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
140 GrRecordingContext* context,
141 GrColorType colorType,
142 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500143 sk_sp<GrSurfaceProxy> proxy,
144 GrSurfaceOrigin origin,
145 const SkSurfaceProps* surfaceProps,
146 bool managedOps) {
147 if (!proxy) {
148 return nullptr;
149 }
150
151 const GrBackendFormat& format = proxy->backendFormat();
152 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
153 GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(format, colorType);
154
Greg Daniel3912a4b2020-01-14 09:56:04 -0500155 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
156 GrSurfaceProxyView outputView(std::move(proxy), origin, outSwizzle);
157
158 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
159 std::move(outputView), colorType,
160 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500161}
162
163std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
164 GrRecordingContext* context,
165 GrColorType colorType,
166 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500167 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500168 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500169 const GrBackendFormat& format,
170 int sampleCnt,
171 GrMipMapped mipMapped,
172 GrProtected isProtected,
173 GrSurfaceOrigin origin,
174 SkBudgeted budgeted,
175 const SkSurfaceProps* surfaceProps) {
176 // It is probably not necessary to check if the context is abandoned here since uses of the
177 // GrRenderTargetContext which need the context will mostly likely fail later on without an
178 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
179 // an abandoned context correctly. It also lets us early out of some extra work.
180 if (context->priv().abandoned()) {
181 return nullptr;
182 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500183
Greg Daniel47c20e82020-01-21 14:29:57 -0500184 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, colorType);
185
Greg Daniele20fcad2020-01-08 11:52:34 -0500186 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500187 format, dimensions, swizzle, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
188 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500189 if (!proxy) {
190 return nullptr;
191 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500192
Greg Danielba0ff782020-01-07 15:42:57 -0500193 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
194 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500195 if (!rtc) {
196 return nullptr;
197 }
198 rtc->discard();
199 return rtc;
200}
201
202std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
203 GrRecordingContext* context,
204 GrColorType colorType,
205 sk_sp<SkColorSpace> colorSpace,
206 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500207 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500208 int sampleCnt,
209 GrMipMapped mipMapped,
210 GrProtected isProtected,
211 GrSurfaceOrigin origin,
212 SkBudgeted budgeted,
213 const SkSurfaceProps* surfaceProps) {
214 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
215 if (!format.isValid()) {
216 return nullptr;
217 }
218
219 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
220 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
221 surfaceProps);
222}
223
224static inline GrColorType color_type_fallback(GrColorType ct) {
225 switch (ct) {
226 // kRGBA_8888 is our default fallback for many color types that may not have renderable
227 // backend formats.
228 case GrColorType::kAlpha_8:
229 case GrColorType::kBGR_565:
230 case GrColorType::kABGR_4444:
231 case GrColorType::kBGRA_8888:
232 case GrColorType::kRGBA_1010102:
233 case GrColorType::kRGBA_F16:
234 case GrColorType::kRGBA_F16_Clamped:
235 return GrColorType::kRGBA_8888;
236 case GrColorType::kAlpha_F16:
237 return GrColorType::kRGBA_F16;
238 case GrColorType::kGray_8:
239 return GrColorType::kRGB_888x;
240 default:
241 return GrColorType::kUnknown;
242 }
243}
244
245std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
246 GrRecordingContext* context,
247 GrColorType colorType,
248 sk_sp<SkColorSpace> colorSpace,
249 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500250 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500251 int sampleCnt,
252 GrMipMapped mipMapped,
253 GrProtected isProtected,
254 GrSurfaceOrigin origin,
255 SkBudgeted budgeted,
256 const SkSurfaceProps* surfaceProps) {
257 std::unique_ptr<GrRenderTargetContext> rtc;
258 do {
259 rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
260 sampleCnt, mipMapped, isProtected, origin, budgeted,
261 surfaceProps);
262 colorType = color_type_fallback(colorType);
263 } while (!rtc && colorType != GrColorType::kUnknown);
264 return rtc;
265}
266
Greg Danielba0ff782020-01-07 15:42:57 -0500267std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
268 GrRecordingContext* context,
269 GrColorType colorType,
270 sk_sp<SkColorSpace> colorSpace,
271 const GrBackendTexture& tex,
272 int sampleCnt,
273 GrSurfaceOrigin origin,
274 const SkSurfaceProps* surfaceProps,
275 ReleaseProc releaseProc,
276 ReleaseContext releaseCtx) {
277 SkASSERT(sampleCnt > 0);
278 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel3a365112020-02-14 10:47:18 -0500279 tex, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
280 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500281 if (!proxy) {
282 return nullptr;
283 }
284
285 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
286 origin, surfaceProps);
287}
288
289std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
290 GrRecordingContext* context,
291 GrColorType colorType,
292 sk_sp<SkColorSpace> colorSpace,
293 const GrBackendTexture& tex,
294 int sampleCnt,
295 GrSurfaceOrigin origin,
296 const SkSurfaceProps* surfaceProps) {
297 SkASSERT(sampleCnt > 0);
298 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500299 tex, colorType, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500300 if (!proxy) {
301 return nullptr;
302 }
303
304 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
305 origin, surfaceProps);
306}
307
308std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
309 GrRecordingContext* context,
310 GrColorType colorType,
311 sk_sp<SkColorSpace> colorSpace,
312 const GrBackendRenderTarget& rt,
313 GrSurfaceOrigin origin,
314 const SkSurfaceProps* surfaceProps,
315 ReleaseProc releaseProc,
316 ReleaseContext releaseCtx) {
317 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500318 rt, colorType, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500319 if (!proxy) {
320 return nullptr;
321 }
322
323 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
324 origin, surfaceProps);
325}
326
327std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
328 GrRecordingContext* context,
329 const SkImageInfo& imageInfo,
330 const GrVkDrawableInfo& vkInfo,
331 const SkSurfaceProps* props) {
332 sk_sp<GrSurfaceProxy> proxy(
333 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
334 vkInfo));
335 if (!proxy) {
336 return nullptr;
337 }
338
Greg Danielba0ff782020-01-07 15:42:57 -0500339 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
340 imageInfo.refColorSpace(), std::move(proxy),
341 kTopLeft_GrSurfaceOrigin, props);
342}
343
Greg Danielf41b2bd2019-08-22 16:19:24 -0400344// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
345// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
346// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
347// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500348GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500349 GrSurfaceProxyView readView,
350 GrSurfaceProxyView outputView,
Brian Salomond6287472019-06-24 15:50:07 -0400351 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400352 sk_sp<SkColorSpace> colorSpace,
353 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400354 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500355 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
356 std::move(colorSpace))
357 , fOutputView(std::move(outputView))
358 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400359 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400360 , fManagedOpsTask(managedOpsTask) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500361 SkASSERT(this->asSurfaceProxy() == fOutputView.proxy());
362 SkASSERT(this->origin() == fOutputView.origin());
363
Brian Salomonf18b1d82017-10-27 11:30:49 -0400364 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700365 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700366}
367
robertphillips2e1e51f2015-10-15 08:01:48 -0700368#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500369void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400370 if (fOpsTask && !fOpsTask->isClosed()) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500371 SkASSERT(fOutputView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700372 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700373}
374#endif
375
Brian Osman11052242016-10-27 14:47:55 -0400376GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800377 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700378}
379
Chris Dalton7d6748e2019-03-13 00:34:52 -0600380inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600381 if (GrAA::kNo == aa) {
382 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
383 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600384 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600385 return GrAAType::kMSAA;
386 }
387 return GrAAType::kNone;
388 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600389 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600390}
391
Greg Daniele252f082017-10-23 16:05:23 -0400392GrMipMapped GrRenderTargetContext::mipMapped() const {
393 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
394 return proxy->mipMapped();
395 }
396 return GrMipMapped::kNo;
397}
398
Greg Danielf41b2bd2019-08-22 16:19:24 -0400399GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800400 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700401 SkDEBUGCODE(this->validate();)
402
Greg Danielf41b2bd2019-08-22 16:19:24 -0400403 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600404 sk_sp<GrOpsTask> newOpsTask =
Greg Daniel16f5c652019-10-29 11:26:01 -0400405 this->drawingManager()->newOpsTask(this->outputSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600406 if (fOpsTask && fNumStencilSamples > 0) {
407 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600408 fOpsTask->setMustPreserveStencil();
409 // Reload the stencil buffer content at the beginning of newOpsTask.
410 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
411 // values?
412 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
413 }
414 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700415 }
416
Greg Danielf41b2bd2019-08-22 16:19:24 -0400417 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700418}
419
Herb Derbycddab252018-07-16 11:19:04 -0400420void GrRenderTargetContext::drawGlyphRunList(
421 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400422 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800423 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700424 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700425 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400426 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700427
Greg Danielbe7fc462019-01-03 16:40:42 -0500428 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
429 // secondary command buffers because it would require stopping and starting a render pass which
430 // we don't have access to.
431 if (this->wrapsVkSecondaryCB()) {
432 return;
433 }
434
Herb Derby26cbe512018-05-24 14:39:01 -0400435 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400436 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400437 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700438}
439
Brian Osman11052242016-10-27 14:47:55 -0400440void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800441 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700442 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700443 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400444 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700445
Robert Phillips72152832017-01-25 17:31:35 -0500446 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400447
Greg Danielf41b2bd2019-08-22 16:19:24 -0400448 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700449}
450
Brian Osman11052242016-10-27 14:47:55 -0400451void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500452 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700453 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800454 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700455 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700456 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400457 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700458
Robert Phillips72152832017-01-25 17:31:35 -0500459 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700460 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
461 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700462}
robertphillips9199a9f2016-07-13 07:48:43 -0700463
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500464void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
465 const SkPMColor4f& color,
466 CanClearFullscreen canClearFullscreen) {
467 ASSERT_SINGLE_OWNER_PRIV
468 RETURN_IF_ABANDONED_PRIV
469 SkDEBUGCODE(fRenderTargetContext->validate();)
470 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
471 fRenderTargetContext->fContext);
472
473 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
474 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
475}
476
477static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
478 paint->setColor4f(color);
479 if (color.isOpaque()) {
480 // Can just rely on the src-over blend mode to do the right thing
481 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
482 } else {
483 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
484 // were src blended
485 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
486 }
487}
488
489void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
490 const SkPMColor4f& color,
491 CanClearFullscreen canClearFullscreen) {
492 bool isFull = false;
493 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400494 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
495 // only clear the entire target if we knew it had not been cleared before. As
496 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500497 isFull = !clip.scissorEnabled() ||
498 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400499 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500500 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
501 }
502
503 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400504 GrOpsTask* opsTask = this->getOpsTask();
505 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500506 !this->caps()->performColorClearsAsDraws()) {
507 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400508 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500509 return;
510 } else {
511 // Will use an op for the clear, reset the load op to discard since the op will
512 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400513 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500514 }
515
516 // Must add an op to the list (either because we couldn't use a load op, or because the
517 // clear load op isn't supported)
518 if (this->caps()->performColorClearsAsDraws()) {
519 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
520 GrPaint paint;
521 clear_to_grpaint(color, &paint);
522 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400523 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
524 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500525 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600526 this->addOp(GrClearOp::Make(
527 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500528 }
529 } else {
530 if (this->caps()->performPartialClearsAsDraws()) {
531 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500532 GrPaint paint;
533 clear_to_grpaint(color, &paint);
534
Michael Ludwig64b28a72019-05-28 12:02:00 -0400535 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400536 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
537 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500538 } else {
539 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
540 this->asSurfaceProxy()));
541 // This version of the clear op factory can return null if the clip doesn't intersect
542 // with the surface proxy's boundary
543 if (!op) {
544 return;
545 }
Chris Dalton08755122019-08-05 16:13:47 -0600546 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500547 }
548 }
549}
550
Brian Osman11052242016-10-27 14:47:55 -0400551void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500552 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400553 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000554 // Start with the render target, since that is the maximum content we could possibly fill.
555 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500556 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400557 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000558 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
559 // inverse view matrix.
560 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
561 } else {
562 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
563 SkMatrix localMatrix;
564 if (!viewMatrix.invert(&localMatrix)) {
565 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400566 }
Michael Ludwig61328202019-06-19 14:48:58 +0000567 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
568 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700569 }
robertphillipsea461502015-05-26 11:38:03 -0700570}
571
Michael Ludwig61328202019-06-19 14:48:58 +0000572enum class GrRenderTargetContext::QuadOptimization {
573 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
574 kDiscarded,
575 // The rect to draw was converted to some other op and appended to the oplist, so no additional
576 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
577 // a constColor is provided.
578 kSubmitted,
579 // The clip was folded into the device quad, with updated edge flags and local coords, and
580 // caller is responsible for adding an appropriate op.
581 kClipApplied,
582 // No change to clip, but quad updated to better fit clip/render target, and caller is
583 // responsible for adding an appropriate op.
584 kCropped
585};
Michael Ludwig61a16512019-01-15 11:15:13 -0500586
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400587static bool make_vertex_finite(float* value) {
588 if (SkScalarIsNaN(*value)) {
589 return false;
590 }
591
592 if (!SkScalarIsFinite(*value)) {
593 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
594 // left when calculating crops.
595 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
596 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
597 }
598
599 return true;
600}
601
Michael Ludwig61328202019-06-19 14:48:58 +0000602GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400603 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500604 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000605 // Optimization requirements:
606 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500607 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
608 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
609 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
610 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
611 // 5. kCropped in all other scenarios (although a crop may be a no-op)
612
613 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
614 // better to just keep the old flags instead of introducing mixed edge flags.
615 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000616
Michael Ludwige08b4432019-06-19 18:00:48 -0400617 SkRect rtRect;
618 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400619 // Must use size at which the rendertarget will ultimately be allocated so that stencil
620 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500621 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400622 } else {
623 // Use the logical size of the render target, which allows for "fullscreen" clears even if
624 // the render target has an approximate backing fit
625 rtRect = SkRect::MakeWH(this->width(), this->height());
626 }
627
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500628 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000629 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400630 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
631 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500632 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400633 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500634 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
635 !make_vertex_finite(quad->fDevice.ys() + i) ||
636 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400637 // Discard if we see a nan
638 return QuadOptimization::kDiscarded;
639 }
640 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500641 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400642 }
643 } else {
644 // CropToRect requires the quads to be finite. If they are not finite and we have local
645 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500646 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400647 return QuadOptimization::kDiscarded;
648 }
Brian Salomon7694b902019-06-18 21:00:21 +0000649 }
650
Michael Ludwig61328202019-06-19 14:48:58 +0000651 // If the quad is entirely off screen, it doesn't matter what the clip does
652 if (!rtRect.intersects(drawBounds)) {
653 return QuadOptimization::kDiscarded;
654 }
Brian Salomon7694b902019-06-18 21:00:21 +0000655
Michael Ludwig61328202019-06-19 14:48:58 +0000656 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
657 // the render target).
658 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400659 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
660 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
661 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
662 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000663 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000664 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000665 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
666 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000667 }
Brian Salomon7694b902019-06-18 21:00:21 +0000668 }
669
Michael Ludwig61328202019-06-19 14:48:58 +0000670 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
671 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400672 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000673 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
674 // of the render target and the clip rect)
675 SkRect clipBounds = rtRect;
676 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
677 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000678 }
679
Michael Ludwig61328202019-06-19 14:48:58 +0000680 if (clipRRect.isRect()) {
681 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500682 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700683 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500684 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000685 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500686 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000687 if (drawBounds.contains(rtRect)) {
688 // Fullscreen clear
689 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
690 return QuadOptimization::kSubmitted;
691 } else if (GrClip::IsPixelAligned(drawBounds) &&
692 drawBounds.width() > 256 && drawBounds.height() > 256) {
693 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
694 SkIRect scissorRect;
695 drawBounds.round(&scissorRect);
696 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
697 return QuadOptimization::kSubmitted;
698 }
699 }
700
701 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000702 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500703 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000704 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
705 // properly reflect the smooth edge of the clip.
706 *aa = GrAA::kYes;
707 }
708 // We intentionally do not downgrade AA here because we don't know if we need to
709 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
710 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
711
712 // deviceQuad is exactly the intersection of original quad and clip, so it can be
713 // drawn with no clip (submitted by caller)
714 return QuadOptimization::kClipApplied;
715 } else {
716 // The quads have been updated to better fit the clip bounds, but can't get rid of
717 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500718 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000719 return QuadOptimization::kCropped;
720 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700721 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000722 // Rounded corners and constant filled color (limit ourselves to solid colors because
723 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500724 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
725 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
726 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000727 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
728 // we can draw the rrect directly and ignore the edge flags
729 GrPaint paint;
730 clear_to_grpaint(*constColor, &paint);
731 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
732 clipRRect, GrStyle::SimpleFill());
733 return QuadOptimization::kSubmitted;
734 } else {
735 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500736 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000737 return QuadOptimization::kCropped;
738 }
Brian Salomon7694b902019-06-18 21:00:21 +0000739 }
Brian Salomon7694b902019-06-18 21:00:21 +0000740 }
741
Michael Ludwig61328202019-06-19 14:48:58 +0000742 // Crop the quad to the conservative bounds of the clip.
743 SkIRect clipDevBounds;
744 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
745 SkRect clipBounds = SkRect::Make(clipDevBounds);
746
747 // One final check for discarding, since we may have gone here directly due to a complex clip
748 if (!clipBounds.intersects(drawBounds)) {
749 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000750 }
751
Michael Ludwig61328202019-06-19 14:48:58 +0000752 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
753 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500754 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
755 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000756
757 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000758}
759
Michael Ludwig61328202019-06-19 14:48:58 +0000760void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500761 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500762 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500763 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400764 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000765 ASSERT_SINGLE_OWNER
766 RETURN_IF_ABANDONED
767 SkDEBUGCODE(this->validate();)
768 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500769
Michael Ludwig61328202019-06-19 14:48:58 +0000770 AutoCheckFlush acf(this->drawingManager());
771
772 SkPMColor4f* constColor = nullptr;
773 SkPMColor4f paintColor;
774 if (!ss && !paint.numCoverageFragmentProcessors() &&
775 paint.isConstantBlendedColor(&paintColor)) {
776 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
777 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500778 }
779
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500780 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000781 if (opt >= QuadOptimization::kClipApplied) {
782 // These optimizations require caller to add an op themselves
783 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
784 : clip;
785 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
786 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500787 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
788 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700789 }
Michael Ludwig61328202019-06-19 14:48:58 +0000790 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800791}
792
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000793void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400794 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500795 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000796 sk_sp<GrColorSpaceXform> textureXform,
797 GrSamplerState::Filter filter,
798 const SkPMColor4f& color,
799 SkBlendMode blendMode,
800 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500801 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000802 const SkRect* domain) {
803 ASSERT_SINGLE_OWNER
804 RETURN_IF_ABANDONED
805 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400806 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000807 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
808
809 AutoCheckFlush acf(this->drawingManager());
810
811 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
812 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500813 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000814
815 SkASSERT(opt != QuadOptimization::kSubmitted);
816 if (opt != QuadOptimization::kDiscarded) {
817 // And the texture op if not discarded
818 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
819 : clip;
820 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400821 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400822 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
823 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000824 // Use the provided domain, although hypothetically we could detect that the cropped local
825 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500826 this->addDrawOp(finalClip,
827 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
828 std::move(textureXform), filter, color, saturate,
829 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000830 }
831}
832
Brian Osman11052242016-10-27 14:47:55 -0400833void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500834 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500835 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400836 const SkMatrix& viewMatrix,
837 const SkRect& rect,
838 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700839 if (!style) {
840 style = &GrStyle::SimpleFill();
841 }
joshualitt1de610a2016-01-06 08:26:09 -0800842 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700843 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700844 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400845 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700846
bsalomon6663acf2016-05-10 09:14:17 -0700847 // Path effects should've been devolved to a path in SkGpuDevice
848 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700849
Robert Phillips72152832017-01-25 17:31:35 -0500850 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700851
bsalomon6663acf2016-05-10 09:14:17 -0700852 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400853 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000854 // Fills the rect, using rect as its own local coordinates
855 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500856 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700857 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
858 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
859 if ((!rect.width() || !rect.height()) &&
860 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
861 SkScalar r = stroke.getWidth() / 2;
862 // TODO: Move these stroke->fill fallbacks to GrShape?
863 switch (stroke.getJoin()) {
864 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500865 this->drawRect(
866 clip, std::move(paint), aa, viewMatrix,
867 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
868 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700869 return;
870 case SkPaint::kRound_Join:
871 // Raster draws nothing when both dimensions are empty.
872 if (rect.width() || rect.height()){
873 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500874 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
875 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700876 return;
877 }
878 case SkPaint::kBevel_Join:
879 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500880 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700881 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
882 &GrStyle::SimpleFill());
883 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500884 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700885 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
886 &GrStyle::SimpleFill());
887 }
888 return;
889 }
890 }
robertphillips44302392016-07-08 14:43:03 -0700891
Brian Salomonbaaf4392017-06-15 09:59:23 -0400892 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700893
Chris Dalton7d6748e2019-03-13 00:34:52 -0600894 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500895 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
896 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
897 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500898 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400899 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700900 return;
robertphillips4bc31812016-03-01 12:22:49 -0800901 }
robertphillips4bc31812016-03-01 12:22:49 -0800902 }
Mike Klein16885072018-12-11 09:54:31 -0500903 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500904 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700905}
906
Michael Ludwig69858532018-11-28 15:34:34 -0500907void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
908 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
909 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600910 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500911
912 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
913 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500914}
915
Robert Phillipsec2249f2016-11-09 08:54:35 -0500916int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500917 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400918 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500919}
920
Greg Danielf41b2bd2019-08-22 16:19:24 -0400921GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600922 ) const {
923#if GR_TEST_UTILS
924 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400925 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600926 }
927#endif
928 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
929 // would normally be overwritten. The one exception is if the render target context is marked as
930 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
931 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
932 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
933 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400934 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600935}
936
Chris Dalton858cf232019-10-14 16:20:00 -0600937void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600938 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600939 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600940 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600941
Chris Daltoneffee202019-07-01 22:28:03 -0600942 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600943 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500944 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600945 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400946 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600947 }
948 SkASSERT(numRequiredSamples > 0);
949
950 if (numRequiredSamples > fNumStencilSamples) {
951 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500952 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600953 }
Chris Dalton6b982802019-06-27 13:53:46 -0600954
Chris Dalton674f77a2019-09-30 20:49:39 -0600955 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600956 if (this->caps()->performStencilClearsAsDraws()) {
957 // There is a driver bug with clearing stencil. We must use an op to manually clear the
958 // stencil buffer before the op that required 'setNeedsStencil'.
959 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
960 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600961 this->getOpsTask()->setInitialStencilContent(
962 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600963 }
964 }
965}
966
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000967void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700968 ASSERT_SINGLE_OWNER_PRIV
969 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400970 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400971 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
972 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700973
Robert Phillips72152832017-01-25 17:31:35 -0500974 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400975
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500976 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
977}
978
979void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600980 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
981
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500982 if (this->caps()->performStencilClearsAsDraws()) {
983 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
984 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
985
986 // Configure the paint to have no impact on the color buffer
987 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400988 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400989 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
990 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500991 } else {
992 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -0500993 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500994 if (!op) {
995 return;
996 }
Chris Dalton08755122019-08-05 16:13:47 -0600997 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500998 }
robertphillips976f5f02016-06-03 10:59:20 -0700999}
1000
Chris Daltonbbfd5162017-11-07 13:35:22 -07001001void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001002 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001003 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001004 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001005 ASSERT_SINGLE_OWNER_PRIV
1006 RETURN_IF_ABANDONED_PRIV
1007 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001008 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1009 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001010
Brian Salomon467921e2017-03-06 16:17:12 -05001011 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1012 SkASSERT(path);
1013 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1014
1015 // FIXME: Use path bounds instead of this WAR once
1016 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1017 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1018
1019 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001020 GrAppliedHardClip appliedClip;
1021 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1022 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001023 return;
1024 }
1025
Robert Phillips7c525e62018-06-12 10:11:12 -04001026 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1027 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001028 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001029 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001030 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001031 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001032 if (!op) {
1033 return;
1034 }
Brian Salomon97180af2017-03-14 13:42:58 -04001035 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001036
Chris Daltoneffee202019-07-01 22:28:03 -06001037 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001038 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001039}
1040
Michael Ludwig379e4962019-12-06 13:21:26 -05001041void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1042 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001043 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001044 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1045 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001046 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001047 ASSERT_SINGLE_OWNER
1048 RETURN_IF_ABANDONED
1049 SkDEBUGCODE(this->validate();)
1050 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001051
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001052 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1053 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1054 AutoCheckFlush acf(this->drawingManager());
1055 GrAAType aaType = this->chooseAAType(aa);
1056 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1057 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1058 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001059 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1060 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001061}
1062
Brian Osman11052242016-10-27 14:47:55 -04001063void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001064 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001065 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001066 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001067 GrPrimitiveType* overridePrimType,
1068 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001069 ASSERT_SINGLE_OWNER
1070 RETURN_IF_ABANDONED
1071 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001072 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001073
1074 AutoCheckFlush acf(this->drawingManager());
1075
1076 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001077 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001078 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001079 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001080 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001081 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001082}
1083
1084///////////////////////////////////////////////////////////////////////////////
1085
Brian Osman4d92b892019-03-24 00:53:23 +00001086void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1087 GrPaint&& paint,
1088 const SkMatrix& viewMatrix,
1089 int spriteCount,
1090 const SkRSXform xform[],
1091 const SkRect texRect[],
1092 const SkColor colors[]) {
1093 ASSERT_SINGLE_OWNER
1094 RETURN_IF_ABANDONED
1095 SkDEBUGCODE(this->validate();)
1096 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1097
1098 AutoCheckFlush acf(this->drawingManager());
1099
1100 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1101 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1102 aaType, spriteCount, xform, texRect, colors);
1103 this->addDrawOp(clip, std::move(op));
1104}
1105
1106///////////////////////////////////////////////////////////////////////////////
1107
Brian Osman11052242016-10-27 14:47:55 -04001108void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001109 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001110 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001111 const SkMatrix& viewMatrix,
1112 const SkRRect& rrect,
1113 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001114 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001115 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001116 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001117 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001118
1119 const SkStrokeRec& stroke = style.strokeRec();
1120 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001121 return;
1122 }
1123
bsalomon7f0d9f32016-08-15 14:49:10 -07001124 GrNoClip noclip;
1125 const GrClip* clip = &origClip;
1126#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1127 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001128 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001129 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1130 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001131 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1132 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001133 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001134 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1135 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001136 clip = &noclip;
1137 }
1138#endif
bsalomon6663acf2016-05-10 09:14:17 -07001139 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001140
Robert Phillips72152832017-01-25 17:31:35 -05001141 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001142
Chris Dalton7d6748e2019-03-13 00:34:52 -06001143 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001144
Chris Dalton0dffbab2019-03-27 13:08:50 -06001145 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001146 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1147 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1148 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1149 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1150 // to avoid perf regressions on some platforms.
1151 assert_alive(paint);
1152 op = GrOvalOpFactory::MakeCircularRRectOp(
1153 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1154 }
1155 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001156 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001157 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001158 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001159 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001160 assert_alive(paint);
1161 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001162 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001163 }
1164 if (op) {
1165 this->addDrawOp(*clip, std::move(op));
1166 return;
robertphillipsea461502015-05-26 11:38:03 -07001167 }
robertphillipsb56f9272016-02-25 11:03:52 -08001168
Mike Klein16885072018-12-11 09:54:31 -05001169 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001170 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1171 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001172}
1173
Jim Van Verthc5903412016-11-17 15:27:09 -05001174///////////////////////////////////////////////////////////////////////////////
1175
Jim Van Verth3af1af92017-05-18 15:06:54 -04001176static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1177 SkPoint3 result;
1178 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1179 result.fZ = pt.fZ;
1180 return result;
1181}
1182
1183bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001184 const SkMatrix& viewMatrix,
1185 const SkPath& path,
1186 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001187 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001188 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001189 return true;
1190 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001191 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001192 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001193
1194 // check z plane
1195 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1196 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1197 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1198 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1199 return false;
1200 }
1201
1202 SkRRect rrect;
1203 SkRect rect;
1204 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001205 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001206 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1207 if (!isRRect &&
1208 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1209 rect.width() > SK_ScalarNearlyZero) {
1210 rrect.setOval(rect);
1211 isRRect = true;
1212 }
1213 if (!isRRect && path.isRect(&rect)) {
1214 rrect.setRect(rect);
1215 isRRect = true;
1216 }
1217
1218 if (!isRRect) {
1219 return false;
1220 }
1221
Jim Van Verthc5903412016-11-17 15:27:09 -05001222 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001223 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001224 }
1225
Robert Phillips72152832017-01-25 17:31:35 -05001226 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001227
Jim Van Verth3af1af92017-05-18 15:06:54 -04001228 // transform light
1229 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1230
1231 // 1/scale
1232 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001233 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001234 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1235 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1236
1237 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001238 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1239
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001240 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001241 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1242 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1243 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001244
1245 // Outset the shadow rrect to the border of the penumbra
1246 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1247 SkRRect ambientRRect;
1248 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1249 // If the rrect was an oval then its outset will also be one.
1250 // We set it explicitly to avoid errors.
1251 if (rrect.isOval()) {
1252 ambientRRect = SkRRect::MakeOval(outsetRect);
1253 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001254 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1256 }
1257
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001258 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001259 if (transparent) {
1260 // set a large inset to force a fill
1261 devSpaceInsetWidth = ambientRRect.width();
1262 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001263
Robert Phillips7c525e62018-06-12 10:11:12 -04001264 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1265 ambientColor,
1266 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001267 ambientRRect,
1268 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001269 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001270 if (op) {
1271 this->addDrawOp(clip, std::move(op));
1272 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001273 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001274
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001275 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001276 SkScalar devSpaceSpotBlur;
1277 SkScalar spotScale;
1278 SkVector spotOffset;
1279 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1280 devLightPos.fZ, rec.fLightRadius,
1281 &devSpaceSpotBlur, &spotScale, &spotOffset);
1282 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001283 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1284
Jim Van Verth3af1af92017-05-18 15:06:54 -04001285 // Adjust translate for the effect of the scale.
1286 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1287 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1288 // This offset is in dev space, need to transform it into source space.
1289 SkMatrix ctmInverse;
1290 if (viewMatrix.invert(&ctmInverse)) {
1291 ctmInverse.mapPoints(&spotOffset, 1);
1292 } else {
1293 // Since the matrix is a similarity, this should never happen, but just in case...
1294 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1295 SkASSERT(false);
1296 }
1297
1298 // Compute the transformed shadow rrect
1299 SkRRect spotShadowRRect;
1300 SkMatrix shadowTransform;
1301 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1302 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001303 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001304
1305 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001306 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001307 SkScalar insetWidth = blurOutset;
1308 if (transparent) {
1309 // If transparent, just do a fill
1310 insetWidth += spotShadowRRect.width();
1311 } else {
1312 // For shadows, instead of using a stroke we specify an inset from the penumbra
1313 // border. We want to extend this inset area so that it meets up with the caster
1314 // geometry. The inset geometry will by default already be inset by the blur width.
1315 //
1316 // We compare the min and max corners inset by the radius between the original
1317 // rrect and the shadow rrect. The distance between the two plus the difference
1318 // between the scaled radius and the original radius gives the distance from the
1319 // transformed shadow shape to the original shape in that corner. The max
1320 // of these gives the maximum distance we need to cover.
1321 //
1322 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1323 // that to get the full insetWidth.
1324 SkScalar maxOffset;
1325 if (rrect.isRect()) {
1326 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001327 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001328 rrect.rect().fLeft),
1329 SkTAbs(spotShadowRRect.rect().fTop -
1330 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001331 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001332 rrect.rect().fRight),
1333 SkTAbs(spotShadowRRect.rect().fBottom -
1334 rrect.rect().fBottom)));
1335 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001336 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001337 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1338 rrect.rect().fLeft + dr,
1339 spotShadowRRect.rect().fTop -
1340 rrect.rect().fTop + dr);
1341 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1342 rrect.rect().fRight - dr,
1343 spotShadowRRect.rect().fBottom -
1344 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001345 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001346 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001347 }
Brian Osman788b9162020-02-07 10:36:46 -05001348 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001349 }
1350
1351 // Outset the shadow rrect to the border of the penumbra
1352 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1353 if (spotShadowRRect.isOval()) {
1354 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1355 } else {
1356 SkScalar outsetRad = spotRadius + blurOutset;
1357 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1358 }
1359
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001360 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001361
Robert Phillips7c525e62018-06-12 10:11:12 -04001362 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1363 spotColor,
1364 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001365 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001366 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001367 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001368 if (op) {
1369 this->addDrawOp(clip, std::move(op));
1370 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001371 }
1372
1373 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001374}
1375
1376///////////////////////////////////////////////////////////////////////////////
1377
Brian Osman11052242016-10-27 14:47:55 -04001378bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001379 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001380 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001381 const SkMatrix& viewMatrix,
1382 const SkRRect& origOuter,
1383 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001384 SkASSERT(!origInner.isEmpty());
1385 SkASSERT(!origOuter.isEmpty());
1386
Brian Salomon65749212017-12-01 16:01:47 -05001387 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1388
Chris Dalton7d6748e2019-03-13 00:34:52 -06001389 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001390
1391 if (GrAAType::kMSAA == aaType) {
1392 return false;
1393 }
1394
Greg Daniel2655ede2019-04-10 00:49:28 +00001395 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1396 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001397 auto outerR = outer->width() / 2.f;
1398 auto innerR = inner->width() / 2.f;
1399 auto cx = outer->getBounds().fLeft + outerR;
1400 auto cy = outer->getBounds().fTop + outerR;
1401 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1402 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1403 auto avgR = (innerR + outerR) / 2.f;
1404 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1405 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1406 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001407 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001408 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001409 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001410 if (op) {
1411 this->addDrawOp(clip, std::move(op));
1412 return true;
1413 }
Mike Klein16885072018-12-11 09:54:31 -05001414 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001415 }
1416 }
1417
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001418 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001419 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001420 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1421 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001422 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001423 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1424 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 }
robertphillips00095892016-02-29 13:50:40 -08001426
robertphillips00095892016-02-29 13:50:40 -08001427 SkMatrix inverseVM;
1428 if (!viewMatrix.isIdentity()) {
1429 if (!origInner.transform(viewMatrix, inner.writable())) {
1430 return false;
1431 }
1432 if (!origOuter.transform(viewMatrix, outer.writable())) {
1433 return false;
1434 }
1435 if (!viewMatrix.invert(&inverseVM)) {
1436 return false;
1437 }
1438 } else {
1439 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001440 }
robertphillips00095892016-02-29 13:50:40 -08001441
Ethan Nicholaseace9352018-10-15 20:09:54 +00001442 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001443 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001444 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001445 if (!innerEffect) {
1446 return false;
1447 }
1448
Ethan Nicholaseace9352018-10-15 20:09:54 +00001449 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001450 if (!outerEffect) {
1451 return false;
1452 }
1453
Brian Salomon82f44312017-01-11 13:42:54 -05001454 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1455 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001456
1457 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001458 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001459 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1460 }
halcanary9d524f22016-03-29 09:03:52 -07001461
Brian Salomon82f44312017-01-11 13:42:54 -05001462 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1463 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001464 return true;
1465}
1466
Brian Osman11052242016-10-27 14:47:55 -04001467void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001468 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001469 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001470 const SkMatrix& viewMatrix,
1471 const SkRRect& outer,
1472 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001473 ASSERT_SINGLE_OWNER
1474 RETURN_IF_ABANDONED
1475 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001476 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001477
1478 SkASSERT(!outer.isEmpty());
1479 SkASSERT(!inner.isEmpty());
1480
Robert Phillips72152832017-01-25 17:31:35 -05001481 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001482
Brian Salomon82f44312017-01-11 13:42:54 -05001483 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001484 return;
1485 }
Mike Klein16885072018-12-11 09:54:31 -05001486 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001487
1488 SkPath path;
1489 path.setIsVolatile(true);
1490 path.addRRect(inner);
1491 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001492 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001493 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001494}
1495
robertphillipsea461502015-05-26 11:38:03 -07001496///////////////////////////////////////////////////////////////////////////////
1497
Brian Osman11052242016-10-27 14:47:55 -04001498void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001499 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001500 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001501 const SkMatrix& viewMatrix,
1502 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001503 const GrStyle& style,
1504 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001505 ASSERT_SINGLE_OWNER
1506 RETURN_IF_ABANDONED
1507 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001508 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001509
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001510 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001511 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001512 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001513 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001514 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1515 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001516 aa = GrAA::kNo;
1517 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001518 }
msarettcc319b92016-08-25 18:07:18 -07001519 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001520 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001521 SkPath path;
1522 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001523 path.setIsVolatile(true);
1524
Brian Salomon82f44312017-01-11 13:42:54 -05001525 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001526 }
1527
Chris Dalton7d6748e2019-03-13 00:34:52 -06001528 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001529 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1530 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001531 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001532}
1533
Brian Osman11052242016-10-27 14:47:55 -04001534void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001535 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001536 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001537 const SkMatrix& viewMatrix,
1538 const SkRect& oval,
1539 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001540 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001541 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001542 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001543 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001544
Robert Phillips7484d202018-07-03 09:09:08 -04001545 const SkStrokeRec& stroke = style.strokeRec();
1546
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001547 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001548 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1549 return;
1550 }
1551
1552 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001553 return;
robertphillipsea461502015-05-26 11:38:03 -07001554 }
1555
Robert Phillips72152832017-01-25 17:31:35 -05001556 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001557
Chris Dalton7d6748e2019-03-13 00:34:52 -06001558 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001559
1560 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001561 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1562 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001563 // We don't draw true circles as round rects in coverage mode, because it can
1564 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1565 assert_alive(paint);
1566 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1567 this->caps()->shaderCaps());
1568 }
1569 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001570 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1571 // the arc equation. This same special geometry and fragment branch also turn out to be a
1572 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1573 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1574 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001575 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001576 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1577 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001578 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001579 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001580 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001581 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1582 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001583 }
1584 if (op) {
1585 this->addDrawOp(clip, std::move(op));
1586 return;
robertphillipsea461502015-05-26 11:38:03 -07001587 }
robertphillipsb56f9272016-02-25 11:03:52 -08001588
Mike Klein16885072018-12-11 09:54:31 -05001589 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001590 this->drawShapeUsingPathRenderer(
1591 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001592 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001593}
1594
Brian Osman11052242016-10-27 14:47:55 -04001595void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001596 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001597 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001598 const SkMatrix& viewMatrix,
1599 const SkRect& oval,
1600 SkScalar startAngle,
1601 SkScalar sweepAngle,
1602 bool useCenter,
1603 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001604 ASSERT_SINGLE_OWNER
1605 RETURN_IF_ABANDONED
1606 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001607 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001608
1609 AutoCheckFlush acf(this->drawingManager());
1610
Chris Dalton7d6748e2019-03-13 00:34:52 -06001611 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001612 if (GrAAType::kCoverage == aaType) {
1613 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1614 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1615 std::move(paint),
1616 viewMatrix,
1617 oval,
1618 startAngle,
1619 sweepAngle,
1620 useCenter,
1621 style,
1622 shaderCaps);
1623 if (op) {
1624 this->addDrawOp(clip, std::move(op));
1625 return;
1626 }
1627 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001628 }
Brian Salomone4949402018-04-26 15:22:04 -04001629 this->drawShapeUsingPathRenderer(
1630 clip, std::move(paint), aa, viewMatrix,
1631 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001632}
1633
Brian Osman11052242016-10-27 14:47:55 -04001634void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001635 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001636 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001637 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001638 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001639 sk_sp<GrColorSpaceXform> csxf,
1640 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001641 std::unique_ptr<SkLatticeIter> iter,
1642 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001643 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001644 RETURN_IF_ABANDONED
1645 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001646 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001647
Robert Phillips72152832017-01-25 17:31:35 -05001648 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001649
Brian Salomon2a943df2018-05-04 13:43:19 -04001650 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001651 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001652 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001653 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001654}
1655
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001656void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1657 const SkRect& bounds) {
1658 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1659 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001660 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001661}
1662
Brian Salomon031b0ba2019-05-23 11:05:26 -04001663void GrRenderTargetContext::asyncRescaleAndReadPixels(
1664 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1665 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1666 auto direct = fContext->priv().asDirectContext();
1667 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001668 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001669 return;
1670 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001671 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001672 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001673 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001674 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001675 if (this->asRenderTargetProxy()->framebufferOnly()) {
1676 callback(context, nullptr);
1677 return;
1678 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001679 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001680 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001681 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001682 return;
1683 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001684 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001685 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001686 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001687 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001688 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001689 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1690 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001691 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001692 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001693 backendFormatOfFinalContext, dstCT);
1694 // Fail if we can't read from the source surface's color type.
1695 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001696 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001697 return;
1698 }
Brian Salomon624f9062019-07-02 14:23:00 -04001699 // Fail if read color type does not have all of dstCT's color channels and those missing color
1700 // channels are in the src.
1701 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1702 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001703 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon624f9062019-07-02 14:23:00 -04001704 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001705 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001706 return;
1707 }
1708
Brian Salomonbf6b9792019-08-21 09:38:10 -04001709 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001710 int x = srcRect.fLeft;
1711 int y = srcRect.fTop;
1712 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001713 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1714 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001715 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 return;
1717 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001718 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001719 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001720 x = y = 0;
1721 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001722 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1723 this->colorInfo().alphaType(),
1724 info.colorSpace(),
1725 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001726 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1727 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001728 // We flip or color convert by drawing and we don't currently support drawing to
1729 // kPremul.
1730 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001731 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001732 return;
1733 }
Greg Daniel40903af2020-01-30 14:55:05 -05001734 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 SkRect srcRectToDraw = SkRect::Make(srcRect);
1736 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001737 if (!texProxyView.asTextureProxy()) {
1738 texProxyView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
1739 this->origin(), this->colorInfo().colorType(),
1740 GrMipMapped::kNo, srcRect,
1741 SkBackingFit::kApprox, SkBudgeted::kNo);
1742 if (!texProxyView.asTextureProxy()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001743 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001744 return;
1745 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1747 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001748 tempRTC = GrRenderTargetContext::Make(
1749 direct, this->colorInfo().colorType(), info.refColorSpace(),
1750 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001751 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001752 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001753 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001754 return;
1755 }
Greg Daniel40903af2020-01-30 14:55:05 -05001756 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1757 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1758 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001759 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1760 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1761 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001762 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001763 }
1764 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001765 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001766 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1767 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001768}
1769
Brian Salomon9241a6d2019-10-03 13:26:54 -04001770class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1771public:
1772 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1773 ~AsyncReadResult() override {
1774 for (int i = 0; i < fPlanes.count(); ++i) {
1775 if (!fPlanes[i].fMappedBuffer) {
1776 delete[] static_cast<const char*>(fPlanes[i].fData);
1777 } else {
1778 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1779 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1780 }
1781 }
1782 }
1783
1784 int count() const override { return fPlanes.count(); }
1785 const void* data(int i) const override { return fPlanes[i].fData; }
1786 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1787
1788 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001789 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001790 size_t rowBytes,
1791 GrClientMappedBufferManager* manager) {
1792 SkASSERT(!result.fTransferBuffer->isMapped());
1793 const void* mappedData = result.fTransferBuffer->map();
1794 if (!mappedData) {
1795 return false;
1796 }
1797 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001798 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001799 result.fPixelConverter(convertedData.get(), mappedData);
1800 this->addCpuPlane(std::move(convertedData), rowBytes);
1801 result.fTransferBuffer->unmap();
1802 } else {
1803 manager->insert(result.fTransferBuffer);
1804 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1805 }
1806 return true;
1807 }
1808
1809 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1810 SkASSERT(data);
1811 SkASSERT(rowBytes > 0);
1812 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1813 }
1814
1815private:
1816 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1817 SkASSERT(data);
1818 SkASSERT(rowBytes > 0);
1819 SkASSERT(mappedBuffer);
1820 SkASSERT(mappedBuffer->isMapped());
1821 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1822 }
1823
1824 struct Plane {
1825 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1826 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1827 const void* fData;
1828 size_t fRowBytes;
1829 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1830 sk_sp<GrGpuBuffer> fMappedBuffer;
1831 };
1832 SkSTArray<3, Plane> fPlanes;
1833 uint32_t fInboxID;
1834};
1835
Brian Salomon024bd002019-06-11 11:38:16 -04001836void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1837 ReadPixelsCallback callback,
1838 ReadPixelsContext context) {
1839 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1840 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1841
Greg Danielca9dbe22020-01-02 13:44:30 -05001842 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001843 callback(context, nullptr);
1844 return;
1845 }
1846
Brian Salomon9241a6d2019-10-03 13:26:54 -04001847 auto directContext = fContext->priv().asDirectContext();
1848 SkASSERT(directContext);
1849 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1850
Brian Salomon024bd002019-06-11 11:38:16 -04001851 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1852
1853 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001854 auto ii = SkImageInfo::Make(rect.size(), colorType,
1855 this->colorInfo().alphaType(),
1856 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001857 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001858 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1859 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1860 result->addCpuPlane(std::move(data), pm.rowBytes());
1861
Brian Salomon1d435302019-07-01 13:05:28 -04001862 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001863 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001864 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001865 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001866 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001867 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001868 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001869
Brian Salomonab32f652019-05-10 14:24:50 -04001870 struct FinishContext {
1871 ReadPixelsCallback* fClientCallback;
1872 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001873 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001874 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001875 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001876 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001877 };
1878 // Assumption is that the caller would like to flush. We could take a parameter or require an
1879 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1880 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001881 auto* finishContext = new FinishContext{callback,
1882 context,
1883 rect.size(),
1884 colorType,
1885 mappedBufferManager,
1886 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001887 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001888 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001889 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001890 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1891 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1892 context->fMappedBufferManager)) {
1893 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001894 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001896 delete context;
1897 };
1898 GrFlushInfo flushInfo;
1899 flushInfo.fFinishedContext = finishContext;
1900 flushInfo.fFinishedProc = finishCallback;
1901 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1902}
1903
Brian Salomon9241a6d2019-10-03 13:26:54 -04001904void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1905 sk_sp<SkColorSpace> dstColorSpace,
1906 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001907 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001908 RescaleGamma rescaleGamma,
1909 SkFilterQuality rescaleQuality,
1910 ReadPixelsCallback callback,
1911 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001912 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1913 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001914 SkASSERT(!dstSize.isZero());
1915 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1916
Brian Salomon024bd002019-06-11 11:38:16 -04001917 auto direct = fContext->priv().asDirectContext();
1918 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001920 return;
1921 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001922 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001923 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001924 return;
1925 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001926 if (this->asRenderTargetProxy()->framebufferOnly()) {
1927 callback(context, nullptr);
1928 return;
1929 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001930 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001931 callback(context, nullptr);
1932 return;
1933 }
Brian Salomon024bd002019-06-11 11:38:16 -04001934 int x = srcRect.fLeft;
1935 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001937 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001938 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001939 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001940 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001941 dstColorSpace);
1942 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001943 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001944 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001945 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001946 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001947 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001948 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001949 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001950 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001951 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001952 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001953 srcView = this->readSurfaceView();
1954 if (!srcView.asTextureProxy()) {
1955 srcView = GrSurfaceProxy::Copy(fContext, fReadView.proxy(), this->origin(),
1956 this->colorInfo().colorType(), GrMipMapped::kNo,
1957 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1958 if (!srcView.asTextureProxy()) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001959 // If we can't get a texture copy of the contents then give up.
1960 callback(context, nullptr);
1961 return;
1962 }
1963 x = y = 0;
1964 }
Brian Salomon4d036892019-07-02 15:10:58 -04001965 // We assume the caller wants kPremul. There is no way to indicate a preference.
1966 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001967 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1968 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001969 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001970 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1971 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001972 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1973 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001974 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001975 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001976 return;
1977 }
Greg Daniel40903af2020-01-30 14:55:05 -05001978 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1979 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1980 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1981 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1982 SkMatrix::I(), std::move(xform));
1983 srcView = tempRTC->readSurfaceView();
1984 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001985 x = y = 0;
1986 }
1987 }
Greg Danielc594e622019-10-15 14:01:49 -04001988
Greg Daniele20fcad2020-01-08 11:52:34 -05001989 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1990 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1991 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001992 int halfW = dstSize.width()/2;
1993 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001994 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1995 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1996 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1997 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1998 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1999 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002000 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002001 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002002 return;
2003 }
2004
Brian Osman28ba5282019-10-30 14:18:07 -04002005 float baseM[20];
2006 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2007
Brian Salomon024bd002019-06-11 11:38:16 -04002008 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2009
2010 auto texMatrix = SkMatrix::MakeTrans(x, y);
2011
Brian Salomon9241a6d2019-10-03 13:26:54 -04002012 SkRect dstRectY = SkRect::Make(dstSize);
2013 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002014
Brian Salomon6aa65052020-01-28 12:16:53 -05002015 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2016 PixelTransferResult yTransfer, uTransfer, vTransfer;
2017
Brian Salomon024bd002019-06-11 11:38:16 -04002018 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2019 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002020 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002021 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002022 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002023 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002024 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002025 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2026 yPaint.addColorFragmentProcessor(std::move(yFP));
2027 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002028 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2029 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002030 if (!doSynchronousRead) {
2031 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2032 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2033 if (!yTransfer.fTransferBuffer) {
2034 callback(context, nullptr);
2035 return;
2036 }
Brian Salomon024bd002019-06-11 11:38:16 -04002037 }
2038
2039 texMatrix.preScale(2.f, 2.f);
2040 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2041 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002042 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002043 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002044 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002045 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002046 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002047 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2048 uPaint.addColorFragmentProcessor(std::move(uFP));
2049 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002050 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2051 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002052 if (!doSynchronousRead) {
2053 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2054 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2055 if (!uTransfer.fTransferBuffer) {
2056 callback(context, nullptr);
2057 return;
2058 }
Brian Salomon024bd002019-06-11 11:38:16 -04002059 }
2060
2061 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2062 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002063 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002064 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002065 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002066 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2067 this->colorInfo().alphaType(), texMatrix,
2068 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002069 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2070 vPaint.addColorFragmentProcessor(std::move(vFP));
2071 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002072 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2073 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002074 if (!doSynchronousRead) {
2075 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2076 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2077 if (!vTransfer.fTransferBuffer) {
2078 callback(context, nullptr);
2079 return;
2080 }
2081 }
2082
2083 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002084 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2085 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2086 size_t yRB = yInfo.minRowBytes();
2087 size_t uvRB = uvInfo.minRowBytes();
2088 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2089 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2090 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2091 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2092 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2093 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002094 callback(context, nullptr);
2095 return;
2096 }
2097 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002098 result->addCpuPlane(std::move(y), yRB );
2099 result->addCpuPlane(std::move(u), uvRB);
2100 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002101 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002102 return;
2103 }
2104
2105 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002106 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002107 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002108 GrClientMappedBufferManager* fMappedBufferManager;
2109 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002110 PixelTransferResult fYTransfer;
2111 PixelTransferResult fUTransfer;
2112 PixelTransferResult fVTransfer;
2113 };
2114 // Assumption is that the caller would like to flush. We could take a parameter or require an
2115 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2116 // callback to GrGpu until after the next flush that flushes our op list, though.
2117 auto* finishContext = new FinishContext{callback,
2118 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002119 direct->priv().clientMappedBufferManager(),
2120 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002121 std::move(yTransfer),
2122 std::move(uTransfer),
2123 std::move(vTransfer)};
2124 auto finishCallback = [](GrGpuFinishedContext c) {
2125 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002126 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002127 auto manager = context->fMappedBufferManager;
2128 size_t rowBytes = SkToSizeT(context->fSize.width());
2129 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2130 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002131 delete context;
2132 return;
2133 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002134 rowBytes /= 2;
2135 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2136 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2137 (*context->fClientCallback)(context->fClientContext, nullptr);
2138 delete context;
2139 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002140 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002141 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2142 (*context->fClientCallback)(context->fClientContext, nullptr);
2143 delete context;
2144 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002145 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002146 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002147 delete context;
2148 };
2149 GrFlushInfo flushInfo;
2150 flushInfo.fFinishedContext = finishContext;
2151 flushInfo.fFinishedProc = finishCallback;
2152 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002153}
2154
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002155GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2156 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002157 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002158 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002159 return GrSemaphoresSubmitted::kNo;
2160 }
robertphillips8c523e02016-07-26 07:41:00 -07002161 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002162 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002163
Greg Daniel55f040b2020-02-13 15:38:32 +00002164 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002165}
2166
Greg Danielc64ee462017-06-15 16:59:49 -04002167bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002168 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002169 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002170 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002171 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002172 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002173
2174 AutoCheckFlush acf(this->drawingManager());
2175
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002176 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002177 return false;
2178 }
2179
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002180 auto direct = fContext->priv().asDirectContext();
2181 if (!direct) {
2182 return false;
2183 }
2184
2185 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002186
Greg Daniel301015c2019-11-18 14:06:46 -05002187 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2188 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002189 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002190 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002191 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2192 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002193 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002194 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2195 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002196 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002197}
joshualitt33a5fce2015-11-18 13:28:51 -08002198
Brian Osman11052242016-10-27 14:47:55 -04002199void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002200 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002201 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002202 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002203 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002204 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002205 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002206 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002207 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002208 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2209
Brian Salomon40b77a62017-12-22 11:25:52 -05002210 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002211
Robert Phillips27927a52018-08-20 13:18:12 -04002212 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002213}
2214
2215void GrRenderTargetContext::drawShape(const GrClip& clip,
2216 GrPaint&& paint,
2217 GrAA aa,
2218 const SkMatrix& viewMatrix,
2219 const GrShape& shape) {
2220 ASSERT_SINGLE_OWNER
2221 RETURN_IF_ABANDONED
2222 SkDEBUGCODE(this->validate();)
2223 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2224
Brian Salomon2fad74a2017-12-20 13:28:55 -05002225 if (shape.isEmpty()) {
2226 if (shape.inverseFilled()) {
2227 this->drawPaint(clip, std::move(paint), viewMatrix);
2228 }
2229 return;
robertphillipsea461502015-05-26 11:38:03 -07002230 }
2231
Robert Phillips72152832017-01-25 17:31:35 -05002232 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002233
Brian Salomon2fad74a2017-12-20 13:28:55 -05002234 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002235 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002236 SkRRect rrect;
2237 // We can ignore the starting point and direction since there is no path effect.
2238 bool inverted;
2239 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2240 if (rrect.isRect()) {
2241 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2242 &shape.style());
2243 return;
2244 } else if (rrect.isOval()) {
2245 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002246 return;
2247 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002248 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2249 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002250 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2251 viewMatrix.rectStaysRect()) {
2252 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2253 // the matrix to all the points individually rather than just to the rect
2254 SkRect rects[2];
2255 if (shape.asNestedRects(rects)) {
2256 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002257 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002258 fContext, std::move(paint), viewMatrix, rects);
2259 if (op) {
2260 this->addDrawOp(clip, std::move(op));
2261 }
2262 // Returning here indicates that there is nothing to draw in this case.
2263 return;
2264 }
robertphillipsea461502015-05-26 11:38:03 -07002265 }
2266 }
robertphillips4bc31812016-03-01 12:22:49 -08002267
Brian Salomon2fad74a2017-12-20 13:28:55 -05002268 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002269}
2270
Chris Daltonbbfd5162017-11-07 13:35:22 -07002271bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002272 const GrUserStencilSettings* ss,
2273 SkRegion::Op op,
2274 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002275 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002276 const SkMatrix& viewMatrix,
2277 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002278 ASSERT_SINGLE_OWNER_PRIV
2279 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002280 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002281 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2282 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002283
2284 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002285 GrPaint paint;
2286 paint.setCoverageSetOpXPFactory(op, invert);
2287 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2288 SkRect::MakeIWH(fRenderTargetContext->width(),
2289 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002290 return true;
2291 }
2292
Robert Phillips72152832017-01-25 17:31:35 -05002293 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002294
2295 // An Assumption here is that path renderer would use some form of tweaking
2296 // the src color (either the input alpha or in the frag shader) to implement
2297 // aa. If we have some future driver-mojo path AA that can do the right
2298 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002299 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002300 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002301
Chris Daltondb91c6e2017-09-08 16:25:08 -06002302 SkIRect clipConservativeBounds;
2303 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2304 &clipConservativeBounds, nullptr);
2305
bsalomon8acedde2016-06-24 10:42:16 -07002306 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002307 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002308 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002309 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002310 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002311 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002312 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002313 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002314 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2315 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002316 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002317
2318 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002319 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002320 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002321 if (!pr) {
2322 return false;
2323 }
2324
2325 GrPaint paint;
2326 paint.setCoverageSetOpXPFactory(op, invert);
2327
Brian Salomonf3569f02017-10-24 12:52:33 -04002328 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2329 std::move(paint),
2330 ss,
2331 fRenderTargetContext,
2332 &clip,
2333 &clipConservativeBounds,
2334 &viewMatrix,
2335 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002336 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002337 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002338 pr->drawPath(args);
2339 return true;
2340}
2341
Brian Osman11052242016-10-27 14:47:55 -04002342SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002343 ASSERT_SINGLE_OWNER_PRIV
2344
Robert Phillips6a6de562019-02-15 15:19:15 -05002345 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002346 return SkBudgeted::kNo;
2347 }
2348
Brian Osman11052242016-10-27 14:47:55 -04002349 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002350
Greg Daniel3912a4b2020-01-14 09:56:04 -05002351 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002352}
2353
Brian Salomon2fad74a2017-12-20 13:28:55 -05002354void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2355 GrPaint&& paint,
2356 GrAA aa,
2357 const SkMatrix& viewMatrix,
2358 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002359 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002360 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002361 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2362
Jim Van Verthf86073a2018-10-02 13:05:38 -04002363 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2364 return;
2365 }
2366
Chris Daltondb91c6e2017-09-08 16:25:08 -06002367 SkIRect clipConservativeBounds;
2368 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2369
Brian Salomon2fad74a2017-12-20 13:28:55 -05002370 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002371 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002372
robertphillips68737822015-10-29 12:12:21 -07002373 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002374 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002375 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002376 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002377 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002378 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002379 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002380 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002381
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002382 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002383 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002384 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002385 return;
2386 }
2387
Chris Dalton6ce447a2019-06-23 18:07:38 -06002388 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002389
2390 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2391 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2392 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2393
Brian Salomon2fad74a2017-12-20 13:28:55 -05002394 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002395 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002396 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2397 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002398 return;
2399 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002400 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002401 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002402 }
2403 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002404 if (canDrawArgs.fShape->style().applies()) {
2405 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2406 styleScale);
2407 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002408 return;
2409 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002410 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002411 // This time, allow SW renderer
2412 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2413 } else {
2414 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002415 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002416 }
robertphillipsea461502015-05-26 11:38:03 -07002417
bsalomon8acedde2016-06-24 10:42:16 -07002418 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002419#ifdef SK_DEBUG
2420 SkDebugf("Unable to find path renderer compatible with path.\n");
2421#endif
2422 return;
2423 }
2424
Robert Phillips256c37b2017-03-01 14:32:46 -05002425 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002426 std::move(paint),
2427 &GrUserStencilSettings::kUnused,
2428 this,
2429 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002430 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002431 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002432 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002433 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002434 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002435 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002436}
2437
Brian Salomon467921e2017-03-06 16:17:12 -05002438static void op_bounds(SkRect* bounds, const GrOp* op) {
2439 *bounds = op->bounds();
2440 if (op->hasZeroArea()) {
2441 if (op->hasAABloat()) {
2442 bounds->outset(0.5f, 0.5f);
2443 } else {
2444 // We don't know which way the particular GPU will snap lines or points at integer
2445 // coords. So we ensure that the bounds is large enough for either snap.
2446 SkRect before = *bounds;
2447 bounds->roundOut(bounds);
2448 if (bounds->fLeft == before.fLeft) {
2449 bounds->fLeft -= 1;
2450 }
2451 if (bounds->fTop == before.fTop) {
2452 bounds->fTop -= 1;
2453 }
2454 if (bounds->fRight == before.fRight) {
2455 bounds->fRight += 1;
2456 }
2457 if (bounds->fBottom == before.fBottom) {
2458 bounds->fBottom += 1;
2459 }
2460 }
2461 }
2462}
2463
Chris Dalton08755122019-08-05 16:13:47 -06002464void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002465 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002466 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2467}
2468
Brian Salomon348a0372018-10-31 10:42:18 -04002469void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2470 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002471 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002472 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002473 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002474 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002475 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002476 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002477 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002478 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002479
Brian Salomon467921e2017-03-06 16:17:12 -05002480 // Setup clip
2481 SkRect bounds;
2482 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002483 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002484 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002485 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002486 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002487
Chris Dalton9acf6822019-11-12 11:52:40 -07002488 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002489 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002490 }
2491
Chris Dalton9acf6822019-11-12 11:52:40 -07002492 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002493 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002494 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002495 }
2496
Chris Dalton9acf6822019-11-12 11:52:40 -07002497 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2498 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2499
2500 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2501 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2502 // that uses a stencil test when the stencil buffer is multisampled.)
2503 bool hasMixedSampledCoverage = (
2504 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002505 SkASSERT(!hasMixedSampledCoverage ||
2506 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002507
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002508 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002509 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002510 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2511
Greg Daniel524e28b2019-11-01 11:48:53 -04002512 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002513 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002514 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002515 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002516 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002517 }
2518 }
2519
2520 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002521 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002522 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002523 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002524 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002525 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002526 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002527}
2528
Greg Daniel524e28b2019-11-01 11:48:53 -04002529bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2530 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002531 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2532 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2533 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002534 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002535 return false;
2536 }
2537
Greg Daniel3912a4b2020-01-14 09:56:04 -05002538 if (this->caps()->textureBarrierSupport() &&
2539 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002540 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002541 // The render target is a texture, so we can read from it directly in the shader. The XP
2542 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002543 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002544 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002545 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002546 }
2547 }
2548
Greg Daniel3912a4b2020-01-14 09:56:04 -05002549 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002550
Eric Karl74480882017-04-03 14:49:05 -07002551 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002552 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002553 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002554 SkRect opBounds = op.bounds();
2555 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2556 // 0.5 pixels.
2557 if (op.hasAABloat() || op.hasZeroArea()) {
2558 opBounds.outset(0.5f, 0.5f);
2559 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2560 // performance we may ignore the clip when the draw is entirely inside the clip is float
2561 // space but will hit pixels just outside the clip when actually rasterizing.
2562 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002563 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002564 }
2565 SkIRect opIBounds;
2566 opBounds.roundOut(&opIBounds);
2567 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002568#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002569 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002570#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002571 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002572 }
2573
Greg Daniel3155f7f2020-01-16 16:54:26 -05002574 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002575 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2576 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002577 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002578 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002579
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002580 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002581 copyRect = clippedRect;
2582 }
Brian Salomon467921e2017-03-06 16:17:12 -05002583
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002584 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002585 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002586 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002587 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002588 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002589 } else {
Eric Karl74480882017-04-03 14:49:05 -07002590 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002591 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002592 }
Greg Daniel40903af2020-01-30 14:55:05 -05002593 GrSurfaceProxyView newProxyView =
2594 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), colorType,
2595 GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2596 restrictions.fRectsMustMatch);
2597 SkASSERT(newProxyView.proxy());
Robert Phillipsbf25d432017-04-07 10:08:53 -04002598
Greg Daniel40903af2020-01-30 14:55:05 -05002599 dstProxyView->setProxyView(std::move(newProxyView));
Greg Daniel524e28b2019-11-01 11:48:53 -04002600 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002601 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002602}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002603
Greg Daniel573312e2020-02-07 17:22:35 -05002604bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002605 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002606 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002607 SkIRect clippedSrcRect;
2608 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002609 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2610 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002611 return false;
2612 }
2613
2614 GrPaint paint;
2615 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002616
Greg Danield2ccbb52020-02-05 10:45:39 -05002617 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002618 if (!fp) {
2619 return false;
2620 }
2621 paint.addColorFragmentProcessor(std::move(fp));
2622
2623 this->fillRectToRect(
2624 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2625 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2626 clippedSrcRect.height()),
2627 SkRect::Make(clippedSrcRect));
2628 return true;
2629}