blob: 6c9ef471bed881aa81f6698b7900bd3a56d11726 [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"
48#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040049#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040050#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000051#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040052#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050053#include "src/gpu/ops/GrAtlasTextOp.h"
54#include "src/gpu/ops/GrClearOp.h"
55#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrDrawAtlasOp.h"
57#include "src/gpu/ops/GrDrawOp.h"
58#include "src/gpu/ops/GrDrawVerticesOp.h"
59#include "src/gpu/ops/GrDrawableOp.h"
60#include "src/gpu/ops/GrFillRRectOp.h"
61#include "src/gpu/ops/GrFillRectOp.h"
62#include "src/gpu/ops/GrLatticeOp.h"
63#include "src/gpu/ops/GrOp.h"
64#include "src/gpu/ops/GrOvalOpFactory.h"
65#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050066#include "src/gpu/ops/GrShadowRRectOp.h"
67#include "src/gpu/ops/GrStencilPathOp.h"
68#include "src/gpu/ops/GrStrokeRectOp.h"
69#include "src/gpu/ops/GrTextureOp.h"
70#include "src/gpu/text/GrTextContext.h"
71#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040072
Herb Derbyc1b482c2018-08-09 15:02:27 -040073class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040074public:
75 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040076 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040077 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040078 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040079 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040080
Robert Phillips7c525e62018-06-12 10:11:12 -040081 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040082 fRenderTargetContext->addDrawOp(clip, std::move(op));
83 }
84
Robert Phillips46a13382018-08-23 13:53:01 -040085 void drawShape(const GrClip& clip, const SkPaint& paint,
86 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040087 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
88 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040089 }
90
91 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040092 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050093 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040094 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040095 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040096 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040097 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040098 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040099 }
100 }
101
Robert Phillips69893702019-02-22 11:16:30 -0500102 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400103 return fRenderTargetContext->fContext;
104 }
105
Herb Derby65956872018-08-21 16:55:04 -0400106 SkGlyphRunListPainter* glyphPainter() override {
107 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400108 }
109
Brian Salomonf18b1d82017-10-27 11:30:49 -0400110private:
111 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400112 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400113
Brian Salomonf18b1d82017-10-27 11:30:49 -0400114};
joshualittbc907352016-01-13 06:45:40 -0800115
Robert Phillips72152832017-01-25 17:31:35 -0500116#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800117#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400118 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800119#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400120 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500121#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
122#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
123#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
124#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
125#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700126
Brian Salomone225b562017-06-14 13:00:03 -0400127//////////////////////////////////////////////////////////////////////////////
128
robertphillipsea461502015-05-26 11:38:03 -0700129class AutoCheckFlush {
130public:
halcanary9d524f22016-03-29 09:03:52 -0700131 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700132 SkASSERT(fDrawingManager);
133 }
bsalomonb77a9072016-09-07 10:02:04 -0700134 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700135
136private:
robertphillips77a2e522015-10-17 07:43:27 -0700137 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700138};
139
Greg Daniele20fcad2020-01-08 11:52:34 -0500140std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
141 GrRecordingContext* context,
142 GrColorType colorType,
143 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500144 sk_sp<GrSurfaceProxy> proxy,
145 GrSurfaceOrigin origin,
146 const SkSurfaceProps* surfaceProps,
147 bool managedOps) {
148 if (!proxy) {
149 return nullptr;
150 }
151
152 const GrBackendFormat& format = proxy->backendFormat();
153 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
154 GrSwizzle outSwizzle = context->priv().caps()->getOutputSwizzle(format, colorType);
155
Greg Daniel3912a4b2020-01-14 09:56:04 -0500156 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
157 GrSurfaceProxyView outputView(std::move(proxy), origin, outSwizzle);
158
159 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
160 std::move(outputView), colorType,
161 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500162}
163
164std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
165 GrRecordingContext* context,
166 GrColorType colorType,
167 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500168 SkBackingFit fit,
169 const SkISize& dimensions,
170 const GrBackendFormat& format,
171 int sampleCnt,
172 GrMipMapped mipMapped,
173 GrProtected isProtected,
174 GrSurfaceOrigin origin,
175 SkBudgeted budgeted,
176 const SkSurfaceProps* surfaceProps) {
177 // It is probably not necessary to check if the context is abandoned here since uses of the
178 // GrRenderTargetContext which need the context will mostly likely fail later on without an
179 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
180 // an abandoned context correctly. It also lets us early out of some extra work.
181 if (context->priv().abandoned()) {
182 return nullptr;
183 }
184 auto config = context->priv().caps()->getConfigFromBackendFormat(format, colorType);
185 if (config == kUnknown_GrPixelConfig) {
186 return nullptr;
187 }
188 GrSurfaceDesc desc;
189 desc.fWidth = dimensions.width();
190 desc.fHeight = dimensions.height();
191 desc.fConfig = config;
192
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
194 format, desc, GrRenderable::kYes, sampleCnt, origin, mipMapped, fit, budgeted,
195 isProtected);
196 if (!proxy) {
197 return nullptr;
198 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500199
Greg Danielba0ff782020-01-07 15:42:57 -0500200 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
201 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500202 if (!rtc) {
203 return nullptr;
204 }
205 rtc->discard();
206 return rtc;
207}
208
209std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
210 GrRecordingContext* context,
211 GrColorType colorType,
212 sk_sp<SkColorSpace> colorSpace,
213 SkBackingFit fit,
214 const SkISize& dimensions,
215 int sampleCnt,
216 GrMipMapped mipMapped,
217 GrProtected isProtected,
218 GrSurfaceOrigin origin,
219 SkBudgeted budgeted,
220 const SkSurfaceProps* surfaceProps) {
221 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
222 if (!format.isValid()) {
223 return nullptr;
224 }
225
226 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
227 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
228 surfaceProps);
229}
230
231static inline GrColorType color_type_fallback(GrColorType ct) {
232 switch (ct) {
233 // kRGBA_8888 is our default fallback for many color types that may not have renderable
234 // backend formats.
235 case GrColorType::kAlpha_8:
236 case GrColorType::kBGR_565:
237 case GrColorType::kABGR_4444:
238 case GrColorType::kBGRA_8888:
239 case GrColorType::kRGBA_1010102:
240 case GrColorType::kRGBA_F16:
241 case GrColorType::kRGBA_F16_Clamped:
242 return GrColorType::kRGBA_8888;
243 case GrColorType::kAlpha_F16:
244 return GrColorType::kRGBA_F16;
245 case GrColorType::kGray_8:
246 return GrColorType::kRGB_888x;
247 default:
248 return GrColorType::kUnknown;
249 }
250}
251
252std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
253 GrRecordingContext* context,
254 GrColorType colorType,
255 sk_sp<SkColorSpace> colorSpace,
256 SkBackingFit fit,
257 const SkISize& dimensions,
258 int sampleCnt,
259 GrMipMapped mipMapped,
260 GrProtected isProtected,
261 GrSurfaceOrigin origin,
262 SkBudgeted budgeted,
263 const SkSurfaceProps* surfaceProps) {
264 std::unique_ptr<GrRenderTargetContext> rtc;
265 do {
266 rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
267 sampleCnt, mipMapped, isProtected, origin, budgeted,
268 surfaceProps);
269 colorType = color_type_fallback(colorType);
270 } while (!rtc && colorType != GrColorType::kUnknown);
271 return rtc;
272}
273
Greg Danielba0ff782020-01-07 15:42:57 -0500274std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
275 GrRecordingContext* context,
276 GrColorType colorType,
277 sk_sp<SkColorSpace> colorSpace,
278 const GrBackendTexture& tex,
279 int sampleCnt,
280 GrSurfaceOrigin origin,
281 const SkSurfaceProps* surfaceProps,
282 ReleaseProc releaseProc,
283 ReleaseContext releaseCtx) {
284 SkASSERT(sampleCnt > 0);
285 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
286 tex, origin, sampleCnt, colorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
287 releaseProc, releaseCtx));
288 if (!proxy) {
289 return nullptr;
290 }
291
292 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
293 origin, surfaceProps);
294}
295
296std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
297 GrRecordingContext* context,
298 GrColorType colorType,
299 sk_sp<SkColorSpace> colorSpace,
300 const GrBackendTexture& tex,
301 int sampleCnt,
302 GrSurfaceOrigin origin,
303 const SkSurfaceProps* surfaceProps) {
304 SkASSERT(sampleCnt > 0);
305 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(
306 tex, colorType, origin, sampleCnt));
307 if (!proxy) {
308 return nullptr;
309 }
310
311 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
312 origin, surfaceProps);
313}
314
315std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
316 GrRecordingContext* context,
317 GrColorType colorType,
318 sk_sp<SkColorSpace> colorSpace,
319 const GrBackendRenderTarget& rt,
320 GrSurfaceOrigin origin,
321 const SkSurfaceProps* surfaceProps,
322 ReleaseProc releaseProc,
323 ReleaseContext releaseCtx) {
324 sk_sp<GrSurfaceProxy> proxy(context->priv().proxyProvider()->wrapBackendRenderTarget(
325 rt, colorType, origin, releaseProc, releaseCtx));
326 if (!proxy) {
327 return nullptr;
328 }
329
330 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
331 origin, surfaceProps);
332}
333
334std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
335 GrRecordingContext* context,
336 const SkImageInfo& imageInfo,
337 const GrVkDrawableInfo& vkInfo,
338 const SkSurfaceProps* props) {
339 sk_sp<GrSurfaceProxy> proxy(
340 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
341 vkInfo));
342 if (!proxy) {
343 return nullptr;
344 }
345
346 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
347 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
348 imageInfo.refColorSpace(), std::move(proxy),
349 kTopLeft_GrSurfaceOrigin, props);
350}
351
Greg Danielf41b2bd2019-08-22 16:19:24 -0400352// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
353// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
354// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
355// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500356GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500357 GrSurfaceProxyView readView,
358 GrSurfaceProxyView outputView,
Brian Salomond6287472019-06-24 15:50:07 -0400359 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400360 sk_sp<SkColorSpace> colorSpace,
361 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400362 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500363 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
364 std::move(colorSpace))
365 , fOutputView(std::move(outputView))
366 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400367 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368 , fManagedOpsTask(managedOpsTask) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 SkASSERT(this->asSurfaceProxy() == fOutputView.proxy());
370 SkASSERT(this->origin() == fOutputView.origin());
371
Brian Salomonf18b1d82017-10-27 11:30:49 -0400372 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700373 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700374}
375
robertphillips2e1e51f2015-10-15 08:01:48 -0700376#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500377void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400378 if (fOpsTask && !fOpsTask->isClosed()) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500379 SkASSERT(fOutputView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700380 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700381}
382#endif
383
Brian Osman11052242016-10-27 14:47:55 -0400384GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800385 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700386}
387
Chris Dalton7d6748e2019-03-13 00:34:52 -0600388inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600389 if (GrAA::kNo == aa) {
390 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
391 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600392 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600393 return GrAAType::kMSAA;
394 }
395 return GrAAType::kNone;
396 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600397 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600398}
399
Greg Daniele252f082017-10-23 16:05:23 -0400400GrMipMapped GrRenderTargetContext::mipMapped() const {
401 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
402 return proxy->mipMapped();
403 }
404 return GrMipMapped::kNo;
405}
406
Greg Danielf41b2bd2019-08-22 16:19:24 -0400407GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800408 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700409 SkDEBUGCODE(this->validate();)
410
Greg Danielf41b2bd2019-08-22 16:19:24 -0400411 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600412 sk_sp<GrOpsTask> newOpsTask =
Greg Daniel16f5c652019-10-29 11:26:01 -0400413 this->drawingManager()->newOpsTask(this->outputSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600414 if (fOpsTask && fNumStencilSamples > 0) {
415 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600416 fOpsTask->setMustPreserveStencil();
417 // Reload the stencil buffer content at the beginning of newOpsTask.
418 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
419 // values?
420 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
421 }
422 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700423 }
424
Greg Danielf41b2bd2019-08-22 16:19:24 -0400425 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700426}
427
Herb Derbycddab252018-07-16 11:19:04 -0400428void GrRenderTargetContext::drawGlyphRunList(
429 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400430 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800431 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700432 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700433 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400434 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700435
Greg Danielbe7fc462019-01-03 16:40:42 -0500436 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
437 // secondary command buffers because it would require stopping and starting a render pass which
438 // we don't have access to.
439 if (this->wrapsVkSecondaryCB()) {
440 return;
441 }
442
Herb Derby26cbe512018-05-24 14:39:01 -0400443 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400444 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400445 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700446}
447
Brian Osman11052242016-10-27 14:47:55 -0400448void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800449 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700450 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700451 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400452 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700453
Robert Phillips72152832017-01-25 17:31:35 -0500454 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400455
Greg Danielf41b2bd2019-08-22 16:19:24 -0400456 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700457}
458
Brian Osman11052242016-10-27 14:47:55 -0400459void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500460 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700461 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800462 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700463 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700464 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400465 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700466
Robert Phillips72152832017-01-25 17:31:35 -0500467 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700468 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
469 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700470}
robertphillips9199a9f2016-07-13 07:48:43 -0700471
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500472void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
473 const SkPMColor4f& color,
474 CanClearFullscreen canClearFullscreen) {
475 ASSERT_SINGLE_OWNER_PRIV
476 RETURN_IF_ABANDONED_PRIV
477 SkDEBUGCODE(fRenderTargetContext->validate();)
478 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
479 fRenderTargetContext->fContext);
480
481 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
482 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
483}
484
485static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
486 paint->setColor4f(color);
487 if (color.isOpaque()) {
488 // Can just rely on the src-over blend mode to do the right thing
489 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
490 } else {
491 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
492 // were src blended
493 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
494 }
495}
496
497void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
498 const SkPMColor4f& color,
499 CanClearFullscreen canClearFullscreen) {
500 bool isFull = false;
501 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400502 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
503 // only clear the entire target if we knew it had not been cleared before. As
504 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500505 isFull = !clip.scissorEnabled() ||
506 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400507 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500508 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
509 }
510
511 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400512 GrOpsTask* opsTask = this->getOpsTask();
513 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500514 !this->caps()->performColorClearsAsDraws()) {
515 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400516 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500517 return;
518 } else {
519 // Will use an op for the clear, reset the load op to discard since the op will
520 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400521 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500522 }
523
524 // Must add an op to the list (either because we couldn't use a load op, or because the
525 // clear load op isn't supported)
526 if (this->caps()->performColorClearsAsDraws()) {
527 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
528 GrPaint paint;
529 clear_to_grpaint(color, &paint);
530 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400531 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
532 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500533 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600534 this->addOp(GrClearOp::Make(
535 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500536 }
537 } else {
538 if (this->caps()->performPartialClearsAsDraws()) {
539 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500540 GrPaint paint;
541 clear_to_grpaint(color, &paint);
542
Michael Ludwig64b28a72019-05-28 12:02:00 -0400543 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400544 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
545 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500546 } else {
547 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
548 this->asSurfaceProxy()));
549 // This version of the clear op factory can return null if the clip doesn't intersect
550 // with the surface proxy's boundary
551 if (!op) {
552 return;
553 }
Chris Dalton08755122019-08-05 16:13:47 -0600554 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 }
556 }
557}
558
Brian Osman11052242016-10-27 14:47:55 -0400559void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500560 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400561 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000562 // Start with the render target, since that is the maximum content we could possibly fill.
563 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500564 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400565 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000566 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
567 // inverse view matrix.
568 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
569 } else {
570 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
571 SkMatrix localMatrix;
572 if (!viewMatrix.invert(&localMatrix)) {
573 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400574 }
Michael Ludwig61328202019-06-19 14:48:58 +0000575 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
576 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700577 }
robertphillipsea461502015-05-26 11:38:03 -0700578}
579
Michael Ludwig61328202019-06-19 14:48:58 +0000580enum class GrRenderTargetContext::QuadOptimization {
581 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
582 kDiscarded,
583 // The rect to draw was converted to some other op and appended to the oplist, so no additional
584 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
585 // a constColor is provided.
586 kSubmitted,
587 // The clip was folded into the device quad, with updated edge flags and local coords, and
588 // caller is responsible for adding an appropriate op.
589 kClipApplied,
590 // No change to clip, but quad updated to better fit clip/render target, and caller is
591 // responsible for adding an appropriate op.
592 kCropped
593};
Michael Ludwig61a16512019-01-15 11:15:13 -0500594
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400595static bool make_vertex_finite(float* value) {
596 if (SkScalarIsNaN(*value)) {
597 return false;
598 }
599
600 if (!SkScalarIsFinite(*value)) {
601 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
602 // left when calculating crops.
603 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
604 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
605 }
606
607 return true;
608}
609
Michael Ludwig61328202019-06-19 14:48:58 +0000610GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400611 const GrClip& clip, const SkPMColor4f* constColor,
612 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
613 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000614 // Optimization requirements:
615 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
616 // 2. kClear applies when constColor and final geom is pixel aligned rect;
617 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
618 // 3. kRRect applies when constColor and rrect clip and quad covers clip
619 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
620 // 5. kCropped applies when rect quad (currently)
621 // 6. kNone always applies
622 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000623
Michael Ludwige08b4432019-06-19 18:00:48 -0400624 SkRect rtRect;
625 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400626 // Must use size at which the rendertarget will ultimately be allocated so that stencil
627 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500628 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400629 } else {
630 // Use the logical size of the render target, which allows for "fullscreen" clears even if
631 // the render target has an approximate backing fit
632 rtRect = SkRect::MakeWH(this->width(), this->height());
633 }
634
Michael Ludwig61328202019-06-19 14:48:58 +0000635 SkRect drawBounds = deviceQuad->bounds();
636 if (constColor) {
637 // Don't bother updating local coordinates when the paint will ignore them anyways
638 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400639 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
640 // will be cropped to the finite 'clip' or render target and there is no local space mapping
641 if (!deviceQuad->isFinite()) {
642 for (int i = 0; i < 4; ++i) {
643 if (!make_vertex_finite(deviceQuad->xs() + i) ||
644 !make_vertex_finite(deviceQuad->ys() + i) ||
645 !make_vertex_finite(deviceQuad->ws() + i)) {
646 // Discard if we see a nan
647 return QuadOptimization::kDiscarded;
648 }
649 }
650 SkASSERT(deviceQuad->isFinite());
651 }
652 } else {
653 // CropToRect requires the quads to be finite. If they are not finite and we have local
654 // coordinates, the mapping from local space to device space is poorly defined so drop it
655 if (!deviceQuad->isFinite()) {
656 return QuadOptimization::kDiscarded;
657 }
Brian Salomon7694b902019-06-18 21:00:21 +0000658 }
659
Michael Ludwig61328202019-06-19 14:48:58 +0000660 // If the quad is entirely off screen, it doesn't matter what the clip does
661 if (!rtRect.intersects(drawBounds)) {
662 return QuadOptimization::kDiscarded;
663 }
Brian Salomon7694b902019-06-18 21:00:21 +0000664
Michael Ludwig61328202019-06-19 14:48:58 +0000665 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
666 // the render target).
667 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400668 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
669 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
670 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
671 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000672 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000673 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000674 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
675 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000676 }
Brian Salomon7694b902019-06-18 21:00:21 +0000677 }
678
Michael Ludwig61328202019-06-19 14:48:58 +0000679 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
680 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400681 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000682 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
683 // of the render target and the clip rect)
684 SkRect clipBounds = rtRect;
685 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
686 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000687 }
688
Michael Ludwig61328202019-06-19 14:48:58 +0000689 if (clipRRect.isRect()) {
690 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
691 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
692 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
693 // Clear optimization is possible
694 drawBounds = deviceQuad->bounds();
695 if (drawBounds.contains(rtRect)) {
696 // Fullscreen clear
697 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
698 return QuadOptimization::kSubmitted;
699 } else if (GrClip::IsPixelAligned(drawBounds) &&
700 drawBounds.width() > 256 && drawBounds.height() > 256) {
701 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
702 SkIRect scissorRect;
703 drawBounds.round(&scissorRect);
704 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
705 return QuadOptimization::kSubmitted;
706 }
707 }
708
709 // Update overall AA setting.
710 *edgeFlags = newFlags;
711 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
712 newFlags != GrQuadAAFlags::kNone) {
713 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
714 // properly reflect the smooth edge of the clip.
715 *aa = GrAA::kYes;
716 }
717 // We intentionally do not downgrade AA here because we don't know if we need to
718 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
719 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
720
721 // deviceQuad is exactly the intersection of original quad and clip, so it can be
722 // drawn with no clip (submitted by caller)
723 return QuadOptimization::kClipApplied;
724 } else {
725 // The quads have been updated to better fit the clip bounds, but can't get rid of
726 // the clip entirely
727 return QuadOptimization::kCropped;
728 }
729 } else if (constColor) {
730 // Rounded corners and constant filled color (limit ourselves to solid colors because
731 // there is no way to use custom local coordinates with drawRRect).
732 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
733 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
734 deviceQuad->bounds().contains(clipBounds)) {
735 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
736 // we can draw the rrect directly and ignore the edge flags
737 GrPaint paint;
738 clear_to_grpaint(*constColor, &paint);
739 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
740 clipRRect, GrStyle::SimpleFill());
741 return QuadOptimization::kSubmitted;
742 } else {
743 // The quad has been updated to better fit clip bounds, but can't remove the clip
744 return QuadOptimization::kCropped;
745 }
Brian Salomon7694b902019-06-18 21:00:21 +0000746 }
Brian Salomon7694b902019-06-18 21:00:21 +0000747 }
748
Michael Ludwig61328202019-06-19 14:48:58 +0000749 // Crop the quad to the conservative bounds of the clip.
750 SkIRect clipDevBounds;
751 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
752 SkRect clipBounds = SkRect::Make(clipDevBounds);
753
754 // One final check for discarding, since we may have gone here directly due to a complex clip
755 if (!clipBounds.intersects(drawBounds)) {
756 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000757 }
758
Michael Ludwig61328202019-06-19 14:48:58 +0000759 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
760 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
761 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
762
763 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000764}
765
Michael Ludwig61328202019-06-19 14:48:58 +0000766void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500767 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500768 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000769 GrQuadAAFlags edgeFlags,
770 const GrQuad& deviceQuad,
771 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400772 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000773 ASSERT_SINGLE_OWNER
774 RETURN_IF_ABANDONED
775 SkDEBUGCODE(this->validate();)
776 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500777
Michael Ludwig61328202019-06-19 14:48:58 +0000778 AutoCheckFlush acf(this->drawingManager());
779
780 SkPMColor4f* constColor = nullptr;
781 SkPMColor4f paintColor;
782 if (!ss && !paint.numCoverageFragmentProcessors() &&
783 paint.isConstantBlendedColor(&paintColor)) {
784 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
785 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500786 }
787
Michael Ludwig61328202019-06-19 14:48:58 +0000788 GrQuad croppedDeviceQuad = deviceQuad;
789 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400790 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
791 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000792 if (opt >= QuadOptimization::kClipApplied) {
793 // These optimizations require caller to add an op themselves
794 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
795 : clip;
796 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
797 : this->chooseAAType(aa);
798 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
799 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700800 }
Michael Ludwig61328202019-06-19 14:48:58 +0000801 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800802}
803
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000804void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400805 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500806 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000807 sk_sp<GrColorSpaceXform> textureXform,
808 GrSamplerState::Filter filter,
809 const SkPMColor4f& color,
810 SkBlendMode blendMode,
811 GrAA aa,
812 GrQuadAAFlags edgeFlags,
813 const GrQuad& deviceQuad,
814 const GrQuad& localQuad,
815 const SkRect* domain) {
816 ASSERT_SINGLE_OWNER
817 RETURN_IF_ABANDONED
818 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400819 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000820 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
821
822 AutoCheckFlush acf(this->drawingManager());
823
824 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
825 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
826 GrQuad croppedDeviceQuad = deviceQuad;
827 GrQuad croppedLocalQuad = localQuad;
828 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
829 &croppedDeviceQuad, &croppedLocalQuad);
830
831 SkASSERT(opt != QuadOptimization::kSubmitted);
832 if (opt != QuadOptimization::kDiscarded) {
833 // And the texture op if not discarded
834 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
835 : clip;
836 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400837 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400838 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
839 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000840 // Use the provided domain, although hypothetically we could detect that the cropped local
841 // quad is sufficiently inside the domain and the constraint could be dropped.
Brian Salomonfc118442019-11-22 19:09:27 -0500842 this->addDrawOp(
843 finalClip,
844 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
845 std::move(textureXform), filter, color, saturate, blendMode,
846 aaType, edgeFlags, croppedDeviceQuad, croppedLocalQuad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000847 }
848}
849
Brian Osman11052242016-10-27 14:47:55 -0400850void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500851 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500852 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400853 const SkMatrix& viewMatrix,
854 const SkRect& rect,
855 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700856 if (!style) {
857 style = &GrStyle::SimpleFill();
858 }
joshualitt1de610a2016-01-06 08:26:09 -0800859 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700860 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700861 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400862 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700863
bsalomon6663acf2016-05-10 09:14:17 -0700864 // Path effects should've been devolved to a path in SkGpuDevice
865 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700866
Robert Phillips72152832017-01-25 17:31:35 -0500867 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700868
bsalomon6663acf2016-05-10 09:14:17 -0700869 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400870 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000871 // Fills the rect, using rect as its own local coordinates
872 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500873 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700874 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
875 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
876 if ((!rect.width() || !rect.height()) &&
877 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
878 SkScalar r = stroke.getWidth() / 2;
879 // TODO: Move these stroke->fill fallbacks to GrShape?
880 switch (stroke.getJoin()) {
881 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500882 this->drawRect(
883 clip, std::move(paint), aa, viewMatrix,
884 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
885 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700886 return;
887 case SkPaint::kRound_Join:
888 // Raster draws nothing when both dimensions are empty.
889 if (rect.width() || rect.height()){
890 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500891 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
892 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700893 return;
894 }
895 case SkPaint::kBevel_Join:
896 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500897 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700898 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
899 &GrStyle::SimpleFill());
900 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500901 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700902 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
903 &GrStyle::SimpleFill());
904 }
905 return;
906 }
907 }
robertphillips44302392016-07-08 14:43:03 -0700908
Brian Salomonbaaf4392017-06-15 09:59:23 -0400909 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700910
Chris Dalton7d6748e2019-03-13 00:34:52 -0600911 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500912 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
913 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
914 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500915 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400916 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700917 return;
robertphillips4bc31812016-03-01 12:22:49 -0800918 }
robertphillips4bc31812016-03-01 12:22:49 -0800919 }
Mike Klein16885072018-12-11 09:54:31 -0500920 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500921 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700922}
923
Michael Ludwig69858532018-11-28 15:34:34 -0500924void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
925 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
926 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600927 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500928
929 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
930 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500931}
932
Robert Phillipsec2249f2016-11-09 08:54:35 -0500933int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500934 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400935 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500936}
937
Greg Danielf41b2bd2019-08-22 16:19:24 -0400938GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600939 ) const {
940#if GR_TEST_UTILS
941 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400942 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600943 }
944#endif
945 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
946 // would normally be overwritten. The one exception is if the render target context is marked as
947 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
948 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
949 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
950 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400951 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600952}
953
Chris Dalton858cf232019-10-14 16:20:00 -0600954void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600955 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600956 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600957 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600958
Chris Daltoneffee202019-07-01 22:28:03 -0600959 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600960 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500961 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600962 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400963 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600964 }
965 SkASSERT(numRequiredSamples > 0);
966
967 if (numRequiredSamples > fNumStencilSamples) {
968 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500969 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600970 }
Chris Dalton6b982802019-06-27 13:53:46 -0600971
Chris Dalton674f77a2019-09-30 20:49:39 -0600972 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600973 if (this->caps()->performStencilClearsAsDraws()) {
974 // There is a driver bug with clearing stencil. We must use an op to manually clear the
975 // stencil buffer before the op that required 'setNeedsStencil'.
976 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
977 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600978 this->getOpsTask()->setInitialStencilContent(
979 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600980 }
981 }
982}
983
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000984void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700985 ASSERT_SINGLE_OWNER_PRIV
986 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400987 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400988 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
989 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700990
Robert Phillips72152832017-01-25 17:31:35 -0500991 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400992
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500993 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
994}
995
996void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600997 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
998
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500999 if (this->caps()->performStencilClearsAsDraws()) {
1000 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
1001 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
1002
1003 // Configure the paint to have no impact on the color buffer
1004 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001005 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001006 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1007 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001008 } else {
1009 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001010 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001011 if (!op) {
1012 return;
1013 }
Chris Dalton08755122019-08-05 16:13:47 -06001014 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001015 }
robertphillips976f5f02016-06-03 10:59:20 -07001016}
1017
Chris Daltonbbfd5162017-11-07 13:35:22 -07001018void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001019 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001020 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001021 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001022 ASSERT_SINGLE_OWNER_PRIV
1023 RETURN_IF_ABANDONED_PRIV
1024 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001025 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1026 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001027
Brian Salomon467921e2017-03-06 16:17:12 -05001028 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1029 SkASSERT(path);
1030 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1031
1032 // FIXME: Use path bounds instead of this WAR once
1033 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1034 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1035
1036 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001037 GrAppliedHardClip appliedClip;
1038 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1039 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001040 return;
1041 }
1042
Robert Phillips7c525e62018-06-12 10:11:12 -04001043 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1044 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001045 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001046 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001047 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001048 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001049 if (!op) {
1050 return;
1051 }
Brian Salomon97180af2017-03-14 13:42:58 -04001052 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001053
Chris Daltoneffee202019-07-01 22:28:03 -06001054 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001055 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001056}
1057
Michael Ludwig379e4962019-12-06 13:21:26 -05001058void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1059 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001060 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001061 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1062 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001063 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001064 ASSERT_SINGLE_OWNER
1065 RETURN_IF_ABANDONED
1066 SkDEBUGCODE(this->validate();)
1067 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001068
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001069 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1070 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1071 AutoCheckFlush acf(this->drawingManager());
1072 GrAAType aaType = this->chooseAAType(aa);
1073 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1074 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1075 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001076 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1077 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001078}
1079
Brian Osman11052242016-10-27 14:47:55 -04001080void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001081 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001082 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001083 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001084 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001085 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001086 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -05001087 ASSERT_SINGLE_OWNER
1088 RETURN_IF_ABANDONED
1089 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001090 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001091
1092 AutoCheckFlush acf(this->drawingManager());
1093
1094 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001095 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001096 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001097 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001098 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -04001099 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001100}
1101
1102///////////////////////////////////////////////////////////////////////////////
1103
Brian Osman4d92b892019-03-24 00:53:23 +00001104void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1105 GrPaint&& paint,
1106 const SkMatrix& viewMatrix,
1107 int spriteCount,
1108 const SkRSXform xform[],
1109 const SkRect texRect[],
1110 const SkColor colors[]) {
1111 ASSERT_SINGLE_OWNER
1112 RETURN_IF_ABANDONED
1113 SkDEBUGCODE(this->validate();)
1114 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1115
1116 AutoCheckFlush acf(this->drawingManager());
1117
1118 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1119 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1120 aaType, spriteCount, xform, texRect, colors);
1121 this->addDrawOp(clip, std::move(op));
1122}
1123
1124///////////////////////////////////////////////////////////////////////////////
1125
Brian Osman11052242016-10-27 14:47:55 -04001126void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001127 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001128 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001129 const SkMatrix& viewMatrix,
1130 const SkRRect& rrect,
1131 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001132 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001133 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001134 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001135 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001136
1137 const SkStrokeRec& stroke = style.strokeRec();
1138 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001139 return;
1140 }
1141
bsalomon7f0d9f32016-08-15 14:49:10 -07001142 GrNoClip noclip;
1143 const GrClip* clip = &origClip;
1144#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1145 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001146 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001147 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1148 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001149 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1150 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001151 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001152 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1153 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001154 clip = &noclip;
1155 }
1156#endif
bsalomon6663acf2016-05-10 09:14:17 -07001157 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001158
Robert Phillips72152832017-01-25 17:31:35 -05001159 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001160
Chris Dalton7d6748e2019-03-13 00:34:52 -06001161 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001162
Chris Dalton0dffbab2019-03-27 13:08:50 -06001163 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001164 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1165 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1166 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1167 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1168 // to avoid perf regressions on some platforms.
1169 assert_alive(paint);
1170 op = GrOvalOpFactory::MakeCircularRRectOp(
1171 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1172 }
1173 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001174 assert_alive(paint);
1175 op = GrFillRRectOp::Make(
1176 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1177 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001178 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001179 assert_alive(paint);
1180 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001181 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001182 }
1183 if (op) {
1184 this->addDrawOp(*clip, std::move(op));
1185 return;
robertphillipsea461502015-05-26 11:38:03 -07001186 }
robertphillipsb56f9272016-02-25 11:03:52 -08001187
Mike Klein16885072018-12-11 09:54:31 -05001188 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001189 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1190 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001191}
1192
Jim Van Verthc5903412016-11-17 15:27:09 -05001193///////////////////////////////////////////////////////////////////////////////
1194
Jim Van Verth3af1af92017-05-18 15:06:54 -04001195static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1196 SkPoint3 result;
1197 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1198 result.fZ = pt.fZ;
1199 return result;
1200}
1201
1202bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203 const SkMatrix& viewMatrix,
1204 const SkPath& path,
1205 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001206 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001207 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001208 return true;
1209 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001210 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001211 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001212
1213 // check z plane
1214 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1215 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1216 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1217 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1218 return false;
1219 }
1220
1221 SkRRect rrect;
1222 SkRect rect;
1223 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001224 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001225 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1226 if (!isRRect &&
1227 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1228 rect.width() > SK_ScalarNearlyZero) {
1229 rrect.setOval(rect);
1230 isRRect = true;
1231 }
1232 if (!isRRect && path.isRect(&rect)) {
1233 rrect.setRect(rect);
1234 isRRect = true;
1235 }
1236
1237 if (!isRRect) {
1238 return false;
1239 }
1240
Jim Van Verthc5903412016-11-17 15:27:09 -05001241 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001242 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001243 }
1244
Robert Phillips72152832017-01-25 17:31:35 -05001245 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001246
Jim Van Verth3af1af92017-05-18 15:06:54 -04001247 // transform light
1248 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1249
1250 // 1/scale
1251 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001252 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001253 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1254 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1255
1256 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001257 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1258
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001259 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001260 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1261 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1262 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001263
1264 // Outset the shadow rrect to the border of the penumbra
1265 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1266 SkRRect ambientRRect;
1267 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1268 // If the rrect was an oval then its outset will also be one.
1269 // We set it explicitly to avoid errors.
1270 if (rrect.isOval()) {
1271 ambientRRect = SkRRect::MakeOval(outsetRect);
1272 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001273 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001274 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1275 }
1276
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001277 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001278 if (transparent) {
1279 // set a large inset to force a fill
1280 devSpaceInsetWidth = ambientRRect.width();
1281 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001282
Robert Phillips7c525e62018-06-12 10:11:12 -04001283 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1284 ambientColor,
1285 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001286 ambientRRect,
1287 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001288 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001289 if (op) {
1290 this->addDrawOp(clip, std::move(op));
1291 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001292 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001293
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001294 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001295 SkScalar devSpaceSpotBlur;
1296 SkScalar spotScale;
1297 SkVector spotOffset;
1298 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1299 devLightPos.fZ, rec.fLightRadius,
1300 &devSpaceSpotBlur, &spotScale, &spotOffset);
1301 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001302 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1303
Jim Van Verth3af1af92017-05-18 15:06:54 -04001304 // Adjust translate for the effect of the scale.
1305 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1306 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1307 // This offset is in dev space, need to transform it into source space.
1308 SkMatrix ctmInverse;
1309 if (viewMatrix.invert(&ctmInverse)) {
1310 ctmInverse.mapPoints(&spotOffset, 1);
1311 } else {
1312 // Since the matrix is a similarity, this should never happen, but just in case...
1313 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1314 SkASSERT(false);
1315 }
1316
1317 // Compute the transformed shadow rrect
1318 SkRRect spotShadowRRect;
1319 SkMatrix shadowTransform;
1320 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1321 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001322 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001323
1324 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001325 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001326 SkScalar insetWidth = blurOutset;
1327 if (transparent) {
1328 // If transparent, just do a fill
1329 insetWidth += spotShadowRRect.width();
1330 } else {
1331 // For shadows, instead of using a stroke we specify an inset from the penumbra
1332 // border. We want to extend this inset area so that it meets up with the caster
1333 // geometry. The inset geometry will by default already be inset by the blur width.
1334 //
1335 // We compare the min and max corners inset by the radius between the original
1336 // rrect and the shadow rrect. The distance between the two plus the difference
1337 // between the scaled radius and the original radius gives the distance from the
1338 // transformed shadow shape to the original shape in that corner. The max
1339 // of these gives the maximum distance we need to cover.
1340 //
1341 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1342 // that to get the full insetWidth.
1343 SkScalar maxOffset;
1344 if (rrect.isRect()) {
1345 // Manhattan distance works better for rects
1346 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1347 rrect.rect().fLeft),
1348 SkTAbs(spotShadowRRect.rect().fTop -
1349 rrect.rect().fTop)),
1350 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1351 rrect.rect().fRight),
1352 SkTAbs(spotShadowRRect.rect().fBottom -
1353 rrect.rect().fBottom)));
1354 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001355 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001356 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1357 rrect.rect().fLeft + dr,
1358 spotShadowRRect.rect().fTop -
1359 rrect.rect().fTop + dr);
1360 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1361 rrect.rect().fRight - dr,
1362 spotShadowRRect.rect().fBottom -
1363 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001364 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1365 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001366 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001367 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001368 }
1369
1370 // Outset the shadow rrect to the border of the penumbra
1371 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1372 if (spotShadowRRect.isOval()) {
1373 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1374 } else {
1375 SkScalar outsetRad = spotRadius + blurOutset;
1376 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1377 }
1378
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001379 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001380
Robert Phillips7c525e62018-06-12 10:11:12 -04001381 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1382 spotColor,
1383 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001384 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001385 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001386 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001387 if (op) {
1388 this->addDrawOp(clip, std::move(op));
1389 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001390 }
1391
1392 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001393}
1394
1395///////////////////////////////////////////////////////////////////////////////
1396
Brian Osman11052242016-10-27 14:47:55 -04001397bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001398 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001399 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001400 const SkMatrix& viewMatrix,
1401 const SkRRect& origOuter,
1402 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001403 SkASSERT(!origInner.isEmpty());
1404 SkASSERT(!origOuter.isEmpty());
1405
Brian Salomon65749212017-12-01 16:01:47 -05001406 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1407
Chris Dalton7d6748e2019-03-13 00:34:52 -06001408 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001409
1410 if (GrAAType::kMSAA == aaType) {
1411 return false;
1412 }
1413
Greg Daniel2655ede2019-04-10 00:49:28 +00001414 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1415 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001416 auto outerR = outer->width() / 2.f;
1417 auto innerR = inner->width() / 2.f;
1418 auto cx = outer->getBounds().fLeft + outerR;
1419 auto cy = outer->getBounds().fTop + outerR;
1420 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1421 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1422 auto avgR = (innerR + outerR) / 2.f;
1423 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1424 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1425 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001426 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001427 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001428 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001429 if (op) {
1430 this->addDrawOp(clip, std::move(op));
1431 return true;
1432 }
Mike Klein16885072018-12-11 09:54:31 -05001433 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001434 }
1435 }
1436
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001437 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001438 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001439 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1440 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001441 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001442 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1443 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001444 }
robertphillips00095892016-02-29 13:50:40 -08001445
robertphillips00095892016-02-29 13:50:40 -08001446 SkMatrix inverseVM;
1447 if (!viewMatrix.isIdentity()) {
1448 if (!origInner.transform(viewMatrix, inner.writable())) {
1449 return false;
1450 }
1451 if (!origOuter.transform(viewMatrix, outer.writable())) {
1452 return false;
1453 }
1454 if (!viewMatrix.invert(&inverseVM)) {
1455 return false;
1456 }
1457 } else {
1458 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001459 }
robertphillips00095892016-02-29 13:50:40 -08001460
Ethan Nicholaseace9352018-10-15 20:09:54 +00001461 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001462 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001463 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001464 if (!innerEffect) {
1465 return false;
1466 }
1467
Ethan Nicholaseace9352018-10-15 20:09:54 +00001468 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001469 if (!outerEffect) {
1470 return false;
1471 }
1472
Brian Salomon82f44312017-01-11 13:42:54 -05001473 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1474 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001475
1476 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001477 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001478 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1479 }
halcanary9d524f22016-03-29 09:03:52 -07001480
Brian Salomon82f44312017-01-11 13:42:54 -05001481 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1482 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001483 return true;
1484}
1485
Brian Osman11052242016-10-27 14:47:55 -04001486void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001487 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001488 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001489 const SkMatrix& viewMatrix,
1490 const SkRRect& outer,
1491 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001492 ASSERT_SINGLE_OWNER
1493 RETURN_IF_ABANDONED
1494 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001495 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001496
1497 SkASSERT(!outer.isEmpty());
1498 SkASSERT(!inner.isEmpty());
1499
Robert Phillips72152832017-01-25 17:31:35 -05001500 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001501
Brian Salomon82f44312017-01-11 13:42:54 -05001502 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001503 return;
1504 }
Mike Klein16885072018-12-11 09:54:31 -05001505 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001506
1507 SkPath path;
1508 path.setIsVolatile(true);
1509 path.addRRect(inner);
1510 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001511 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001512 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001513}
1514
robertphillipsea461502015-05-26 11:38:03 -07001515///////////////////////////////////////////////////////////////////////////////
1516
Brian Osman11052242016-10-27 14:47:55 -04001517void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001518 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001519 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001520 const SkMatrix& viewMatrix,
1521 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001522 const GrStyle& style,
1523 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001524 ASSERT_SINGLE_OWNER
1525 RETURN_IF_ABANDONED
1526 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001527 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001528
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001529 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001530 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001531 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001532 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001533 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1534 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001535 aa = GrAA::kNo;
1536 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001537 }
msarettcc319b92016-08-25 18:07:18 -07001538 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001539 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001540 SkPath path;
1541 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001542 path.setIsVolatile(true);
1543
Brian Salomon82f44312017-01-11 13:42:54 -05001544 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001545 }
1546
Chris Dalton7d6748e2019-03-13 00:34:52 -06001547 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001548 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1549 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001550 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001551}
1552
Brian Osman11052242016-10-27 14:47:55 -04001553void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001554 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001555 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001556 const SkMatrix& viewMatrix,
1557 const SkRect& oval,
1558 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001559 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001560 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001561 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001562 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001563
Robert Phillips7484d202018-07-03 09:09:08 -04001564 const SkStrokeRec& stroke = style.strokeRec();
1565
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001566 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001567 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1568 return;
1569 }
1570
1571 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001572 return;
robertphillipsea461502015-05-26 11:38:03 -07001573 }
1574
Robert Phillips72152832017-01-25 17:31:35 -05001575 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001576
Chris Dalton7d6748e2019-03-13 00:34:52 -06001577 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001578
1579 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001580 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1581 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001582 // We don't draw true circles as round rects in coverage mode, because it can
1583 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1584 assert_alive(paint);
1585 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1586 this->caps()->shaderCaps());
1587 }
1588 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001589 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1590 // the arc equation. This same special geometry and fragment branch also turn out to be a
1591 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1592 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1593 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001594 assert_alive(paint);
1595 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1596 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001597 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001598 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001599 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001600 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1601 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001602 }
1603 if (op) {
1604 this->addDrawOp(clip, std::move(op));
1605 return;
robertphillipsea461502015-05-26 11:38:03 -07001606 }
robertphillipsb56f9272016-02-25 11:03:52 -08001607
Mike Klein16885072018-12-11 09:54:31 -05001608 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001609 this->drawShapeUsingPathRenderer(
1610 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001611 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001612}
1613
Brian Osman11052242016-10-27 14:47:55 -04001614void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001615 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001616 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001617 const SkMatrix& viewMatrix,
1618 const SkRect& oval,
1619 SkScalar startAngle,
1620 SkScalar sweepAngle,
1621 bool useCenter,
1622 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001623 ASSERT_SINGLE_OWNER
1624 RETURN_IF_ABANDONED
1625 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001626 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001627
1628 AutoCheckFlush acf(this->drawingManager());
1629
Chris Dalton7d6748e2019-03-13 00:34:52 -06001630 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001631 if (GrAAType::kCoverage == aaType) {
1632 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1633 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1634 std::move(paint),
1635 viewMatrix,
1636 oval,
1637 startAngle,
1638 sweepAngle,
1639 useCenter,
1640 style,
1641 shaderCaps);
1642 if (op) {
1643 this->addDrawOp(clip, std::move(op));
1644 return;
1645 }
1646 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001647 }
Brian Salomone4949402018-04-26 15:22:04 -04001648 this->drawShapeUsingPathRenderer(
1649 clip, std::move(paint), aa, viewMatrix,
1650 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001651}
1652
Brian Osman11052242016-10-27 14:47:55 -04001653void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001654 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001655 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001656 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001657 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001658 sk_sp<GrColorSpaceXform> csxf,
1659 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001660 std::unique_ptr<SkLatticeIter> iter,
1661 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001662 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001663 RETURN_IF_ABANDONED
1664 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001665 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001666
Robert Phillips72152832017-01-25 17:31:35 -05001667 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001668
Brian Salomon2a943df2018-05-04 13:43:19 -04001669 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001670 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001671 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001672 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001673}
1674
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001675void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1676 const SkRect& bounds) {
1677 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1678 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001679 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001680}
1681
Brian Salomon031b0ba2019-05-23 11:05:26 -04001682void GrRenderTargetContext::asyncRescaleAndReadPixels(
1683 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1684 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1685 auto direct = fContext->priv().asDirectContext();
1686 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001687 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001688 return;
1689 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001690 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001691 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001692 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001693 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001694 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001695 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001696 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001697 return;
1698 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001699 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001700 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001701 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001702 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001703 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001704 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1705 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001706 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001707 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001708 backendFormatOfFinalContext, dstCT);
1709 // Fail if we can't read from the source surface's color type.
1710 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001711 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001712 return;
1713 }
Brian Salomon624f9062019-07-02 14:23:00 -04001714 // Fail if read color type does not have all of dstCT's color channels and those missing color
1715 // channels are in the src.
1716 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1717 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001718 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon624f9062019-07-02 14:23:00 -04001719 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001720 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001721 return;
1722 }
1723
Brian Salomonbf6b9792019-08-21 09:38:10 -04001724 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001725 int x = srcRect.fLeft;
1726 int y = srcRect.fTop;
1727 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001728 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1729 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001730 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001731 return;
1732 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001733 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001734 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 x = y = 0;
1736 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001737 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1738 this->colorInfo().alphaType(),
1739 info.colorSpace(),
1740 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001741 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1742 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001743 // We flip or color convert by drawing and we don't currently support drawing to
1744 // kPremul.
1745 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001746 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001747 return;
1748 }
Greg Daniel3912a4b2020-01-14 09:56:04 -05001749 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001750 SkRect srcRectToDraw = SkRect::Make(srcRect);
1751 // If the src is not texturable first try to make a copy to a texture.
1752 if (!texProxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -05001753 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(),
Greg Daniel3155f7f2020-01-16 16:54:26 -05001754 this->colorInfo().colorType(), GrMipMapped::kNo,
1755 srcRect, SkBackingFit::kApprox, SkBudgeted::kNo);
Greg Daniel46cfbc62019-06-07 11:43:30 -04001756 if (!texProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001757 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001758 return;
1759 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001760 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1761 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001762 tempRTC = GrRenderTargetContext::Make(
1763 direct, this->colorInfo().colorType(), info.refColorSpace(),
1764 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001765 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001766 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001767 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001768 return;
1769 }
Greg Danielc594e622019-10-15 14:01:49 -04001770 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), this->colorInfo().colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -05001771 this->colorInfo().alphaType(), GrSamplerState::Filter::kNearest,
1772 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001773 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1774 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1775 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001776 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001777 }
1778 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001779 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001780 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1781 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001782}
1783
Brian Salomon9241a6d2019-10-03 13:26:54 -04001784class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1785public:
1786 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1787 ~AsyncReadResult() override {
1788 for (int i = 0; i < fPlanes.count(); ++i) {
1789 if (!fPlanes[i].fMappedBuffer) {
1790 delete[] static_cast<const char*>(fPlanes[i].fData);
1791 } else {
1792 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1793 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1794 }
1795 }
1796 }
1797
1798 int count() const override { return fPlanes.count(); }
1799 const void* data(int i) const override { return fPlanes[i].fData; }
1800 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1801
1802 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001803 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001804 size_t rowBytes,
1805 GrClientMappedBufferManager* manager) {
1806 SkASSERT(!result.fTransferBuffer->isMapped());
1807 const void* mappedData = result.fTransferBuffer->map();
1808 if (!mappedData) {
1809 return false;
1810 }
1811 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001812 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001813 result.fPixelConverter(convertedData.get(), mappedData);
1814 this->addCpuPlane(std::move(convertedData), rowBytes);
1815 result.fTransferBuffer->unmap();
1816 } else {
1817 manager->insert(result.fTransferBuffer);
1818 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1819 }
1820 return true;
1821 }
1822
1823 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1824 SkASSERT(data);
1825 SkASSERT(rowBytes > 0);
1826 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1827 }
1828
1829private:
1830 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1831 SkASSERT(data);
1832 SkASSERT(rowBytes > 0);
1833 SkASSERT(mappedBuffer);
1834 SkASSERT(mappedBuffer->isMapped());
1835 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1836 }
1837
1838 struct Plane {
1839 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1840 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1841 const void* fData;
1842 size_t fRowBytes;
1843 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1844 sk_sp<GrGpuBuffer> fMappedBuffer;
1845 };
1846 SkSTArray<3, Plane> fPlanes;
1847 uint32_t fInboxID;
1848};
1849
Brian Salomon024bd002019-06-11 11:38:16 -04001850void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1851 ReadPixelsCallback callback,
1852 ReadPixelsContext context) {
1853 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1854 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1855
Greg Danielca9dbe22020-01-02 13:44:30 -05001856 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001857 callback(context, nullptr);
1858 return;
1859 }
1860
Brian Salomon9241a6d2019-10-03 13:26:54 -04001861 auto directContext = fContext->priv().asDirectContext();
1862 SkASSERT(directContext);
1863 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1864
Brian Salomon024bd002019-06-11 11:38:16 -04001865 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1866
1867 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001868 auto ii = SkImageInfo::Make(rect.size(), colorType,
1869 this->colorInfo().alphaType(),
1870 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001871 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001872 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1873 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1874 result->addCpuPlane(std::move(data), pm.rowBytes());
1875
Brian Salomon1d435302019-07-01 13:05:28 -04001876 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001877 callback(context, nullptr);
Brian Salomonab32f652019-05-10 14:24:50 -04001878 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001879 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001880 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001881 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001882
Brian Salomonab32f652019-05-10 14:24:50 -04001883 struct FinishContext {
1884 ReadPixelsCallback* fClientCallback;
1885 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001886 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001887 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001888 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001889 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001890 };
1891 // Assumption is that the caller would like to flush. We could take a parameter or require an
1892 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1893 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001894 auto* finishContext = new FinishContext{callback,
1895 context,
1896 rect.size(),
1897 colorType,
1898 mappedBufferManager,
1899 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001900 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001901 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001902 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001903 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1904 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1905 context->fMappedBufferManager)) {
1906 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001907 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001908 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001909 delete context;
1910 };
1911 GrFlushInfo flushInfo;
1912 flushInfo.fFinishedContext = finishContext;
1913 flushInfo.fFinishedProc = finishCallback;
1914 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1915}
1916
Brian Salomon9241a6d2019-10-03 13:26:54 -04001917void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1918 sk_sp<SkColorSpace> dstColorSpace,
1919 const SkIRect& srcRect,
1920 const SkISize& dstSize,
1921 RescaleGamma rescaleGamma,
1922 SkFilterQuality rescaleQuality,
1923 ReadPixelsCallback callback,
1924 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001925 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1926 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001927 SkASSERT(!dstSize.isZero());
1928 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1929
Brian Salomon024bd002019-06-11 11:38:16 -04001930 auto direct = fContext->priv().asDirectContext();
1931 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001932 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001933 return;
1934 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001935 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001936 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001937 return;
1938 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001939 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001940 callback(context, nullptr);
1941 return;
1942 }
Brian Salomon024bd002019-06-11 11:38:16 -04001943 int x = srcRect.fLeft;
1944 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001945 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001946 bool needsRescale = srcRect.size() != dstSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001947 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001948 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001949 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001950 dstColorSpace);
1951 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001952 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1953 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001954 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001955 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001956 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001957 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001958 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001959 x = y = 0;
1960 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001961 // We assume the caller wants kPremul. There is no way to indicate a preference.
1962 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001963 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1964 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001965 if (xform) {
1966 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1967 // TODO: Do something if the input is not a texture already.
1968 if (!texProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001969 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001970 return;
1971 }
Brian Salomon024bd002019-06-11 11:38:16 -04001972 SkRect srcRectToDraw = SkRect::Make(srcRect);
Greg Daniele20fcad2020-01-08 11:52:34 -05001973 tempRTC = GrRenderTargetContext::Make(
1974 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1975 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001976 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001977 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001978 return;
1979 }
Greg Danielc594e622019-10-15 14:01:49 -04001980 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), this->colorInfo().colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -05001981 this->colorInfo().alphaType(), GrSamplerState::Filter::kNearest,
1982 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001983 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1984 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1985 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001986 x = y = 0;
1987 }
1988 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001989 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001990 // TODO: Do something if the input is not a texture already.
1991 if (!srcProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001992 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001993 return;
1994 }
Greg Danielc594e622019-10-15 14:01:49 -04001995
Greg Daniele20fcad2020-01-08 11:52:34 -05001996 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1997 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1998 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001999 int halfW = dstSize.width()/2;
2000 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002001 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2002 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2003 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2004 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2005 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2006 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002007 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002008 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002009 return;
2010 }
2011
Brian Osman28ba5282019-10-30 14:18:07 -04002012 float baseM[20];
2013 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2014
Brian Salomon024bd002019-06-11 11:38:16 -04002015 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2016
2017 auto texMatrix = SkMatrix::MakeTrans(x, y);
2018
Brian Salomon9241a6d2019-10-03 13:26:54 -04002019 SkRect dstRectY = SkRect::Make(dstSize);
2020 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002021
Brian Salomon024bd002019-06-11 11:38:16 -04002022 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2023 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002024 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002025 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002026 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002027 yPaint.addColorFragmentProcessor(
2028 GrTextureEffect::Make(srcProxy, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002029 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2030 yPaint.addColorFragmentProcessor(std::move(yFP));
2031 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002032 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2033 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04002034 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2035 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2036 if (!yTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002037 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002038 return;
2039 }
2040
2041 texMatrix.preScale(2.f, 2.f);
2042 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2043 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002044 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002045 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002046 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002047 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
2048 srcProxy, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002049 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2050 uPaint.addColorFragmentProcessor(std::move(uFP));
2051 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002052 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2053 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04002054 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2055 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2056 if (!uTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002057 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002058 return;
2059 }
2060
2061 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2062 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002063 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002064 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002065 GrPaint vPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002066 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(
2067 srcProxy, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002068 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2069 vPaint.addColorFragmentProcessor(std::move(vFP));
2070 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002071 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2072 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04002073 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2074 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2075 if (!vTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002076 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002077 return;
2078 }
2079
2080 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002081 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002082 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002083 GrClientMappedBufferManager* fMappedBufferManager;
2084 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002085 PixelTransferResult fYTransfer;
2086 PixelTransferResult fUTransfer;
2087 PixelTransferResult fVTransfer;
2088 };
2089 // Assumption is that the caller would like to flush. We could take a parameter or require an
2090 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2091 // callback to GrGpu until after the next flush that flushes our op list, though.
2092 auto* finishContext = new FinishContext{callback,
2093 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002094 direct->priv().clientMappedBufferManager(),
2095 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002096 std::move(yTransfer),
2097 std::move(uTransfer),
2098 std::move(vTransfer)};
2099 auto finishCallback = [](GrGpuFinishedContext c) {
2100 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002101 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002102 auto manager = context->fMappedBufferManager;
2103 size_t rowBytes = SkToSizeT(context->fSize.width());
2104 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2105 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002106 delete context;
2107 return;
2108 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002109 rowBytes /= 2;
2110 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2111 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2112 (*context->fClientCallback)(context->fClientContext, nullptr);
2113 delete context;
2114 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002115 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002116 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2117 (*context->fClientCallback)(context->fClientContext, nullptr);
2118 delete context;
2119 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002120 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002121 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002122 delete context;
2123 };
2124 GrFlushInfo flushInfo;
2125 flushInfo.fFinishedContext = finishContext;
2126 flushInfo.fFinishedProc = finishCallback;
2127 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002128}
2129
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002130GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2131 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002132 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002133 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002134 return GrSemaphoresSubmitted::kNo;
2135 }
robertphillips8c523e02016-07-26 07:41:00 -07002136 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002137 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002138
Greg Daniel3912a4b2020-01-14 09:56:04 -05002139 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002140}
2141
Greg Danielc64ee462017-06-15 16:59:49 -04002142bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002143 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002144 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002145 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002146 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002147 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002148
2149 AutoCheckFlush acf(this->drawingManager());
2150
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002151 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002152 return false;
2153 }
2154
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002155 auto direct = fContext->priv().asDirectContext();
2156 if (!direct) {
2157 return false;
2158 }
2159
2160 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002161
Greg Daniel301015c2019-11-18 14:06:46 -05002162 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2163 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002164 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002165 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002166 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2167 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002168 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002169 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2170 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002171 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002172}
joshualitt33a5fce2015-11-18 13:28:51 -08002173
Brian Osman11052242016-10-27 14:47:55 -04002174void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002175 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002176 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002177 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002178 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002179 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002180 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002181 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002182 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002183 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2184
Brian Salomon40b77a62017-12-22 11:25:52 -05002185 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002186
Robert Phillips27927a52018-08-20 13:18:12 -04002187 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002188}
2189
2190void GrRenderTargetContext::drawShape(const GrClip& clip,
2191 GrPaint&& paint,
2192 GrAA aa,
2193 const SkMatrix& viewMatrix,
2194 const GrShape& shape) {
2195 ASSERT_SINGLE_OWNER
2196 RETURN_IF_ABANDONED
2197 SkDEBUGCODE(this->validate();)
2198 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2199
Brian Salomon2fad74a2017-12-20 13:28:55 -05002200 if (shape.isEmpty()) {
2201 if (shape.inverseFilled()) {
2202 this->drawPaint(clip, std::move(paint), viewMatrix);
2203 }
2204 return;
robertphillipsea461502015-05-26 11:38:03 -07002205 }
2206
Robert Phillips72152832017-01-25 17:31:35 -05002207 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002208
Brian Salomon2fad74a2017-12-20 13:28:55 -05002209 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002210 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002211 SkRRect rrect;
2212 // We can ignore the starting point and direction since there is no path effect.
2213 bool inverted;
2214 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2215 if (rrect.isRect()) {
2216 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2217 &shape.style());
2218 return;
2219 } else if (rrect.isOval()) {
2220 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002221 return;
2222 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002223 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2224 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002225 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2226 viewMatrix.rectStaysRect()) {
2227 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2228 // the matrix to all the points individually rather than just to the rect
2229 SkRect rects[2];
2230 if (shape.asNestedRects(rects)) {
2231 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002232 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002233 fContext, std::move(paint), viewMatrix, rects);
2234 if (op) {
2235 this->addDrawOp(clip, std::move(op));
2236 }
2237 // Returning here indicates that there is nothing to draw in this case.
2238 return;
2239 }
robertphillipsea461502015-05-26 11:38:03 -07002240 }
2241 }
robertphillips4bc31812016-03-01 12:22:49 -08002242
Brian Salomon2fad74a2017-12-20 13:28:55 -05002243 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002244}
2245
Chris Daltonbbfd5162017-11-07 13:35:22 -07002246bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002247 const GrUserStencilSettings* ss,
2248 SkRegion::Op op,
2249 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002250 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002251 const SkMatrix& viewMatrix,
2252 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002253 ASSERT_SINGLE_OWNER_PRIV
2254 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002255 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002256 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2257 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002258
2259 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002260 GrPaint paint;
2261 paint.setCoverageSetOpXPFactory(op, invert);
2262 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2263 SkRect::MakeIWH(fRenderTargetContext->width(),
2264 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002265 return true;
2266 }
2267
Robert Phillips72152832017-01-25 17:31:35 -05002268 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002269
2270 // An Assumption here is that path renderer would use some form of tweaking
2271 // the src color (either the input alpha or in the frag shader) to implement
2272 // aa. If we have some future driver-mojo path AA that can do the right
2273 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002274 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002275 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002276
Chris Daltondb91c6e2017-09-08 16:25:08 -06002277 SkIRect clipConservativeBounds;
2278 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2279 &clipConservativeBounds, nullptr);
2280
bsalomon8acedde2016-06-24 10:42:16 -07002281 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002282 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002283 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002284 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002285 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002286 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002287 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002288 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002289 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2290 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002291 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002292
2293 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002294 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002295 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002296 if (!pr) {
2297 return false;
2298 }
2299
2300 GrPaint paint;
2301 paint.setCoverageSetOpXPFactory(op, invert);
2302
Brian Salomonf3569f02017-10-24 12:52:33 -04002303 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2304 std::move(paint),
2305 ss,
2306 fRenderTargetContext,
2307 &clip,
2308 &clipConservativeBounds,
2309 &viewMatrix,
2310 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002311 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002312 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002313 pr->drawPath(args);
2314 return true;
2315}
2316
Brian Osman11052242016-10-27 14:47:55 -04002317SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002318 ASSERT_SINGLE_OWNER_PRIV
2319
Robert Phillips6a6de562019-02-15 15:19:15 -05002320 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002321 return SkBudgeted::kNo;
2322 }
2323
Brian Osman11052242016-10-27 14:47:55 -04002324 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002325
Greg Daniel3912a4b2020-01-14 09:56:04 -05002326 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002327}
2328
Brian Salomon2fad74a2017-12-20 13:28:55 -05002329void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2330 GrPaint&& paint,
2331 GrAA aa,
2332 const SkMatrix& viewMatrix,
2333 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002334 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002335 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002336 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2337
Jim Van Verthf86073a2018-10-02 13:05:38 -04002338 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2339 return;
2340 }
2341
Chris Daltondb91c6e2017-09-08 16:25:08 -06002342 SkIRect clipConservativeBounds;
2343 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2344
Brian Salomon2fad74a2017-12-20 13:28:55 -05002345 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002346 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002347
robertphillips68737822015-10-29 12:12:21 -07002348 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002349 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002350 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002351 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002352 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002353 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002354 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002355 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002356
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002357 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002358 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002359 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002360 return;
2361 }
2362
Chris Dalton6ce447a2019-06-23 18:07:38 -06002363 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002364
2365 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2366 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2367 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2368
Brian Salomon2fad74a2017-12-20 13:28:55 -05002369 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002370 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002371 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2372 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002373 return;
2374 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002375 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002376 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002377 }
2378 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002379 if (canDrawArgs.fShape->style().applies()) {
2380 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2381 styleScale);
2382 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002383 return;
2384 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002385 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002386 // This time, allow SW renderer
2387 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2388 } else {
2389 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002390 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002391 }
robertphillipsea461502015-05-26 11:38:03 -07002392
bsalomon8acedde2016-06-24 10:42:16 -07002393 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002394#ifdef SK_DEBUG
2395 SkDebugf("Unable to find path renderer compatible with path.\n");
2396#endif
2397 return;
2398 }
2399
Robert Phillips256c37b2017-03-01 14:32:46 -05002400 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002401 std::move(paint),
2402 &GrUserStencilSettings::kUnused,
2403 this,
2404 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002405 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002406 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002407 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002408 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002409 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002410 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002411}
2412
Brian Salomon467921e2017-03-06 16:17:12 -05002413static void op_bounds(SkRect* bounds, const GrOp* op) {
2414 *bounds = op->bounds();
2415 if (op->hasZeroArea()) {
2416 if (op->hasAABloat()) {
2417 bounds->outset(0.5f, 0.5f);
2418 } else {
2419 // We don't know which way the particular GPU will snap lines or points at integer
2420 // coords. So we ensure that the bounds is large enough for either snap.
2421 SkRect before = *bounds;
2422 bounds->roundOut(bounds);
2423 if (bounds->fLeft == before.fLeft) {
2424 bounds->fLeft -= 1;
2425 }
2426 if (bounds->fTop == before.fTop) {
2427 bounds->fTop -= 1;
2428 }
2429 if (bounds->fRight == before.fRight) {
2430 bounds->fRight += 1;
2431 }
2432 if (bounds->fBottom == before.fBottom) {
2433 bounds->fBottom += 1;
2434 }
2435 }
2436 }
2437}
2438
Chris Dalton08755122019-08-05 16:13:47 -06002439void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002440 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002441 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2442}
2443
Brian Salomon348a0372018-10-31 10:42:18 -04002444void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2445 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002446 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002447 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002448 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002449 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002450 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002451 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002452 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002453 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002454
Brian Salomon467921e2017-03-06 16:17:12 -05002455 // Setup clip
2456 SkRect bounds;
2457 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002458 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002459 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002460 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002461 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002462
Chris Dalton9acf6822019-11-12 11:52:40 -07002463 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002464 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002465 }
2466
Chris Dalton9acf6822019-11-12 11:52:40 -07002467 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002468 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002469 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002470 }
2471
Chris Dalton9acf6822019-11-12 11:52:40 -07002472 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2473 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2474
2475 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2476 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2477 // that uses a stencil test when the stencil buffer is multisampled.)
2478 bool hasMixedSampledCoverage = (
2479 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002480 SkASSERT(!hasMixedSampledCoverage ||
2481 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002482
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002483 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002484 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002485 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2486
Greg Daniel524e28b2019-11-01 11:48:53 -04002487 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002488 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002489 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002490 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002491 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002492 }
2493 }
2494
2495 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002496 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002497 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002498 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002499 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002500 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002501 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002502}
2503
Greg Daniel524e28b2019-11-01 11:48:53 -04002504bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2505 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002506 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2507 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2508 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002509 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002510 return false;
2511 }
2512
Greg Daniel3912a4b2020-01-14 09:56:04 -05002513 if (this->caps()->textureBarrierSupport() &&
2514 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002515 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002516 // The render target is a texture, so we can read from it directly in the shader. The XP
2517 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002518 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002519 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002520 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002521 }
2522 }
2523
Greg Daniel3912a4b2020-01-14 09:56:04 -05002524 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002525
Eric Karl74480882017-04-03 14:49:05 -07002526 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002527 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002528 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002529 SkRect opBounds = op.bounds();
2530 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2531 // 0.5 pixels.
2532 if (op.hasAABloat() || op.hasZeroArea()) {
2533 opBounds.outset(0.5f, 0.5f);
2534 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2535 // performance we may ignore the clip when the draw is entirely inside the clip is float
2536 // space but will hit pixels just outside the clip when actually rasterizing.
2537 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002538 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002539 }
2540 SkIRect opIBounds;
2541 opBounds.roundOut(&opIBounds);
2542 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002543#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002544 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002545#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002546 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002547 }
2548
Greg Daniel3155f7f2020-01-16 16:54:26 -05002549 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002550 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2551 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002552 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002553 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002554
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002555 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002556 copyRect = clippedRect;
2557 }
Brian Salomon467921e2017-03-06 16:17:12 -05002558
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002559 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002560 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002561 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002562 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002563 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002564 } else {
Eric Karl74480882017-04-03 14:49:05 -07002565 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002566 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002567 }
Brian Salomonfc118442019-11-22 19:09:27 -05002568 sk_sp<GrTextureProxy> newProxy =
Greg Daniel3155f7f2020-01-16 16:54:26 -05002569 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), colorType, GrMipMapped::kNo,
2570 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002571 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002572
Greg Daniel46e366a2019-12-16 14:38:36 -05002573 dstProxyView->setProxyView({std::move(newProxy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002574 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002575 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002576}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002577
Brian Salomonfc118442019-11-22 19:09:27 -05002578bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2579 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002580 SkIRect clippedSrcRect;
2581 SkIPoint clippedDstPoint;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04002582 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), src->dimensions(), srcRect,
2583 dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002584 return false;
2585 }
2586
2587 GrPaint paint;
2588 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbfb72112020-01-13 10:51:50 -05002589 auto fp = GrTextureEffect::Make(sk_ref_sp(src), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002590 if (!fp) {
2591 return false;
2592 }
2593 paint.addColorFragmentProcessor(std::move(fp));
2594
2595 this->fillRectToRect(
2596 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2597 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2598 clippedSrcRect.height()),
2599 SkRect::Make(clippedSrcRect));
2600 return true;
2601}