blob: ada5a047f0f8415353592ebf55cee6e3a4e44a31 [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 Daniele20fcad2020-01-08 11:52:34 -0500184 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400185 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500186 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500187 if (!proxy) {
188 return nullptr;
189 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500190
Greg Danielba0ff782020-01-07 15:42:57 -0500191 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
192 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 if (!rtc) {
194 return nullptr;
195 }
196 rtc->discard();
197 return rtc;
198}
199
200std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
201 GrRecordingContext* context,
202 GrColorType colorType,
203 sk_sp<SkColorSpace> colorSpace,
204 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500205 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500206 int sampleCnt,
207 GrMipMapped mipMapped,
208 GrProtected isProtected,
209 GrSurfaceOrigin origin,
210 SkBudgeted budgeted,
211 const SkSurfaceProps* surfaceProps) {
212 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
213 if (!format.isValid()) {
214 return nullptr;
215 }
216
217 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
218 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
219 surfaceProps);
220}
221
222static inline GrColorType color_type_fallback(GrColorType ct) {
223 switch (ct) {
224 // kRGBA_8888 is our default fallback for many color types that may not have renderable
225 // backend formats.
226 case GrColorType::kAlpha_8:
227 case GrColorType::kBGR_565:
228 case GrColorType::kABGR_4444:
229 case GrColorType::kBGRA_8888:
230 case GrColorType::kRGBA_1010102:
231 case GrColorType::kRGBA_F16:
232 case GrColorType::kRGBA_F16_Clamped:
233 return GrColorType::kRGBA_8888;
234 case GrColorType::kAlpha_F16:
235 return GrColorType::kRGBA_F16;
236 case GrColorType::kGray_8:
237 return GrColorType::kRGB_888x;
238 default:
239 return GrColorType::kUnknown;
240 }
241}
242
243std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
244 GrRecordingContext* context,
245 GrColorType colorType,
246 sk_sp<SkColorSpace> colorSpace,
247 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500248 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500249 int sampleCnt,
250 GrMipMapped mipMapped,
251 GrProtected isProtected,
252 GrSurfaceOrigin origin,
253 SkBudgeted budgeted,
254 const SkSurfaceProps* surfaceProps) {
255 std::unique_ptr<GrRenderTargetContext> rtc;
256 do {
257 rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
258 sampleCnt, mipMapped, isProtected, origin, budgeted,
259 surfaceProps);
260 colorType = color_type_fallback(colorType);
261 } while (!rtc && colorType != GrColorType::kUnknown);
262 return rtc;
263}
264
Greg Danielba0ff782020-01-07 15:42:57 -0500265std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
266 GrRecordingContext* context,
267 GrColorType colorType,
268 sk_sp<SkColorSpace> colorSpace,
269 const GrBackendTexture& tex,
270 int sampleCnt,
271 GrSurfaceOrigin origin,
272 const SkSurfaceProps* surfaceProps,
273 ReleaseProc releaseProc,
274 ReleaseContext releaseCtx) {
275 SkASSERT(sampleCnt > 0);
276 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel3a365112020-02-14 10:47:18 -0500277 tex, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
278 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500279 if (!proxy) {
280 return nullptr;
281 }
282
283 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
284 origin, surfaceProps);
285}
286
287std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
288 GrRecordingContext* context,
289 GrColorType colorType,
290 sk_sp<SkColorSpace> colorSpace,
291 const GrBackendTexture& tex,
292 int sampleCnt,
293 GrSurfaceOrigin origin,
294 const SkSurfaceProps* surfaceProps) {
295 SkASSERT(sampleCnt > 0);
296 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500297 tex, colorType, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500298 if (!proxy) {
299 return nullptr;
300 }
301
302 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
303 origin, surfaceProps);
304}
305
306std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
307 GrRecordingContext* context,
308 GrColorType colorType,
309 sk_sp<SkColorSpace> colorSpace,
310 const GrBackendRenderTarget& rt,
311 GrSurfaceOrigin origin,
312 const SkSurfaceProps* surfaceProps,
313 ReleaseProc releaseProc,
314 ReleaseContext releaseCtx) {
315 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendRenderTarget(
Greg Daniel3a365112020-02-14 10:47:18 -0500316 rt, colorType, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500317 if (!proxy) {
318 return nullptr;
319 }
320
321 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
322 origin, surfaceProps);
323}
324
325std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
326 GrRecordingContext* context,
327 const SkImageInfo& imageInfo,
328 const GrVkDrawableInfo& vkInfo,
329 const SkSurfaceProps* props) {
330 sk_sp<GrSurfaceProxy> proxy(
331 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
332 vkInfo));
333 if (!proxy) {
334 return nullptr;
335 }
336
Greg Danielba0ff782020-01-07 15:42:57 -0500337 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
338 imageInfo.refColorSpace(), std::move(proxy),
339 kTopLeft_GrSurfaceOrigin, props);
340}
341
Greg Danielf41b2bd2019-08-22 16:19:24 -0400342// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
343// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
344// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
345// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500346GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500347 GrSurfaceProxyView readView,
348 GrSurfaceProxyView outputView,
Brian Salomond6287472019-06-24 15:50:07 -0400349 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400350 sk_sp<SkColorSpace> colorSpace,
351 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400352 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500353 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
354 std::move(colorSpace))
355 , fOutputView(std::move(outputView))
356 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400357 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400358 , fManagedOpsTask(managedOpsTask) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500359 SkASSERT(this->asSurfaceProxy() == fOutputView.proxy());
360 SkASSERT(this->origin() == fOutputView.origin());
361
Brian Salomonf18b1d82017-10-27 11:30:49 -0400362 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700363 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700364}
365
robertphillips2e1e51f2015-10-15 08:01:48 -0700366#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500367void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368 if (fOpsTask && !fOpsTask->isClosed()) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 SkASSERT(fOutputView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700370 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700371}
372#endif
373
Brian Osman11052242016-10-27 14:47:55 -0400374GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800375 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700376}
377
Chris Dalton7d6748e2019-03-13 00:34:52 -0600378inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600379 if (GrAA::kNo == aa) {
380 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
381 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600382 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600383 return GrAAType::kMSAA;
384 }
385 return GrAAType::kNone;
386 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600387 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600388}
389
Greg Daniele252f082017-10-23 16:05:23 -0400390GrMipMapped GrRenderTargetContext::mipMapped() const {
391 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
392 return proxy->mipMapped();
393 }
394 return GrMipMapped::kNo;
395}
396
Greg Danielf41b2bd2019-08-22 16:19:24 -0400397GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800398 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700399 SkDEBUGCODE(this->validate();)
400
Greg Danielf41b2bd2019-08-22 16:19:24 -0400401 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600402 sk_sp<GrOpsTask> newOpsTask =
Greg Daniel16f5c652019-10-29 11:26:01 -0400403 this->drawingManager()->newOpsTask(this->outputSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600404 if (fOpsTask && fNumStencilSamples > 0) {
405 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600406 fOpsTask->setMustPreserveStencil();
407 // Reload the stencil buffer content at the beginning of newOpsTask.
408 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
409 // values?
410 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
411 }
412 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700413 }
414
Greg Danielf41b2bd2019-08-22 16:19:24 -0400415 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700416}
417
Herb Derbycddab252018-07-16 11:19:04 -0400418void GrRenderTargetContext::drawGlyphRunList(
419 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400420 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800421 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700422 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700423 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400424 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700425
Greg Danielbe7fc462019-01-03 16:40:42 -0500426 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
427 // secondary command buffers because it would require stopping and starting a render pass which
428 // we don't have access to.
429 if (this->wrapsVkSecondaryCB()) {
430 return;
431 }
432
Herb Derby26cbe512018-05-24 14:39:01 -0400433 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400434 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400435 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700436}
437
Brian Osman11052242016-10-27 14:47:55 -0400438void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800439 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700440 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700441 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400442 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700443
Robert Phillips72152832017-01-25 17:31:35 -0500444 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400445
Greg Danielf41b2bd2019-08-22 16:19:24 -0400446 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700447}
448
Brian Osman11052242016-10-27 14:47:55 -0400449void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500450 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700451 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800452 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700453 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700454 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700456
Robert Phillips72152832017-01-25 17:31:35 -0500457 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700458 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
459 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700460}
robertphillips9199a9f2016-07-13 07:48:43 -0700461
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500462void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
463 const SkPMColor4f& color,
464 CanClearFullscreen canClearFullscreen) {
465 ASSERT_SINGLE_OWNER_PRIV
466 RETURN_IF_ABANDONED_PRIV
467 SkDEBUGCODE(fRenderTargetContext->validate();)
468 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
469 fRenderTargetContext->fContext);
470
471 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
472 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
473}
474
475static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
476 paint->setColor4f(color);
477 if (color.isOpaque()) {
478 // Can just rely on the src-over blend mode to do the right thing
479 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
480 } else {
481 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
482 // were src blended
483 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
484 }
485}
486
487void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
488 const SkPMColor4f& color,
489 CanClearFullscreen canClearFullscreen) {
490 bool isFull = false;
491 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400492 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
493 // only clear the entire target if we knew it had not been cleared before. As
494 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500495 isFull = !clip.scissorEnabled() ||
496 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400497 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500498 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
499 }
500
501 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400502 GrOpsTask* opsTask = this->getOpsTask();
503 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500504 !this->caps()->performColorClearsAsDraws()) {
505 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400506 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500507 return;
508 } else {
509 // Will use an op for the clear, reset the load op to discard since the op will
510 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400511 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500512 }
513
514 // Must add an op to the list (either because we couldn't use a load op, or because the
515 // clear load op isn't supported)
516 if (this->caps()->performColorClearsAsDraws()) {
517 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
518 GrPaint paint;
519 clear_to_grpaint(color, &paint);
520 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400521 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
522 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500523 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600524 this->addOp(GrClearOp::Make(
525 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500526 }
527 } else {
528 if (this->caps()->performPartialClearsAsDraws()) {
529 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500530 GrPaint paint;
531 clear_to_grpaint(color, &paint);
532
Michael Ludwig64b28a72019-05-28 12:02:00 -0400533 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400534 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
535 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 } else {
537 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
538 this->asSurfaceProxy()));
539 // This version of the clear op factory can return null if the clip doesn't intersect
540 // with the surface proxy's boundary
541 if (!op) {
542 return;
543 }
Chris Dalton08755122019-08-05 16:13:47 -0600544 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500545 }
546 }
547}
548
Brian Osman11052242016-10-27 14:47:55 -0400549void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500550 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400551 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000552 // Start with the render target, since that is the maximum content we could possibly fill.
553 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500554 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400555 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000556 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
557 // inverse view matrix.
558 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
559 } else {
560 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
561 SkMatrix localMatrix;
562 if (!viewMatrix.invert(&localMatrix)) {
563 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400564 }
Michael Ludwig61328202019-06-19 14:48:58 +0000565 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
566 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700567 }
robertphillipsea461502015-05-26 11:38:03 -0700568}
569
Michael Ludwig61328202019-06-19 14:48:58 +0000570enum class GrRenderTargetContext::QuadOptimization {
571 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
572 kDiscarded,
573 // The rect to draw was converted to some other op and appended to the oplist, so no additional
574 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
575 // a constColor is provided.
576 kSubmitted,
577 // The clip was folded into the device quad, with updated edge flags and local coords, and
578 // caller is responsible for adding an appropriate op.
579 kClipApplied,
580 // No change to clip, but quad updated to better fit clip/render target, and caller is
581 // responsible for adding an appropriate op.
582 kCropped
583};
Michael Ludwig61a16512019-01-15 11:15:13 -0500584
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400585static bool make_vertex_finite(float* value) {
586 if (SkScalarIsNaN(*value)) {
587 return false;
588 }
589
590 if (!SkScalarIsFinite(*value)) {
591 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
592 // left when calculating crops.
593 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
594 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
595 }
596
597 return true;
598}
599
Michael Ludwig61328202019-06-19 14:48:58 +0000600GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400601 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500602 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000603 // Optimization requirements:
604 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500605 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
606 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
607 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
608 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
609 // 5. kCropped in all other scenarios (although a crop may be a no-op)
610
611 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
612 // better to just keep the old flags instead of introducing mixed edge flags.
613 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000614
Michael Ludwige08b4432019-06-19 18:00:48 -0400615 SkRect rtRect;
616 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400617 // Must use size at which the rendertarget will ultimately be allocated so that stencil
618 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500619 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400620 } else {
621 // Use the logical size of the render target, which allows for "fullscreen" clears even if
622 // the render target has an approximate backing fit
623 rtRect = SkRect::MakeWH(this->width(), this->height());
624 }
625
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500626 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000627 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400628 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
629 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500630 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400631 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500632 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
633 !make_vertex_finite(quad->fDevice.ys() + i) ||
634 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400635 // Discard if we see a nan
636 return QuadOptimization::kDiscarded;
637 }
638 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500639 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400640 }
641 } else {
642 // CropToRect requires the quads to be finite. If they are not finite and we have local
643 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500644 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400645 return QuadOptimization::kDiscarded;
646 }
Brian Salomon7694b902019-06-18 21:00:21 +0000647 }
648
Michael Ludwig61328202019-06-19 14:48:58 +0000649 // If the quad is entirely off screen, it doesn't matter what the clip does
650 if (!rtRect.intersects(drawBounds)) {
651 return QuadOptimization::kDiscarded;
652 }
Brian Salomon7694b902019-06-18 21:00:21 +0000653
Michael Ludwig61328202019-06-19 14:48:58 +0000654 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
655 // the render target).
656 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400657 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
658 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
659 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
660 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000661 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000662 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000663 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
664 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000665 }
Brian Salomon7694b902019-06-18 21:00:21 +0000666 }
667
Michael Ludwig61328202019-06-19 14:48:58 +0000668 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
669 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400670 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000671 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
672 // of the render target and the clip rect)
673 SkRect clipBounds = rtRect;
674 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
675 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000676 }
677
Michael Ludwig61328202019-06-19 14:48:58 +0000678 if (clipRRect.isRect()) {
679 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500680 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700681 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500682 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000683 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500684 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000685 if (drawBounds.contains(rtRect)) {
686 // Fullscreen clear
687 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
688 return QuadOptimization::kSubmitted;
689 } else if (GrClip::IsPixelAligned(drawBounds) &&
690 drawBounds.width() > 256 && drawBounds.height() > 256) {
691 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
692 SkIRect scissorRect;
693 drawBounds.round(&scissorRect);
694 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
695 return QuadOptimization::kSubmitted;
696 }
697 }
698
699 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000700 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500701 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000702 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
703 // properly reflect the smooth edge of the clip.
704 *aa = GrAA::kYes;
705 }
706 // We intentionally do not downgrade AA here because we don't know if we need to
707 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
708 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
709
710 // deviceQuad is exactly the intersection of original quad and clip, so it can be
711 // drawn with no clip (submitted by caller)
712 return QuadOptimization::kClipApplied;
713 } else {
714 // The quads have been updated to better fit the clip bounds, but can't get rid of
715 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500716 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000717 return QuadOptimization::kCropped;
718 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700719 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000720 // Rounded corners and constant filled color (limit ourselves to solid colors because
721 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500722 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
723 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
724 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000725 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
726 // we can draw the rrect directly and ignore the edge flags
727 GrPaint paint;
728 clear_to_grpaint(*constColor, &paint);
729 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
730 clipRRect, GrStyle::SimpleFill());
731 return QuadOptimization::kSubmitted;
732 } else {
733 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500734 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000735 return QuadOptimization::kCropped;
736 }
Brian Salomon7694b902019-06-18 21:00:21 +0000737 }
Brian Salomon7694b902019-06-18 21:00:21 +0000738 }
739
Michael Ludwig61328202019-06-19 14:48:58 +0000740 // Crop the quad to the conservative bounds of the clip.
741 SkIRect clipDevBounds;
742 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
743 SkRect clipBounds = SkRect::Make(clipDevBounds);
744
745 // One final check for discarding, since we may have gone here directly due to a complex clip
746 if (!clipBounds.intersects(drawBounds)) {
747 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000748 }
749
Michael Ludwig61328202019-06-19 14:48:58 +0000750 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
751 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500752 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
753 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000754
755 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000756}
757
Michael Ludwig61328202019-06-19 14:48:58 +0000758void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500759 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500760 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500761 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400762 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000763 ASSERT_SINGLE_OWNER
764 RETURN_IF_ABANDONED
765 SkDEBUGCODE(this->validate();)
766 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500767
Michael Ludwig61328202019-06-19 14:48:58 +0000768 AutoCheckFlush acf(this->drawingManager());
769
770 SkPMColor4f* constColor = nullptr;
771 SkPMColor4f paintColor;
772 if (!ss && !paint.numCoverageFragmentProcessors() &&
773 paint.isConstantBlendedColor(&paintColor)) {
774 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
775 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500776 }
777
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500778 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000779 if (opt >= QuadOptimization::kClipApplied) {
780 // These optimizations require caller to add an op themselves
781 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
782 : clip;
783 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
784 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500785 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
786 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700787 }
Michael Ludwig61328202019-06-19 14:48:58 +0000788 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800789}
790
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000791void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400792 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500793 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000794 sk_sp<GrColorSpaceXform> textureXform,
795 GrSamplerState::Filter filter,
796 const SkPMColor4f& color,
797 SkBlendMode blendMode,
798 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500799 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000800 const SkRect* domain) {
801 ASSERT_SINGLE_OWNER
802 RETURN_IF_ABANDONED
803 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400804 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000805 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
806
807 AutoCheckFlush acf(this->drawingManager());
808
809 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
810 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500811 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000812
813 SkASSERT(opt != QuadOptimization::kSubmitted);
814 if (opt != QuadOptimization::kDiscarded) {
815 // And the texture op if not discarded
816 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
817 : clip;
818 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400819 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400820 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
821 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000822 // Use the provided domain, although hypothetically we could detect that the cropped local
823 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500824 this->addDrawOp(finalClip,
825 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
826 std::move(textureXform), filter, color, saturate,
827 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000828 }
829}
830
Brian Osman11052242016-10-27 14:47:55 -0400831void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500832 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500833 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400834 const SkMatrix& viewMatrix,
835 const SkRect& rect,
836 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700837 if (!style) {
838 style = &GrStyle::SimpleFill();
839 }
joshualitt1de610a2016-01-06 08:26:09 -0800840 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700841 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700842 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400843 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700844
bsalomon6663acf2016-05-10 09:14:17 -0700845 // Path effects should've been devolved to a path in SkGpuDevice
846 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700847
Robert Phillips72152832017-01-25 17:31:35 -0500848 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700849
bsalomon6663acf2016-05-10 09:14:17 -0700850 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400851 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000852 // Fills the rect, using rect as its own local coordinates
853 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500854 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700855 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
856 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
857 if ((!rect.width() || !rect.height()) &&
858 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
859 SkScalar r = stroke.getWidth() / 2;
860 // TODO: Move these stroke->fill fallbacks to GrShape?
861 switch (stroke.getJoin()) {
862 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500863 this->drawRect(
864 clip, std::move(paint), aa, viewMatrix,
865 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
866 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700867 return;
868 case SkPaint::kRound_Join:
869 // Raster draws nothing when both dimensions are empty.
870 if (rect.width() || rect.height()){
871 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500872 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
873 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700874 return;
875 }
876 case SkPaint::kBevel_Join:
877 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500878 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700879 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
880 &GrStyle::SimpleFill());
881 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500882 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700883 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
884 &GrStyle::SimpleFill());
885 }
886 return;
887 }
888 }
robertphillips44302392016-07-08 14:43:03 -0700889
Brian Salomonbaaf4392017-06-15 09:59:23 -0400890 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700891
Chris Dalton7d6748e2019-03-13 00:34:52 -0600892 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500893 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
894 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
895 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500896 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400897 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700898 return;
robertphillips4bc31812016-03-01 12:22:49 -0800899 }
robertphillips4bc31812016-03-01 12:22:49 -0800900 }
Mike Klein16885072018-12-11 09:54:31 -0500901 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500902 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700903}
904
Michael Ludwig69858532018-11-28 15:34:34 -0500905void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
906 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
907 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600908 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500909
910 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
911 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500912}
913
Robert Phillipsec2249f2016-11-09 08:54:35 -0500914int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500915 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400916 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500917}
918
Greg Danielf41b2bd2019-08-22 16:19:24 -0400919GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600920 ) const {
921#if GR_TEST_UTILS
922 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400923 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600924 }
925#endif
926 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
927 // would normally be overwritten. The one exception is if the render target context is marked as
928 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
929 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
930 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
931 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400932 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600933}
934
Chris Dalton858cf232019-10-14 16:20:00 -0600935void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600936 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600937 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600938 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600939
Chris Daltoneffee202019-07-01 22:28:03 -0600940 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600941 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500942 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600943 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400944 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600945 }
946 SkASSERT(numRequiredSamples > 0);
947
948 if (numRequiredSamples > fNumStencilSamples) {
949 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500950 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600951 }
Chris Dalton6b982802019-06-27 13:53:46 -0600952
Chris Dalton674f77a2019-09-30 20:49:39 -0600953 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600954 if (this->caps()->performStencilClearsAsDraws()) {
955 // There is a driver bug with clearing stencil. We must use an op to manually clear the
956 // stencil buffer before the op that required 'setNeedsStencil'.
957 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
958 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600959 this->getOpsTask()->setInitialStencilContent(
960 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600961 }
962 }
963}
964
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000965void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700966 ASSERT_SINGLE_OWNER_PRIV
967 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400968 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400969 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
970 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700971
Robert Phillips72152832017-01-25 17:31:35 -0500972 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400973
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500974 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
975}
976
977void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600978 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
979
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500980 if (this->caps()->performStencilClearsAsDraws()) {
981 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
982 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
983
984 // Configure the paint to have no impact on the color buffer
985 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400986 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400987 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
988 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500989 } else {
990 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -0500991 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500992 if (!op) {
993 return;
994 }
Chris Dalton08755122019-08-05 16:13:47 -0600995 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500996 }
robertphillips976f5f02016-06-03 10:59:20 -0700997}
998
Chris Daltonbbfd5162017-11-07 13:35:22 -0700999void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001000 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001001 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001002 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001003 ASSERT_SINGLE_OWNER_PRIV
1004 RETURN_IF_ABANDONED_PRIV
1005 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001006 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1007 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001008
Brian Salomon467921e2017-03-06 16:17:12 -05001009 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1010 SkASSERT(path);
1011 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1012
1013 // FIXME: Use path bounds instead of this WAR once
1014 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1015 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1016
1017 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001018 GrAppliedHardClip appliedClip;
1019 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1020 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001021 return;
1022 }
1023
Robert Phillips7c525e62018-06-12 10:11:12 -04001024 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1025 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001026 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001027 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001028 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001029 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001030 if (!op) {
1031 return;
1032 }
Brian Salomon97180af2017-03-14 13:42:58 -04001033 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001034
Chris Daltoneffee202019-07-01 22:28:03 -06001035 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001036 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001037}
1038
Michael Ludwig379e4962019-12-06 13:21:26 -05001039void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1040 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001041 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001042 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1043 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001044 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001045 ASSERT_SINGLE_OWNER
1046 RETURN_IF_ABANDONED
1047 SkDEBUGCODE(this->validate();)
1048 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001049
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001050 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1051 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1052 AutoCheckFlush acf(this->drawingManager());
1053 GrAAType aaType = this->chooseAAType(aa);
1054 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1055 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1056 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001057 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1058 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001059}
1060
Brian Osman11052242016-10-27 14:47:55 -04001061void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001062 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001063 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001064 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001065 GrPrimitiveType* overridePrimType,
1066 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001067 ASSERT_SINGLE_OWNER
1068 RETURN_IF_ABANDONED
1069 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001070 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001071
1072 AutoCheckFlush acf(this->drawingManager());
1073
1074 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001075 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001076 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Mike Reed5caf9352020-03-02 14:57:09 -05001077 fContext, std::move(paint), std::move(vertices), viewMatrix, aaType,
Brian Osman3c358422020-03-23 10:44:12 -04001078 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001079 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001080}
1081
1082///////////////////////////////////////////////////////////////////////////////
1083
Brian Osman4d92b892019-03-24 00:53:23 +00001084void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1085 GrPaint&& paint,
1086 const SkMatrix& viewMatrix,
1087 int spriteCount,
1088 const SkRSXform xform[],
1089 const SkRect texRect[],
1090 const SkColor colors[]) {
1091 ASSERT_SINGLE_OWNER
1092 RETURN_IF_ABANDONED
1093 SkDEBUGCODE(this->validate();)
1094 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1095
1096 AutoCheckFlush acf(this->drawingManager());
1097
1098 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1099 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1100 aaType, spriteCount, xform, texRect, colors);
1101 this->addDrawOp(clip, std::move(op));
1102}
1103
1104///////////////////////////////////////////////////////////////////////////////
1105
Brian Osman11052242016-10-27 14:47:55 -04001106void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001107 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001108 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001109 const SkMatrix& viewMatrix,
1110 const SkRRect& rrect,
1111 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001112 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001113 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001114 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001115 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001116
1117 const SkStrokeRec& stroke = style.strokeRec();
1118 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001119 return;
1120 }
1121
bsalomon7f0d9f32016-08-15 14:49:10 -07001122 GrNoClip noclip;
1123 const GrClip* clip = &origClip;
1124#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1125 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001126 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001127 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1128 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001129 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1130 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001131 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001132 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1133 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001134 clip = &noclip;
1135 }
1136#endif
bsalomon6663acf2016-05-10 09:14:17 -07001137 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001138
Robert Phillips72152832017-01-25 17:31:35 -05001139 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001140
Chris Dalton7d6748e2019-03-13 00:34:52 -06001141 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001142
Chris Dalton0dffbab2019-03-27 13:08:50 -06001143 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001144 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1145 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1146 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1147 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1148 // to avoid perf regressions on some platforms.
1149 assert_alive(paint);
1150 op = GrOvalOpFactory::MakeCircularRRectOp(
1151 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1152 }
1153 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001154 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001155 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001156 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001157 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001158 assert_alive(paint);
1159 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001160 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001161 }
1162 if (op) {
1163 this->addDrawOp(*clip, std::move(op));
1164 return;
robertphillipsea461502015-05-26 11:38:03 -07001165 }
robertphillipsb56f9272016-02-25 11:03:52 -08001166
Mike Klein16885072018-12-11 09:54:31 -05001167 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001168 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1169 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001170}
1171
Jim Van Verthc5903412016-11-17 15:27:09 -05001172///////////////////////////////////////////////////////////////////////////////
1173
Jim Van Verth3af1af92017-05-18 15:06:54 -04001174static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1175 SkPoint3 result;
1176 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1177 result.fZ = pt.fZ;
1178 return result;
1179}
1180
1181bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001182 const SkMatrix& viewMatrix,
1183 const SkPath& path,
1184 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001185 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001186 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001187 return true;
1188 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001189 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001190 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001191
1192 // check z plane
1193 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1194 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1195 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1196 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1197 return false;
1198 }
1199
1200 SkRRect rrect;
1201 SkRect rect;
1202 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001203 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001204 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1205 if (!isRRect &&
1206 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1207 rect.width() > SK_ScalarNearlyZero) {
1208 rrect.setOval(rect);
1209 isRRect = true;
1210 }
1211 if (!isRRect && path.isRect(&rect)) {
1212 rrect.setRect(rect);
1213 isRRect = true;
1214 }
1215
1216 if (!isRRect) {
1217 return false;
1218 }
1219
Jim Van Verthc5903412016-11-17 15:27:09 -05001220 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001221 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001222 }
1223
Robert Phillips72152832017-01-25 17:31:35 -05001224 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001225
Jim Van Verth3af1af92017-05-18 15:06:54 -04001226 // transform light
1227 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1228
1229 // 1/scale
1230 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001231 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001232 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1233 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1234
1235 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001236 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1237
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001238 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001239 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1240 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1241 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001242
1243 // Outset the shadow rrect to the border of the penumbra
1244 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1245 SkRRect ambientRRect;
1246 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1247 // If the rrect was an oval then its outset will also be one.
1248 // We set it explicitly to avoid errors.
1249 if (rrect.isOval()) {
1250 ambientRRect = SkRRect::MakeOval(outsetRect);
1251 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001252 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001253 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1254 }
1255
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001256 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001257 if (transparent) {
1258 // set a large inset to force a fill
1259 devSpaceInsetWidth = ambientRRect.width();
1260 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001261
Robert Phillips7c525e62018-06-12 10:11:12 -04001262 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1263 ambientColor,
1264 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001265 ambientRRect,
1266 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001267 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001268 if (op) {
1269 this->addDrawOp(clip, std::move(op));
1270 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001271 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001272
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001273 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001274 SkScalar devSpaceSpotBlur;
1275 SkScalar spotScale;
1276 SkVector spotOffset;
1277 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1278 devLightPos.fZ, rec.fLightRadius,
1279 &devSpaceSpotBlur, &spotScale, &spotOffset);
1280 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001281 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1282
Jim Van Verth3af1af92017-05-18 15:06:54 -04001283 // Adjust translate for the effect of the scale.
1284 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1285 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1286 // This offset is in dev space, need to transform it into source space.
1287 SkMatrix ctmInverse;
1288 if (viewMatrix.invert(&ctmInverse)) {
1289 ctmInverse.mapPoints(&spotOffset, 1);
1290 } else {
1291 // Since the matrix is a similarity, this should never happen, but just in case...
1292 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1293 SkASSERT(false);
1294 }
1295
1296 // Compute the transformed shadow rrect
1297 SkRRect spotShadowRRect;
1298 SkMatrix shadowTransform;
1299 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1300 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001301 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001302
1303 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001304 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001305 SkScalar insetWidth = blurOutset;
1306 if (transparent) {
1307 // If transparent, just do a fill
1308 insetWidth += spotShadowRRect.width();
1309 } else {
1310 // For shadows, instead of using a stroke we specify an inset from the penumbra
1311 // border. We want to extend this inset area so that it meets up with the caster
1312 // geometry. The inset geometry will by default already be inset by the blur width.
1313 //
1314 // We compare the min and max corners inset by the radius between the original
1315 // rrect and the shadow rrect. The distance between the two plus the difference
1316 // between the scaled radius and the original radius gives the distance from the
1317 // transformed shadow shape to the original shape in that corner. The max
1318 // of these gives the maximum distance we need to cover.
1319 //
1320 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1321 // that to get the full insetWidth.
1322 SkScalar maxOffset;
1323 if (rrect.isRect()) {
1324 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001325 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001326 rrect.rect().fLeft),
1327 SkTAbs(spotShadowRRect.rect().fTop -
1328 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001329 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001330 rrect.rect().fRight),
1331 SkTAbs(spotShadowRRect.rect().fBottom -
1332 rrect.rect().fBottom)));
1333 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001334 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001335 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1336 rrect.rect().fLeft + dr,
1337 spotShadowRRect.rect().fTop -
1338 rrect.rect().fTop + dr);
1339 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1340 rrect.rect().fRight - dr,
1341 spotShadowRRect.rect().fBottom -
1342 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001343 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001344 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001345 }
Brian Osman788b9162020-02-07 10:36:46 -05001346 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001347 }
1348
1349 // Outset the shadow rrect to the border of the penumbra
1350 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1351 if (spotShadowRRect.isOval()) {
1352 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1353 } else {
1354 SkScalar outsetRad = spotRadius + blurOutset;
1355 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1356 }
1357
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001358 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001359
Robert Phillips7c525e62018-06-12 10:11:12 -04001360 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1361 spotColor,
1362 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001363 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001364 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001365 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001366 if (op) {
1367 this->addDrawOp(clip, std::move(op));
1368 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001369 }
1370
1371 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001372}
1373
1374///////////////////////////////////////////////////////////////////////////////
1375
Brian Osman11052242016-10-27 14:47:55 -04001376bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001377 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001378 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001379 const SkMatrix& viewMatrix,
1380 const SkRRect& origOuter,
1381 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001382 SkASSERT(!origInner.isEmpty());
1383 SkASSERT(!origOuter.isEmpty());
1384
Brian Salomon65749212017-12-01 16:01:47 -05001385 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1386
Chris Dalton7d6748e2019-03-13 00:34:52 -06001387 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001388
1389 if (GrAAType::kMSAA == aaType) {
1390 return false;
1391 }
1392
Greg Daniel2655ede2019-04-10 00:49:28 +00001393 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1394 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001395 auto outerR = outer->width() / 2.f;
1396 auto innerR = inner->width() / 2.f;
1397 auto cx = outer->getBounds().fLeft + outerR;
1398 auto cy = outer->getBounds().fTop + outerR;
1399 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1400 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1401 auto avgR = (innerR + outerR) / 2.f;
1402 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1403 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1404 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001405 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001406 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001407 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001408 if (op) {
1409 this->addDrawOp(clip, std::move(op));
1410 return true;
1411 }
Mike Klein16885072018-12-11 09:54:31 -05001412 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001413 }
1414 }
1415
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001416 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001417 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001418 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1419 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001420 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001421 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1422 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001423 }
robertphillips00095892016-02-29 13:50:40 -08001424
robertphillips00095892016-02-29 13:50:40 -08001425 SkMatrix inverseVM;
1426 if (!viewMatrix.isIdentity()) {
1427 if (!origInner.transform(viewMatrix, inner.writable())) {
1428 return false;
1429 }
1430 if (!origOuter.transform(viewMatrix, outer.writable())) {
1431 return false;
1432 }
1433 if (!viewMatrix.invert(&inverseVM)) {
1434 return false;
1435 }
1436 } else {
1437 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001438 }
robertphillips00095892016-02-29 13:50:40 -08001439
Ethan Nicholaseace9352018-10-15 20:09:54 +00001440 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001441 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001442 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001443 if (!innerEffect) {
1444 return false;
1445 }
1446
Ethan Nicholaseace9352018-10-15 20:09:54 +00001447 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001448 if (!outerEffect) {
1449 return false;
1450 }
1451
Brian Salomon82f44312017-01-11 13:42:54 -05001452 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1453 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001454
1455 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001456 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001457 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1458 }
halcanary9d524f22016-03-29 09:03:52 -07001459
Brian Salomon82f44312017-01-11 13:42:54 -05001460 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1461 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001462 return true;
1463}
1464
Brian Osman11052242016-10-27 14:47:55 -04001465void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001466 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001467 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001468 const SkMatrix& viewMatrix,
1469 const SkRRect& outer,
1470 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001471 ASSERT_SINGLE_OWNER
1472 RETURN_IF_ABANDONED
1473 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001474 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001475
1476 SkASSERT(!outer.isEmpty());
1477 SkASSERT(!inner.isEmpty());
1478
Robert Phillips72152832017-01-25 17:31:35 -05001479 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001480
Brian Salomon82f44312017-01-11 13:42:54 -05001481 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001482 return;
1483 }
Mike Klein16885072018-12-11 09:54:31 -05001484 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001485
1486 SkPath path;
1487 path.setIsVolatile(true);
1488 path.addRRect(inner);
1489 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001490 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001491 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001492}
1493
robertphillipsea461502015-05-26 11:38:03 -07001494///////////////////////////////////////////////////////////////////////////////
1495
Brian Osman11052242016-10-27 14:47:55 -04001496void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001497 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001498 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001499 const SkMatrix& viewMatrix,
1500 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001501 const GrStyle& style,
1502 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001503 ASSERT_SINGLE_OWNER
1504 RETURN_IF_ABANDONED
1505 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001506 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001507
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001508 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001509 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001510 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001511 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001512 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1513 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001514 aa = GrAA::kNo;
1515 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001516 }
msarettcc319b92016-08-25 18:07:18 -07001517 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001518 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001519 SkPath path;
1520 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001521 path.setIsVolatile(true);
1522
Brian Salomon82f44312017-01-11 13:42:54 -05001523 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001524 }
1525
Chris Dalton7d6748e2019-03-13 00:34:52 -06001526 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001527 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1528 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001529 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001530}
1531
Brian Osman11052242016-10-27 14:47:55 -04001532void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001533 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001534 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001535 const SkMatrix& viewMatrix,
1536 const SkRect& oval,
1537 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001538 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001539 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001540 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001541 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001542
Robert Phillips7484d202018-07-03 09:09:08 -04001543 const SkStrokeRec& stroke = style.strokeRec();
1544
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001545 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001546 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1547 return;
1548 }
1549
1550 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001551 return;
robertphillipsea461502015-05-26 11:38:03 -07001552 }
1553
Robert Phillips72152832017-01-25 17:31:35 -05001554 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001555
Chris Dalton7d6748e2019-03-13 00:34:52 -06001556 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001557
1558 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001559 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1560 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001561 // We don't draw true circles as round rects in coverage mode, because it can
1562 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1563 assert_alive(paint);
1564 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1565 this->caps()->shaderCaps());
1566 }
1567 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001568 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1569 // the arc equation. This same special geometry and fragment branch also turn out to be a
1570 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1571 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1572 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001573 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001574 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1575 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001576 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001577 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001578 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001579 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1580 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001581 }
1582 if (op) {
1583 this->addDrawOp(clip, std::move(op));
1584 return;
robertphillipsea461502015-05-26 11:38:03 -07001585 }
robertphillipsb56f9272016-02-25 11:03:52 -08001586
Mike Klein16885072018-12-11 09:54:31 -05001587 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001588 this->drawShapeUsingPathRenderer(
1589 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001590 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001591}
1592
Brian Osman11052242016-10-27 14:47:55 -04001593void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001594 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001595 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001596 const SkMatrix& viewMatrix,
1597 const SkRect& oval,
1598 SkScalar startAngle,
1599 SkScalar sweepAngle,
1600 bool useCenter,
1601 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001602 ASSERT_SINGLE_OWNER
1603 RETURN_IF_ABANDONED
1604 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001605 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001606
1607 AutoCheckFlush acf(this->drawingManager());
1608
Chris Dalton7d6748e2019-03-13 00:34:52 -06001609 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001610 if (GrAAType::kCoverage == aaType) {
1611 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1612 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1613 std::move(paint),
1614 viewMatrix,
1615 oval,
1616 startAngle,
1617 sweepAngle,
1618 useCenter,
1619 style,
1620 shaderCaps);
1621 if (op) {
1622 this->addDrawOp(clip, std::move(op));
1623 return;
1624 }
1625 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001626 }
Brian Salomone4949402018-04-26 15:22:04 -04001627 this->drawShapeUsingPathRenderer(
1628 clip, std::move(paint), aa, viewMatrix,
1629 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001630}
1631
Brian Osman11052242016-10-27 14:47:55 -04001632void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001633 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001634 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001635 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001636 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001637 sk_sp<GrColorSpaceXform> csxf,
1638 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001639 std::unique_ptr<SkLatticeIter> iter,
1640 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001641 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001642 RETURN_IF_ABANDONED
1643 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001644 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001645
Robert Phillips72152832017-01-25 17:31:35 -05001646 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001647
Brian Salomon2a943df2018-05-04 13:43:19 -04001648 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001649 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001650 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001651 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001652}
1653
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001654void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1655 const SkRect& bounds) {
1656 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1657 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001658 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001659}
1660
Brian Salomon031b0ba2019-05-23 11:05:26 -04001661void GrRenderTargetContext::asyncRescaleAndReadPixels(
1662 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1663 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1664 auto direct = fContext->priv().asDirectContext();
1665 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001666 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001667 return;
1668 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001669 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001670 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001671 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001672 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001673 if (this->asRenderTargetProxy()->framebufferOnly()) {
1674 callback(context, nullptr);
1675 return;
1676 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001677 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001678 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001679 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001680 return;
1681 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001682 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001683 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001684 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001685 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001686 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001687 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1688 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001689 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001690 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001691 backendFormatOfFinalContext, dstCT);
1692 // Fail if we can't read from the source surface's color type.
1693 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001694 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001695 return;
1696 }
Brian Salomon624f9062019-07-02 14:23:00 -04001697 // Fail if read color type does not have all of dstCT's color channels and those missing color
1698 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001699 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1700 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1701 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1702 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001703 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001704 return;
1705 }
1706
Brian Salomonbf6b9792019-08-21 09:38:10 -04001707 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 int x = srcRect.fLeft;
1709 int y = srcRect.fTop;
1710 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001711 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1712 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001713 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001714 return;
1715 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001716 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001717 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001718 x = y = 0;
1719 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001720 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1721 this->colorInfo().alphaType(),
1722 info.colorSpace(),
1723 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001724 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1725 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001726 // We flip or color convert by drawing and we don't currently support drawing to
1727 // kPremul.
1728 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001729 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001730 return;
1731 }
Greg Daniel40903af2020-01-30 14:55:05 -05001732 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001733 SkRect srcRectToDraw = SkRect::Make(srcRect);
1734 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001735 if (!texProxyView.asTextureProxy()) {
1736 texProxyView = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
1737 this->origin(), this->colorInfo().colorType(),
1738 GrMipMapped::kNo, srcRect,
1739 SkBackingFit::kApprox, SkBudgeted::kNo);
1740 if (!texProxyView.asTextureProxy()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001741 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001742 return;
1743 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001744 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1745 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001746 tempRTC = GrRenderTargetContext::Make(
1747 direct, this->colorInfo().colorType(), info.refColorSpace(),
1748 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001749 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001750 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001751 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001752 return;
1753 }
Greg Daniel40903af2020-01-30 14:55:05 -05001754 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1755 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1756 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001757 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1758 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1759 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001760 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001761 }
1762 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001763 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001764 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1765 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001766}
1767
Brian Salomon9241a6d2019-10-03 13:26:54 -04001768class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1769public:
1770 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1771 ~AsyncReadResult() override {
1772 for (int i = 0; i < fPlanes.count(); ++i) {
1773 if (!fPlanes[i].fMappedBuffer) {
1774 delete[] static_cast<const char*>(fPlanes[i].fData);
1775 } else {
1776 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1777 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1778 }
1779 }
1780 }
1781
1782 int count() const override { return fPlanes.count(); }
1783 const void* data(int i) const override { return fPlanes[i].fData; }
1784 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1785
1786 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001787 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001788 size_t rowBytes,
1789 GrClientMappedBufferManager* manager) {
1790 SkASSERT(!result.fTransferBuffer->isMapped());
1791 const void* mappedData = result.fTransferBuffer->map();
1792 if (!mappedData) {
1793 return false;
1794 }
1795 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001796 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001797 result.fPixelConverter(convertedData.get(), mappedData);
1798 this->addCpuPlane(std::move(convertedData), rowBytes);
1799 result.fTransferBuffer->unmap();
1800 } else {
1801 manager->insert(result.fTransferBuffer);
1802 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1803 }
1804 return true;
1805 }
1806
1807 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1808 SkASSERT(data);
1809 SkASSERT(rowBytes > 0);
1810 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1811 }
1812
1813private:
1814 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1815 SkASSERT(data);
1816 SkASSERT(rowBytes > 0);
1817 SkASSERT(mappedBuffer);
1818 SkASSERT(mappedBuffer->isMapped());
1819 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1820 }
1821
1822 struct Plane {
1823 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1824 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1825 const void* fData;
1826 size_t fRowBytes;
1827 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1828 sk_sp<GrGpuBuffer> fMappedBuffer;
1829 };
1830 SkSTArray<3, Plane> fPlanes;
1831 uint32_t fInboxID;
1832};
1833
Brian Salomon024bd002019-06-11 11:38:16 -04001834void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1835 ReadPixelsCallback callback,
1836 ReadPixelsContext context) {
1837 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1838 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1839
Greg Danielca9dbe22020-01-02 13:44:30 -05001840 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001841 callback(context, nullptr);
1842 return;
1843 }
1844
Brian Salomon9241a6d2019-10-03 13:26:54 -04001845 auto directContext = fContext->priv().asDirectContext();
1846 SkASSERT(directContext);
1847 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1848
Brian Salomon024bd002019-06-11 11:38:16 -04001849 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1850
1851 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001852 auto ii = SkImageInfo::Make(rect.size(), colorType,
1853 this->colorInfo().alphaType(),
1854 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001855 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001856 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1857 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1858 result->addCpuPlane(std::move(data), pm.rowBytes());
1859
Brian Salomon1d435302019-07-01 13:05:28 -04001860 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001861 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001862 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001863 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001864 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001865 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001866 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001867
Brian Salomonab32f652019-05-10 14:24:50 -04001868 struct FinishContext {
1869 ReadPixelsCallback* fClientCallback;
1870 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001871 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001872 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001873 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001874 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001875 };
1876 // Assumption is that the caller would like to flush. We could take a parameter or require an
1877 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1878 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001879 auto* finishContext = new FinishContext{callback,
1880 context,
1881 rect.size(),
1882 colorType,
1883 mappedBufferManager,
1884 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001885 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001886 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001887 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001888 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1889 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1890 context->fMappedBufferManager)) {
1891 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001892 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001893 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001894 delete context;
1895 };
1896 GrFlushInfo flushInfo;
1897 flushInfo.fFinishedContext = finishContext;
1898 flushInfo.fFinishedProc = finishCallback;
1899 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1900}
1901
Brian Salomon9241a6d2019-10-03 13:26:54 -04001902void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1903 sk_sp<SkColorSpace> dstColorSpace,
1904 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001905 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001906 RescaleGamma rescaleGamma,
1907 SkFilterQuality rescaleQuality,
1908 ReadPixelsCallback callback,
1909 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001910 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1911 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001912 SkASSERT(!dstSize.isZero());
1913 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1914
Brian Salomon024bd002019-06-11 11:38:16 -04001915 auto direct = fContext->priv().asDirectContext();
1916 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001917 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001918 return;
1919 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001920 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001921 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001922 return;
1923 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001924 if (this->asRenderTargetProxy()->framebufferOnly()) {
1925 callback(context, nullptr);
1926 return;
1927 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001928 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001929 callback(context, nullptr);
1930 return;
1931 }
Brian Salomon024bd002019-06-11 11:38:16 -04001932 int x = srcRect.fLeft;
1933 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001934 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001935 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001936 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001937 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001938 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001939 dstColorSpace);
1940 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001941 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001942 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001943 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001944 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001945 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001946 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001947 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001948 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001949 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001950 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001951 srcView = this->readSurfaceView();
1952 if (!srcView.asTextureProxy()) {
1953 srcView = GrSurfaceProxy::Copy(fContext, fReadView.proxy(), this->origin(),
1954 this->colorInfo().colorType(), GrMipMapped::kNo,
1955 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1956 if (!srcView.asTextureProxy()) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001957 // If we can't get a texture copy of the contents then give up.
1958 callback(context, nullptr);
1959 return;
1960 }
1961 x = y = 0;
1962 }
Brian Salomon4d036892019-07-02 15:10:58 -04001963 // We assume the caller wants kPremul. There is no way to indicate a preference.
1964 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001965 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1966 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001967 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001968 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1969 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001970 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1971 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001972 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001973 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001974 return;
1975 }
Greg Daniel40903af2020-01-30 14:55:05 -05001976 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1977 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1978 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1979 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1980 SkMatrix::I(), std::move(xform));
1981 srcView = tempRTC->readSurfaceView();
1982 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001983 x = y = 0;
1984 }
1985 }
Greg Danielc594e622019-10-15 14:01:49 -04001986
Greg Daniele20fcad2020-01-08 11:52:34 -05001987 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1988 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1989 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001990 int halfW = dstSize.width()/2;
1991 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001992 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1993 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1994 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1995 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1996 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1997 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001998 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001999 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002000 return;
2001 }
2002
Brian Osman28ba5282019-10-30 14:18:07 -04002003 float baseM[20];
2004 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2005
Brian Salomon024bd002019-06-11 11:38:16 -04002006 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2007
2008 auto texMatrix = SkMatrix::MakeTrans(x, y);
2009
Brian Salomon9241a6d2019-10-03 13:26:54 -04002010 SkRect dstRectY = SkRect::Make(dstSize);
2011 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002012
Brian Salomon6aa65052020-01-28 12:16:53 -05002013 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2014 PixelTransferResult yTransfer, uTransfer, vTransfer;
2015
Brian Salomon024bd002019-06-11 11:38:16 -04002016 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2017 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002018 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002019 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002020 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002021 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002022 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002023 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2024 yPaint.addColorFragmentProcessor(std::move(yFP));
2025 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002026 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2027 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002028 if (!doSynchronousRead) {
2029 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2030 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2031 if (!yTransfer.fTransferBuffer) {
2032 callback(context, nullptr);
2033 return;
2034 }
Brian Salomon024bd002019-06-11 11:38:16 -04002035 }
2036
2037 texMatrix.preScale(2.f, 2.f);
2038 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2039 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002040 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002041 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002042 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002043 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002044 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002045 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2046 uPaint.addColorFragmentProcessor(std::move(uFP));
2047 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002048 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2049 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002050 if (!doSynchronousRead) {
2051 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2052 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2053 if (!uTransfer.fTransferBuffer) {
2054 callback(context, nullptr);
2055 return;
2056 }
Brian Salomon024bd002019-06-11 11:38:16 -04002057 }
2058
2059 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2060 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002061 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002062 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002063 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002064 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2065 this->colorInfo().alphaType(), texMatrix,
2066 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002067 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2068 vPaint.addColorFragmentProcessor(std::move(vFP));
2069 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002070 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2071 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002072 if (!doSynchronousRead) {
2073 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2074 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2075 if (!vTransfer.fTransferBuffer) {
2076 callback(context, nullptr);
2077 return;
2078 }
2079 }
2080
2081 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002082 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2083 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2084 size_t yRB = yInfo.minRowBytes();
2085 size_t uvRB = uvInfo.minRowBytes();
2086 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2087 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2088 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2089 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2090 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2091 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002092 callback(context, nullptr);
2093 return;
2094 }
2095 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002096 result->addCpuPlane(std::move(y), yRB );
2097 result->addCpuPlane(std::move(u), uvRB);
2098 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002099 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002100 return;
2101 }
2102
2103 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002104 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002105 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002106 GrClientMappedBufferManager* fMappedBufferManager;
2107 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002108 PixelTransferResult fYTransfer;
2109 PixelTransferResult fUTransfer;
2110 PixelTransferResult fVTransfer;
2111 };
2112 // Assumption is that the caller would like to flush. We could take a parameter or require an
2113 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2114 // callback to GrGpu until after the next flush that flushes our op list, though.
2115 auto* finishContext = new FinishContext{callback,
2116 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002117 direct->priv().clientMappedBufferManager(),
2118 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002119 std::move(yTransfer),
2120 std::move(uTransfer),
2121 std::move(vTransfer)};
2122 auto finishCallback = [](GrGpuFinishedContext c) {
2123 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002124 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002125 auto manager = context->fMappedBufferManager;
2126 size_t rowBytes = SkToSizeT(context->fSize.width());
2127 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2128 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002129 delete context;
2130 return;
2131 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002132 rowBytes /= 2;
2133 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2134 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2135 (*context->fClientCallback)(context->fClientContext, nullptr);
2136 delete context;
2137 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002138 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002139 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2140 (*context->fClientCallback)(context->fClientContext, nullptr);
2141 delete context;
2142 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002143 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002144 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002145 delete context;
2146 };
2147 GrFlushInfo flushInfo;
2148 flushInfo.fFinishedContext = finishContext;
2149 flushInfo.fFinishedProc = finishCallback;
2150 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002151}
2152
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002153GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2154 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002155 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002156 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002157 return GrSemaphoresSubmitted::kNo;
2158 }
robertphillips8c523e02016-07-26 07:41:00 -07002159 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002160 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002161
Greg Daniel55f040b2020-02-13 15:38:32 +00002162 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002163}
2164
Greg Danielc64ee462017-06-15 16:59:49 -04002165bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002166 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002167 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002168 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002169 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002170 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002171
2172 AutoCheckFlush acf(this->drawingManager());
2173
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002174 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002175 return false;
2176 }
2177
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002178 auto direct = fContext->priv().asDirectContext();
2179 if (!direct) {
2180 return false;
2181 }
2182
2183 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002184
Greg Daniel301015c2019-11-18 14:06:46 -05002185 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2186 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002187 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002188 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002189 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2190 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002191 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002192 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2193 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002194 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002195}
joshualitt33a5fce2015-11-18 13:28:51 -08002196
Brian Osman11052242016-10-27 14:47:55 -04002197void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002198 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002199 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002200 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002201 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002202 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002203 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002204 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002205 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002206 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2207
Brian Salomon40b77a62017-12-22 11:25:52 -05002208 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002209
Robert Phillips27927a52018-08-20 13:18:12 -04002210 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002211}
2212
2213void GrRenderTargetContext::drawShape(const GrClip& clip,
2214 GrPaint&& paint,
2215 GrAA aa,
2216 const SkMatrix& viewMatrix,
2217 const GrShape& shape) {
2218 ASSERT_SINGLE_OWNER
2219 RETURN_IF_ABANDONED
2220 SkDEBUGCODE(this->validate();)
2221 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2222
Brian Salomon2fad74a2017-12-20 13:28:55 -05002223 if (shape.isEmpty()) {
2224 if (shape.inverseFilled()) {
2225 this->drawPaint(clip, std::move(paint), viewMatrix);
2226 }
2227 return;
robertphillipsea461502015-05-26 11:38:03 -07002228 }
2229
Robert Phillips72152832017-01-25 17:31:35 -05002230 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002231
Brian Salomon2fad74a2017-12-20 13:28:55 -05002232 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002233 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002234 SkRRect rrect;
2235 // We can ignore the starting point and direction since there is no path effect.
2236 bool inverted;
2237 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2238 if (rrect.isRect()) {
2239 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2240 &shape.style());
2241 return;
2242 } else if (rrect.isOval()) {
2243 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002244 return;
2245 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002246 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2247 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002248 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2249 viewMatrix.rectStaysRect()) {
2250 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2251 // the matrix to all the points individually rather than just to the rect
2252 SkRect rects[2];
2253 if (shape.asNestedRects(rects)) {
2254 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002255 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002256 fContext, std::move(paint), viewMatrix, rects);
2257 if (op) {
2258 this->addDrawOp(clip, std::move(op));
2259 }
2260 // Returning here indicates that there is nothing to draw in this case.
2261 return;
2262 }
robertphillipsea461502015-05-26 11:38:03 -07002263 }
2264 }
robertphillips4bc31812016-03-01 12:22:49 -08002265
Brian Salomon2fad74a2017-12-20 13:28:55 -05002266 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002267}
2268
Chris Daltonbbfd5162017-11-07 13:35:22 -07002269bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002270 const GrUserStencilSettings* ss,
2271 SkRegion::Op op,
2272 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002273 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002274 const SkMatrix& viewMatrix,
2275 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002276 ASSERT_SINGLE_OWNER_PRIV
2277 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002278 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002279 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2280 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002281
2282 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002283 GrPaint paint;
2284 paint.setCoverageSetOpXPFactory(op, invert);
2285 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2286 SkRect::MakeIWH(fRenderTargetContext->width(),
2287 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002288 return true;
2289 }
2290
Robert Phillips72152832017-01-25 17:31:35 -05002291 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002292
2293 // An Assumption here is that path renderer would use some form of tweaking
2294 // the src color (either the input alpha or in the frag shader) to implement
2295 // aa. If we have some future driver-mojo path AA that can do the right
2296 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002297 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002298 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002299
Chris Daltondb91c6e2017-09-08 16:25:08 -06002300 SkIRect clipConservativeBounds;
2301 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2302 &clipConservativeBounds, nullptr);
2303
bsalomon8acedde2016-06-24 10:42:16 -07002304 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002305 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002306 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002307 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002308 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002309 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002310 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002311 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002312 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2313 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002314 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002315
2316 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002317 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002318 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002319 if (!pr) {
2320 return false;
2321 }
2322
2323 GrPaint paint;
2324 paint.setCoverageSetOpXPFactory(op, invert);
2325
Brian Salomonf3569f02017-10-24 12:52:33 -04002326 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2327 std::move(paint),
2328 ss,
2329 fRenderTargetContext,
2330 &clip,
2331 &clipConservativeBounds,
2332 &viewMatrix,
2333 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002334 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002335 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002336 pr->drawPath(args);
2337 return true;
2338}
2339
Brian Osman11052242016-10-27 14:47:55 -04002340SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002341 ASSERT_SINGLE_OWNER_PRIV
2342
Robert Phillips6a6de562019-02-15 15:19:15 -05002343 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002344 return SkBudgeted::kNo;
2345 }
2346
Brian Osman11052242016-10-27 14:47:55 -04002347 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002348
Greg Daniel3912a4b2020-01-14 09:56:04 -05002349 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002350}
2351
Brian Salomon2fad74a2017-12-20 13:28:55 -05002352void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2353 GrPaint&& paint,
2354 GrAA aa,
2355 const SkMatrix& viewMatrix,
2356 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002357 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002358 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002359 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2360
Jim Van Verthf86073a2018-10-02 13:05:38 -04002361 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2362 return;
2363 }
2364
Chris Daltondb91c6e2017-09-08 16:25:08 -06002365 SkIRect clipConservativeBounds;
2366 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2367
Brian Salomon2fad74a2017-12-20 13:28:55 -05002368 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002369 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002370
robertphillips68737822015-10-29 12:12:21 -07002371 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002372 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002373 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002374 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002375 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002376 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002377 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002378 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002379
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002380 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002381 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002382 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002383 return;
2384 }
2385
Chris Dalton6ce447a2019-06-23 18:07:38 -06002386 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002387
2388 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2389 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2390 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2391
Brian Salomon2fad74a2017-12-20 13:28:55 -05002392 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002393 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002394 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2395 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002396 return;
2397 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002398 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002399 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002400 }
2401 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002402 if (canDrawArgs.fShape->style().applies()) {
2403 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2404 styleScale);
2405 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002406 return;
2407 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002408 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002409 // This time, allow SW renderer
2410 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2411 } else {
2412 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002413 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002414 }
robertphillipsea461502015-05-26 11:38:03 -07002415
bsalomon8acedde2016-06-24 10:42:16 -07002416 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002417#ifdef SK_DEBUG
2418 SkDebugf("Unable to find path renderer compatible with path.\n");
2419#endif
2420 return;
2421 }
2422
Robert Phillips256c37b2017-03-01 14:32:46 -05002423 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002424 std::move(paint),
2425 &GrUserStencilSettings::kUnused,
2426 this,
2427 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002428 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002429 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002430 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002431 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002432 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002433 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002434}
2435
Brian Salomon467921e2017-03-06 16:17:12 -05002436static void op_bounds(SkRect* bounds, const GrOp* op) {
2437 *bounds = op->bounds();
2438 if (op->hasZeroArea()) {
2439 if (op->hasAABloat()) {
2440 bounds->outset(0.5f, 0.5f);
2441 } else {
2442 // We don't know which way the particular GPU will snap lines or points at integer
2443 // coords. So we ensure that the bounds is large enough for either snap.
2444 SkRect before = *bounds;
2445 bounds->roundOut(bounds);
2446 if (bounds->fLeft == before.fLeft) {
2447 bounds->fLeft -= 1;
2448 }
2449 if (bounds->fTop == before.fTop) {
2450 bounds->fTop -= 1;
2451 }
2452 if (bounds->fRight == before.fRight) {
2453 bounds->fRight += 1;
2454 }
2455 if (bounds->fBottom == before.fBottom) {
2456 bounds->fBottom += 1;
2457 }
2458 }
2459 }
2460}
2461
Chris Dalton08755122019-08-05 16:13:47 -06002462void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002463 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002464 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2465}
2466
Brian Salomon348a0372018-10-31 10:42:18 -04002467void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2468 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002469 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002470 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002471 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002472 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002473 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002474 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002475 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002476 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002477
Brian Salomon467921e2017-03-06 16:17:12 -05002478 // Setup clip
2479 SkRect bounds;
2480 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002481 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002482 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002483 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002484 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002485
Chris Dalton9acf6822019-11-12 11:52:40 -07002486 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002487 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002488 }
2489
Chris Dalton9acf6822019-11-12 11:52:40 -07002490 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002491 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002492 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002493 }
2494
Chris Dalton9acf6822019-11-12 11:52:40 -07002495 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2496 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2497
2498 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2499 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2500 // that uses a stencil test when the stencil buffer is multisampled.)
2501 bool hasMixedSampledCoverage = (
2502 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002503 SkASSERT(!hasMixedSampledCoverage ||
2504 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002505
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002506 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002507 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002508 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2509
Greg Daniel524e28b2019-11-01 11:48:53 -04002510 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002511 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002512 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002513 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002514 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002515 }
2516 }
2517
2518 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002519 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002520 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002521 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002522 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002523 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002524 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002525}
2526
Greg Daniel524e28b2019-11-01 11:48:53 -04002527bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2528 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002529 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2530 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2531 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002532 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002533 return false;
2534 }
2535
Greg Daniel3912a4b2020-01-14 09:56:04 -05002536 if (this->caps()->textureBarrierSupport() &&
2537 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002538 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002539 // The render target is a texture, so we can read from it directly in the shader. The XP
2540 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002541 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002542 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002543 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002544 }
2545 }
2546
Greg Daniel3912a4b2020-01-14 09:56:04 -05002547 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002548
Eric Karl74480882017-04-03 14:49:05 -07002549 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002550 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002551 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002552 SkRect opBounds = op.bounds();
2553 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2554 // 0.5 pixels.
2555 if (op.hasAABloat() || op.hasZeroArea()) {
2556 opBounds.outset(0.5f, 0.5f);
2557 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2558 // performance we may ignore the clip when the draw is entirely inside the clip is float
2559 // space but will hit pixels just outside the clip when actually rasterizing.
2560 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002561 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002562 }
2563 SkIRect opIBounds;
2564 opBounds.roundOut(&opIBounds);
2565 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002566#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002567 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002568#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002569 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002570 }
2571
Greg Daniel3155f7f2020-01-16 16:54:26 -05002572 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002573 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2574 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002575 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002576 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002577
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002578 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002579 copyRect = clippedRect;
2580 }
Brian Salomon467921e2017-03-06 16:17:12 -05002581
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002582 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002583 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002584 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002585 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002586 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002587 } else {
Eric Karl74480882017-04-03 14:49:05 -07002588 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002589 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002590 }
Greg Daniel40903af2020-01-30 14:55:05 -05002591 GrSurfaceProxyView newProxyView =
2592 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), colorType,
2593 GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2594 restrictions.fRectsMustMatch);
2595 SkASSERT(newProxyView.proxy());
Robert Phillipsbf25d432017-04-07 10:08:53 -04002596
Greg Daniel40903af2020-01-30 14:55:05 -05002597 dstProxyView->setProxyView(std::move(newProxyView));
Greg Daniel524e28b2019-11-01 11:48:53 -04002598 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002600}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002601
Greg Daniel573312e2020-02-07 17:22:35 -05002602bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002603 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002604 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002605 SkIRect clippedSrcRect;
2606 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002607 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2608 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002609 return false;
2610 }
2611
2612 GrPaint paint;
2613 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002614
Greg Danield2ccbb52020-02-05 10:45:39 -05002615 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002616 if (!fp) {
2617 return false;
2618 }
2619 paint.addColorFragmentProcessor(std::move(fp));
2620
2621 this->fillRectToRect(
2622 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2623 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2624 clippedSrcRect.height()),
2625 SkRect::Make(clippedSrcRect));
2626 return true;
2627}