blob: cc5391050ca9f268f3c4754135a9882337750b66 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
Brian Osmanf5ecf512020-04-01 09:29:13 -040011#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrBackendSemaphore.h"
Brian Salomond005b692020-04-01 15:47:05 -040013#include "include/private/GrImageContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/GrRecordingContext.h"
15#include "include/private/SkShadowFlags.h"
16#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040017#include "src/core/SkAutoPixmapStorage.h"
18#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkDrawShadowInfo.h"
20#include "src/core/SkGlyphRunPainter.h"
21#include "src/core/SkLatticeIter.h"
22#include "src/core/SkMatrixPriv.h"
23#include "src/core/SkRRectPriv.h"
24#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040025#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrBlurUtils.h"
29#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040030#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040031#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040033#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrDrawingManager.h"
35#include "src/gpu/GrFixedClip.h"
36#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomond005b692020-04-01 15:47:05 -040037#include "src/gpu/GrImageContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040038#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrMemoryPool.h"
40#include "src/gpu/GrPathRenderer.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040041#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040043#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/GrRenderTargetContextPriv.h"
45#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/gpu/GrStencilAttachment.h"
47#include "src/gpu/GrStyle.h"
48#include "src/gpu/GrTracing.h"
49#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040050#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/gpu/effects/GrRRectEffect.h"
Brian Salomon024bd002019-06-11 11:38:16 -040052#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040053#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000054#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000055#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrAtlasTextOp.h"
57#include "src/gpu/ops/GrClearOp.h"
58#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059#include "src/gpu/ops/GrDrawAtlasOp.h"
60#include "src/gpu/ops/GrDrawOp.h"
61#include "src/gpu/ops/GrDrawVerticesOp.h"
62#include "src/gpu/ops/GrDrawableOp.h"
63#include "src/gpu/ops/GrFillRRectOp.h"
64#include "src/gpu/ops/GrFillRectOp.h"
65#include "src/gpu/ops/GrLatticeOp.h"
66#include "src/gpu/ops/GrOp.h"
67#include "src/gpu/ops/GrOvalOpFactory.h"
68#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069#include "src/gpu/ops/GrShadowRRectOp.h"
70#include "src/gpu/ops/GrStencilPathOp.h"
71#include "src/gpu/ops/GrStrokeRectOp.h"
72#include "src/gpu/ops/GrTextureOp.h"
73#include "src/gpu/text/GrTextContext.h"
74#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040075
Herb Derbyc1b482c2018-08-09 15:02:27 -040076class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040077public:
78 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040079 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040081 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040082 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040083
Robert Phillips7c525e62018-06-12 10:11:12 -040084 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040085 fRenderTargetContext->addDrawOp(clip, std::move(op));
86 }
87
Brian Osman449b1152020-04-15 16:43:00 -040088 void drawShape(const GrClip& clip,
89 const SkPaint& paint,
90 const SkMatrixProvider& matrixProvider,
91 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040092 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040093 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 }
95
Brian Osman449b1152020-04-15 16:43:00 -040096 void makeGrPaint(GrMaskFormat maskFormat,
97 const SkPaint& skPaint,
98 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040099 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -0500100 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400101 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400103 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
104 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400105 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400106 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107 }
108 }
109
Robert Phillips69893702019-02-22 11:16:30 -0500110 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 return fRenderTargetContext->fContext;
112 }
113
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter* glyphPainter() override {
115 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400116 }
117
Brian Salomonf18b1d82017-10-27 11:30:49 -0400118private:
119 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400120 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400121
Brian Salomonf18b1d82017-10-27 11:30:49 -0400122};
joshualittbc907352016-01-13 06:45:40 -0800123
Robert Phillips72152832017-01-25 17:31:35 -0500124#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800125#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400126 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800127#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400128 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500129#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
130#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
131#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
132#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
133#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700134
Brian Salomone225b562017-06-14 13:00:03 -0400135//////////////////////////////////////////////////////////////////////////////
136
robertphillipsea461502015-05-26 11:38:03 -0700137class AutoCheckFlush {
138public:
halcanary9d524f22016-03-29 09:03:52 -0700139 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700140 SkASSERT(fDrawingManager);
141 }
bsalomonb77a9072016-09-07 10:02:04 -0700142 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700143
144private:
robertphillips77a2e522015-10-17 07:43:27 -0700145 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700146};
147
Greg Daniele20fcad2020-01-08 11:52:34 -0500148std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
149 GrRecordingContext* context,
150 GrColorType colorType,
151 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500152 sk_sp<GrSurfaceProxy> proxy,
153 GrSurfaceOrigin origin,
154 const SkSurfaceProps* surfaceProps,
155 bool managedOps) {
156 if (!proxy) {
157 return nullptr;
158 }
159
160 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400161 GrSwizzle readSwizzle, writeSwizzle;
162 if (colorType != GrColorType::kUnknown) {
163 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
164 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
165 }
Greg Danielba0ff782020-01-07 15:42:57 -0500166
Greg Daniel3912a4b2020-01-14 09:56:04 -0500167 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400168 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500169
170 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400171 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500172 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500173}
174
175std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
176 GrRecordingContext* context,
177 GrColorType colorType,
178 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500180 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500181 const GrBackendFormat& format,
182 int sampleCnt,
183 GrMipMapped mipMapped,
184 GrProtected isProtected,
185 GrSurfaceOrigin origin,
186 SkBudgeted budgeted,
187 const SkSurfaceProps* surfaceProps) {
188 // It is probably not necessary to check if the context is abandoned here since uses of the
189 // GrRenderTargetContext which need the context will mostly likely fail later on without an
190 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
191 // an abandoned context correctly. It also lets us early out of some extra work.
192 if (context->priv().abandoned()) {
193 return nullptr;
194 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500195
Greg Daniele20fcad2020-01-08 11:52:34 -0500196 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400197 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500198 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500199 if (!proxy) {
200 return nullptr;
201 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500202
Greg Danielba0ff782020-01-07 15:42:57 -0500203 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
204 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500205 if (!rtc) {
206 return nullptr;
207 }
208 rtc->discard();
209 return rtc;
210}
211
212std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
213 GrRecordingContext* context,
214 GrColorType colorType,
215 sk_sp<SkColorSpace> colorSpace,
216 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500217 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500218 int sampleCnt,
219 GrMipMapped mipMapped,
220 GrProtected isProtected,
221 GrSurfaceOrigin origin,
222 SkBudgeted budgeted,
223 const SkSurfaceProps* surfaceProps) {
224 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
225 if (!format.isValid()) {
226 return nullptr;
227 }
228
229 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
230 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
231 surfaceProps);
232}
233
234static inline GrColorType color_type_fallback(GrColorType ct) {
235 switch (ct) {
236 // kRGBA_8888 is our default fallback for many color types that may not have renderable
237 // backend formats.
238 case GrColorType::kAlpha_8:
239 case GrColorType::kBGR_565:
240 case GrColorType::kABGR_4444:
241 case GrColorType::kBGRA_8888:
242 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400243 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500244 case GrColorType::kRGBA_F16:
245 case GrColorType::kRGBA_F16_Clamped:
246 return GrColorType::kRGBA_8888;
247 case GrColorType::kAlpha_F16:
248 return GrColorType::kRGBA_F16;
249 case GrColorType::kGray_8:
250 return GrColorType::kRGB_888x;
251 default:
252 return GrColorType::kUnknown;
253 }
254}
255
Brian Salomond005b692020-04-01 15:47:05 -0400256std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400257 GrImageContext* context, GrColorType colorType, int sampleCnt) {
258 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400259 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400260 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
261 // We continue to the fallback color type if there no default renderable format or we
262 // requested msaa and the format doesn't support msaa.
263 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400264 return {colorType, format};
265 }
266 colorType = color_type_fallback(colorType);
267 } while (colorType != GrColorType::kUnknown);
268 return {GrColorType::kUnknown, {}};
269}
270
Greg Daniele20fcad2020-01-08 11:52:34 -0500271std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
272 GrRecordingContext* context,
273 GrColorType colorType,
274 sk_sp<SkColorSpace> colorSpace,
275 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500276 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500277 int sampleCnt,
278 GrMipMapped mipMapped,
279 GrProtected isProtected,
280 GrSurfaceOrigin origin,
281 SkBudgeted budgeted,
282 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400283 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400284 if (ct == GrColorType::kUnknown) {
285 return nullptr;
286 }
287 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
288 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500289}
290
Greg Danielba0ff782020-01-07 15:42:57 -0500291std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
292 GrRecordingContext* context,
293 GrColorType colorType,
294 sk_sp<SkColorSpace> colorSpace,
295 const GrBackendTexture& tex,
296 int sampleCnt,
297 GrSurfaceOrigin origin,
298 const SkSurfaceProps* surfaceProps,
299 ReleaseProc releaseProc,
300 ReleaseContext releaseCtx) {
301 SkASSERT(sampleCnt > 0);
302 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400303 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500304 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500305 if (!proxy) {
306 return nullptr;
307 }
308
309 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
310 origin, surfaceProps);
311}
312
313std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
314 GrRecordingContext* context,
315 GrColorType colorType,
316 sk_sp<SkColorSpace> colorSpace,
317 const GrBackendTexture& tex,
318 int sampleCnt,
319 GrSurfaceOrigin origin,
320 const SkSurfaceProps* surfaceProps) {
321 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400322 sk_sp<GrSurfaceProxy> proxy(
323 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500324 if (!proxy) {
325 return nullptr;
326 }
327
328 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
329 origin, surfaceProps);
330}
331
332std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
333 GrRecordingContext* context,
334 GrColorType colorType,
335 sk_sp<SkColorSpace> colorSpace,
336 const GrBackendRenderTarget& rt,
337 GrSurfaceOrigin origin,
338 const SkSurfaceProps* surfaceProps,
339 ReleaseProc releaseProc,
340 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400341 sk_sp<GrSurfaceProxy> proxy(
342 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500343 if (!proxy) {
344 return nullptr;
345 }
346
347 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
348 origin, surfaceProps);
349}
350
351std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
352 GrRecordingContext* context,
353 const SkImageInfo& imageInfo,
354 const GrVkDrawableInfo& vkInfo,
355 const SkSurfaceProps* props) {
356 sk_sp<GrSurfaceProxy> proxy(
357 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
358 vkInfo));
359 if (!proxy) {
360 return nullptr;
361 }
362
Greg Danielba0ff782020-01-07 15:42:57 -0500363 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
364 imageInfo.refColorSpace(), std::move(proxy),
365 kTopLeft_GrSurfaceOrigin, props);
366}
367
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
369// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
370// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
371// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500372GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500373 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400374 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400375 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400376 sk_sp<SkColorSpace> colorSpace,
377 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400378 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500379 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
380 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400381 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500382 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400383 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400384 , fManagedOpsTask(managedOpsTask) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400385 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400386 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400387 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400388 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
389 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500390
Brian Salomonf18b1d82017-10-27 11:30:49 -0400391 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700392 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700393}
394
robertphillips2e1e51f2015-10-15 08:01:48 -0700395#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500396void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400397 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400398 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700399 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700400}
401#endif
402
Brian Osman11052242016-10-27 14:47:55 -0400403GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800404 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400405 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400406 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400407 }
robertphillipsa106c622015-10-16 09:07:06 -0700408}
409
Chris Dalton7d6748e2019-03-13 00:34:52 -0600410inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600411 if (GrAA::kNo == aa) {
412 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
413 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600414 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600415 return GrAAType::kMSAA;
416 }
417 return GrAAType::kNone;
418 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600419 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600420}
421
Greg Daniele252f082017-10-23 16:05:23 -0400422GrMipMapped GrRenderTargetContext::mipMapped() const {
423 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
424 return proxy->mipMapped();
425 }
426 return GrMipMapped::kNo;
427}
428
Greg Danielf41b2bd2019-08-22 16:19:24 -0400429GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800430 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700431 SkDEBUGCODE(this->validate();)
432
Brian Salomon3b8486a2020-04-21 12:43:26 -0400433 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600434 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400435 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600436 if (fOpsTask && fNumStencilSamples > 0) {
437 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600438 fOpsTask->setMustPreserveStencil();
439 // Reload the stencil buffer content at the beginning of newOpsTask.
440 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
441 // values?
442 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
443 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400444 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600445 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700446 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400447 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400448 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700449}
450
Brian Osman449b1152020-04-15 16:43:00 -0400451void GrRenderTargetContext::drawGlyphRunList(const GrClip& clip,
452 const SkMatrixProvider& matrixProvider,
453 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800454 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700455 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700456 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400457 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700458
Greg Danielbe7fc462019-01-03 16:40:42 -0500459 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
460 // secondary command buffers because it would require stopping and starting a render pass which
461 // we don't have access to.
462 if (this->wrapsVkSecondaryCB()) {
463 return;
464 }
465
Herb Derby26cbe512018-05-24 14:39:01 -0400466 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400467 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400468 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700469}
470
Brian Osman11052242016-10-27 14:47:55 -0400471void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800472 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700473 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700474 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400475 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700476
Robert Phillips72152832017-01-25 17:31:35 -0500477 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400478
Greg Danielf41b2bd2019-08-22 16:19:24 -0400479 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700480}
481
Michael Ludwig4730f292020-05-26 14:37:15 +0000482void GrRenderTargetContext::clear(const SkIRect* rect,
483 const SkPMColor4f& color,
484 CanClearFullscreen canClearFullscreen) {
485 ASSERT_SINGLE_OWNER
486 RETURN_IF_ABANDONED
487 SkDEBUGCODE(this->validate();)
488 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
489
490 AutoCheckFlush acf(this->drawingManager());
491 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
492 canClearFullscreen);
493}
494
495void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
496 const SkPMColor4f& color,
497 CanClearFullscreen canClearFullscreen) {
498 ASSERT_SINGLE_OWNER_PRIV
499 RETURN_IF_ABANDONED_PRIV
500 SkDEBUGCODE(fRenderTargetContext->validate();)
501 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
502 fRenderTargetContext->fContext);
503
504 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
505 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
506}
507
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500508static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
509 paint->setColor4f(color);
510 if (color.isOpaque()) {
511 // Can just rely on the src-over blend mode to do the right thing
512 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
513 } else {
514 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
515 // were src blended
516 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
517 }
518}
519
Michael Ludwig4730f292020-05-26 14:37:15 +0000520void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500521 const SkPMColor4f& color,
Michael Ludwig4730f292020-05-26 14:37:15 +0000522 CanClearFullscreen canClearFullscreen) {
523 bool isFull = false;
524 if (!clip.hasWindowRectangles()) {
525 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
526 // only clear the entire target if we knew it had not been cleared before. As
527 // is this could end up doing a lot of redundant clears.
528 isFull = !clip.scissorEnabled() ||
529 (CanClearFullscreen::kYes == canClearFullscreen &&
530 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
531 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500532 }
533
534 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400535 GrOpsTask* opsTask = this->getOpsTask();
536 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500537 !this->caps()->performColorClearsAsDraws()) {
538 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400539 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500540 return;
541 } else {
542 // Will use an op for the clear, reset the load op to discard since the op will
543 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400544 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500545 }
546
547 // Must add an op to the list (either because we couldn't use a load op, or because the
548 // clear load op isn't supported)
549 if (this->caps()->performColorClearsAsDraws()) {
550 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
551 GrPaint paint;
552 clear_to_grpaint(color, &paint);
553 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400554 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
555 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500556 } else {
Michael Ludwig4730f292020-05-26 14:37:15 +0000557 this->addOp(GrClearOp::Make(
558 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500559 }
560 } else {
Michael Ludwig4730f292020-05-26 14:37:15 +0000561 if (this->caps()->performPartialClearsAsDraws() || clip.hasWindowRectangles()) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500562 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500563 GrPaint paint;
564 clear_to_grpaint(color, &paint);
565
Michael Ludwig4730f292020-05-26 14:37:15 +0000566 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400567 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
Michael Ludwig4730f292020-05-26 14:37:15 +0000568 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500569 } else {
Michael Ludwig4730f292020-05-26 14:37:15 +0000570 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip.scissorState(), color,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500571 this->asSurfaceProxy()));
572 // This version of the clear op factory can return null if the clip doesn't intersect
573 // with the surface proxy's boundary
574 if (!op) {
575 return;
576 }
Chris Dalton08755122019-08-05 16:13:47 -0600577 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500578 }
579 }
580}
581
Brian Osman11052242016-10-27 14:47:55 -0400582void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500583 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400584 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000585 // Start with the render target, since that is the maximum content we could possibly fill.
586 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500587 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400588 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000589 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
590 // inverse view matrix.
591 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
592 } else {
593 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
594 SkMatrix localMatrix;
595 if (!viewMatrix.invert(&localMatrix)) {
596 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400597 }
Michael Ludwig61328202019-06-19 14:48:58 +0000598 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
599 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700600 }
robertphillipsea461502015-05-26 11:38:03 -0700601}
602
Michael Ludwig61328202019-06-19 14:48:58 +0000603enum class GrRenderTargetContext::QuadOptimization {
604 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
605 kDiscarded,
606 // The rect to draw was converted to some other op and appended to the oplist, so no additional
607 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
608 // a constColor is provided.
609 kSubmitted,
610 // The clip was folded into the device quad, with updated edge flags and local coords, and
611 // caller is responsible for adding an appropriate op.
612 kClipApplied,
613 // No change to clip, but quad updated to better fit clip/render target, and caller is
614 // responsible for adding an appropriate op.
615 kCropped
616};
Michael Ludwig61a16512019-01-15 11:15:13 -0500617
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400618static bool make_vertex_finite(float* value) {
619 if (SkScalarIsNaN(*value)) {
620 return false;
621 }
622
623 if (!SkScalarIsFinite(*value)) {
624 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
625 // left when calculating crops.
626 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
627 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
628 }
629
630 return true;
631}
632
Michael Ludwig61328202019-06-19 14:48:58 +0000633GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400634 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500635 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000636 // Optimization requirements:
637 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500638 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
639 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
640 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
641 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
642 // 5. kCropped in all other scenarios (although a crop may be a no-op)
643
644 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
645 // better to just keep the old flags instead of introducing mixed edge flags.
646 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000647
Michael Ludwige08b4432019-06-19 18:00:48 -0400648 SkRect rtRect;
649 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400650 // Must use size at which the rendertarget will ultimately be allocated so that stencil
651 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500652 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400653 } else {
654 // Use the logical size of the render target, which allows for "fullscreen" clears even if
655 // the render target has an approximate backing fit
656 rtRect = SkRect::MakeWH(this->width(), this->height());
657 }
658
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500659 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000660 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400661 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
662 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500663 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400664 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500665 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
666 !make_vertex_finite(quad->fDevice.ys() + i) ||
667 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400668 // Discard if we see a nan
669 return QuadOptimization::kDiscarded;
670 }
671 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500672 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400673 }
674 } else {
675 // CropToRect requires the quads to be finite. If they are not finite and we have local
676 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500677 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400678 return QuadOptimization::kDiscarded;
679 }
Brian Salomon7694b902019-06-18 21:00:21 +0000680 }
681
Michael Ludwig61328202019-06-19 14:48:58 +0000682 // If the quad is entirely off screen, it doesn't matter what the clip does
683 if (!rtRect.intersects(drawBounds)) {
684 return QuadOptimization::kDiscarded;
685 }
Brian Salomon7694b902019-06-18 21:00:21 +0000686
Michael Ludwig61328202019-06-19 14:48:58 +0000687 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
688 // the render target).
689 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400690 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
691 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
692 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
693 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000694 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000695 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000696 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
697 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000698 }
Brian Salomon7694b902019-06-18 21:00:21 +0000699 }
700
Michael Ludwig61328202019-06-19 14:48:58 +0000701 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
702 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400703 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000704 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
705 // of the render target and the clip rect)
706 SkRect clipBounds = rtRect;
707 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
708 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000709 }
710
Michael Ludwig61328202019-06-19 14:48:58 +0000711 if (clipRRect.isRect()) {
712 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500713 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700714 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500715 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000716 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500717 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000718 if (drawBounds.contains(rtRect)) {
719 // Fullscreen clear
Michael Ludwig4730f292020-05-26 14:37:15 +0000720 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
Michael Ludwig61328202019-06-19 14:48:58 +0000721 return QuadOptimization::kSubmitted;
722 } else if (GrClip::IsPixelAligned(drawBounds) &&
723 drawBounds.width() > 256 && drawBounds.height() > 256) {
724 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
725 SkIRect scissorRect;
726 drawBounds.round(&scissorRect);
Michael Ludwig4730f292020-05-26 14:37:15 +0000727 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
Michael Ludwig61328202019-06-19 14:48:58 +0000728 return QuadOptimization::kSubmitted;
729 }
730 }
731
732 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000733 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500734 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000735 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
736 // properly reflect the smooth edge of the clip.
737 *aa = GrAA::kYes;
738 }
739 // We intentionally do not downgrade AA here because we don't know if we need to
740 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
741 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
742
743 // deviceQuad is exactly the intersection of original quad and clip, so it can be
744 // drawn with no clip (submitted by caller)
745 return QuadOptimization::kClipApplied;
746 } else {
747 // The quads have been updated to better fit the clip bounds, but can't get rid of
748 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500749 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000750 return QuadOptimization::kCropped;
751 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700752 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000753 // Rounded corners and constant filled color (limit ourselves to solid colors because
754 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500755 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
756 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
757 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000758 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
759 // we can draw the rrect directly and ignore the edge flags
760 GrPaint paint;
761 clear_to_grpaint(*constColor, &paint);
762 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
763 clipRRect, GrStyle::SimpleFill());
764 return QuadOptimization::kSubmitted;
765 } else {
766 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500767 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000768 return QuadOptimization::kCropped;
769 }
Brian Salomon7694b902019-06-18 21:00:21 +0000770 }
Brian Salomon7694b902019-06-18 21:00:21 +0000771 }
772
Michael Ludwig61328202019-06-19 14:48:58 +0000773 // Crop the quad to the conservative bounds of the clip.
Michael Ludwigc002d562020-05-13 14:17:57 -0400774 SkRect clipBounds = SkRect::Make(clip.getConservativeBounds(rtRect.width(), rtRect.height()));
Michael Ludwig61328202019-06-19 14:48:58 +0000775
776 // One final check for discarding, since we may have gone here directly due to a complex clip
777 if (!clipBounds.intersects(drawBounds)) {
778 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000779 }
780
Michael Ludwig61328202019-06-19 14:48:58 +0000781 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
782 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500783 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
784 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000785
786 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000787}
788
Michael Ludwig61328202019-06-19 14:48:58 +0000789void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500790 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500791 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500792 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400793 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000794 ASSERT_SINGLE_OWNER
795 RETURN_IF_ABANDONED
796 SkDEBUGCODE(this->validate();)
797 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500798
Michael Ludwig61328202019-06-19 14:48:58 +0000799 AutoCheckFlush acf(this->drawingManager());
800
801 SkPMColor4f* constColor = nullptr;
802 SkPMColor4f paintColor;
803 if (!ss && !paint.numCoverageFragmentProcessors() &&
804 paint.isConstantBlendedColor(&paintColor)) {
805 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
806 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500807 }
808
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500809 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000810 if (opt >= QuadOptimization::kClipApplied) {
811 // These optimizations require caller to add an op themselves
812 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
813 : clip;
814 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
815 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500816 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
817 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700818 }
Michael Ludwig61328202019-06-19 14:48:58 +0000819 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800820}
821
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000822void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400823 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500824 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000825 sk_sp<GrColorSpaceXform> textureXform,
826 GrSamplerState::Filter filter,
827 const SkPMColor4f& color,
828 SkBlendMode blendMode,
829 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500830 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400831 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000832 ASSERT_SINGLE_OWNER
833 RETURN_IF_ABANDONED
834 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400835 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000836 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
837
838 AutoCheckFlush acf(this->drawingManager());
839
840 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
841 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500842 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000843
844 SkASSERT(opt != QuadOptimization::kSubmitted);
845 if (opt != QuadOptimization::kDiscarded) {
846 // And the texture op if not discarded
847 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
848 : clip;
849 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400850 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400851 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
852 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400853 // Use the provided subset, although hypothetically we could detect that the cropped local
854 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500855 this->addDrawOp(finalClip,
856 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
857 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400858 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000859 }
860}
861
Brian Osman11052242016-10-27 14:47:55 -0400862void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500863 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500864 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400865 const SkMatrix& viewMatrix,
866 const SkRect& rect,
867 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700868 if (!style) {
869 style = &GrStyle::SimpleFill();
870 }
joshualitt1de610a2016-01-06 08:26:09 -0800871 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700872 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700873 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400874 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700875
bsalomon6663acf2016-05-10 09:14:17 -0700876 // Path effects should've been devolved to a path in SkGpuDevice
877 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700878
Robert Phillips72152832017-01-25 17:31:35 -0500879 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700880
bsalomon6663acf2016-05-10 09:14:17 -0700881 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400882 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000883 // Fills the rect, using rect as its own local coordinates
884 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500885 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400886 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
887 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
888 (rect.width() && rect.height())) {
889 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
890 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400891 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700892
Chris Dalton7d6748e2019-03-13 00:34:52 -0600893 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500894 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
895 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
896 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500897 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400898 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700899 return;
robertphillips4bc31812016-03-01 12:22:49 -0800900 }
robertphillips4bc31812016-03-01 12:22:49 -0800901 }
Mike Klein16885072018-12-11 09:54:31 -0500902 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000903 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
904 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700905}
906
Michael Ludwig69858532018-11-28 15:34:34 -0500907void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
908 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
909 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600910 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500911
912 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
913 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500914}
915
Robert Phillipsec2249f2016-11-09 08:54:35 -0500916int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500917 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400918 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500919}
920
Greg Danielf41b2bd2019-08-22 16:19:24 -0400921GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600922 ) const {
923#if GR_TEST_UTILS
924 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400925 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600926 }
927#endif
928 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
929 // would normally be overwritten. The one exception is if the render target context is marked as
930 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
931 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
932 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
933 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400934 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600935}
936
Chris Dalton858cf232019-10-14 16:20:00 -0600937void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600938 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600939 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600940 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600941
Chris Daltoneffee202019-07-01 22:28:03 -0600942 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600943 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500944 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600945 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400946 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600947 }
948 SkASSERT(numRequiredSamples > 0);
949
950 if (numRequiredSamples > fNumStencilSamples) {
951 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500952 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600953 }
Chris Dalton6b982802019-06-27 13:53:46 -0600954
Chris Dalton674f77a2019-09-30 20:49:39 -0600955 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600956 if (this->caps()->performStencilClearsAsDraws()) {
957 // There is a driver bug with clearing stencil. We must use an op to manually clear the
958 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig4730f292020-05-26 14:37:15 +0000959 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600960 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600961 this->getOpsTask()->setInitialStencilContent(
962 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600963 }
964 }
965}
966
Michael Ludwig4730f292020-05-26 14:37:15 +0000967void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
968 ASSERT_SINGLE_OWNER_PRIV
969 RETURN_IF_ABANDONED_PRIV
970 SkDEBUGCODE(fRenderTargetContext->validate();)
971 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
972 fRenderTargetContext->fContext);
973
974 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
975
976 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
977}
978
979void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600980 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
981
Michael Ludwig1e632792020-05-21 12:45:31 -0400982 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwig4730f292020-05-26 14:37:15 +0000983 (clip.scissorEnabled() && this->caps()->performPartialClearsAsDraws());
984 // TODO(michaelludwig): internalStencilClear will eventually just take a GrScissorState so
985 // we won't need to check window rectangles here.
986 if (clearWithDraw || clip.hasWindowRectangles()) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500987 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwig4730f292020-05-26 14:37:15 +0000988 SkRect rect = clip.scissorEnabled() ? SkRect::Make(clip.scissorRect())
989 : SkRect::MakeWH(this->width(), this->height());
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500990
991 // Configure the paint to have no impact on the color buffer
992 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400993 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwig4730f292020-05-26 14:37:15 +0000994 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
995 rect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500996 } else {
Michael Ludwig1e632792020-05-21 12:45:31 -0400997 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(
Michael Ludwig4730f292020-05-26 14:37:15 +0000998 fContext, clip.scissorState(), insideStencilMask, this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500999 if (!op) {
1000 return;
1001 }
Chris Dalton08755122019-08-05 16:13:47 -06001002 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001003 }
robertphillips976f5f02016-06-03 10:59:20 -07001004}
1005
Chris Daltonbbfd5162017-11-07 13:35:22 -07001006void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001007 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001008 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001009 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001010 ASSERT_SINGLE_OWNER_PRIV
1011 RETURN_IF_ABANDONED_PRIV
1012 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001013 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1014 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001015
Brian Salomon467921e2017-03-06 16:17:12 -05001016 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1017 SkASSERT(path);
1018 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1019
1020 // FIXME: Use path bounds instead of this WAR once
1021 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1022 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1023
1024 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001025 GrAppliedHardClip appliedClip;
1026 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1027 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001028 return;
1029 }
1030
Robert Phillips7c525e62018-06-12 10:11:12 -04001031 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1032 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001033 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001034 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001035 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001036 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001037 if (!op) {
1038 return;
1039 }
Brian Salomon97180af2017-03-14 13:42:58 -04001040 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001041
Chris Daltoneffee202019-07-01 22:28:03 -06001042 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001043 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001044}
1045
Michael Ludwig379e4962019-12-06 13:21:26 -05001046void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1047 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001048 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001049 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1050 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001051 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001052 ASSERT_SINGLE_OWNER
1053 RETURN_IF_ABANDONED
1054 SkDEBUGCODE(this->validate();)
1055 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001056
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001057 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1058 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1059 AutoCheckFlush acf(this->drawingManager());
1060 GrAAType aaType = this->chooseAAType(aa);
1061 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1062 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1063 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001064 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1065 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001066}
1067
Brian Osman11052242016-10-27 14:47:55 -04001068void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001069 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001070 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001071 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001072 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001073 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001074 ASSERT_SINGLE_OWNER
1075 RETURN_IF_ABANDONED
1076 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001077 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001078
1079 AutoCheckFlush acf(this->drawingManager());
1080
1081 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001082 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001083 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001084 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001085 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001086 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001087 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001088}
1089
1090///////////////////////////////////////////////////////////////////////////////
1091
Brian Osman4d92b892019-03-24 00:53:23 +00001092void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1093 GrPaint&& paint,
1094 const SkMatrix& viewMatrix,
1095 int spriteCount,
1096 const SkRSXform xform[],
1097 const SkRect texRect[],
1098 const SkColor colors[]) {
1099 ASSERT_SINGLE_OWNER
1100 RETURN_IF_ABANDONED
1101 SkDEBUGCODE(this->validate();)
1102 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1103
1104 AutoCheckFlush acf(this->drawingManager());
1105
1106 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1107 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1108 aaType, spriteCount, xform, texRect, colors);
1109 this->addDrawOp(clip, std::move(op));
1110}
1111
1112///////////////////////////////////////////////////////////////////////////////
1113
Brian Osman11052242016-10-27 14:47:55 -04001114void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001115 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001116 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001117 const SkMatrix& viewMatrix,
1118 const SkRRect& rrect,
1119 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001120 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001121 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001122 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001123 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001124
1125 const SkStrokeRec& stroke = style.strokeRec();
1126 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001127 return;
1128 }
1129
bsalomon7f0d9f32016-08-15 14:49:10 -07001130 GrNoClip noclip;
1131 const GrClip* clip = &origClip;
1132#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1133 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001134 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001135 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1136 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001137 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1138 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001139 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001140 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1141 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001142 clip = &noclip;
1143 }
1144#endif
bsalomon6663acf2016-05-10 09:14:17 -07001145 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001146
Robert Phillips72152832017-01-25 17:31:35 -05001147 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001148
Chris Dalton7d6748e2019-03-13 00:34:52 -06001149 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001150
Chris Dalton0dffbab2019-03-27 13:08:50 -06001151 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001152 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1153 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1154 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1155 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1156 // to avoid perf regressions on some platforms.
1157 assert_alive(paint);
1158 op = GrOvalOpFactory::MakeCircularRRectOp(
1159 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1160 }
1161 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001162 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001163 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001164 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001165 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001166 assert_alive(paint);
1167 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001168 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001169 }
1170 if (op) {
1171 this->addDrawOp(*clip, std::move(op));
1172 return;
robertphillipsea461502015-05-26 11:38:03 -07001173 }
robertphillipsb56f9272016-02-25 11:03:52 -08001174
Mike Klein16885072018-12-11 09:54:31 -05001175 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001176 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001177 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001178}
1179
Jim Van Verthc5903412016-11-17 15:27:09 -05001180///////////////////////////////////////////////////////////////////////////////
1181
Jim Van Verth3af1af92017-05-18 15:06:54 -04001182static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1183 SkPoint3 result;
1184 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1185 result.fZ = pt.fZ;
1186 return result;
1187}
1188
1189bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001190 const SkMatrix& viewMatrix,
1191 const SkPath& path,
1192 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001193 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001194 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001195 return true;
1196 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001197 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001198 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001199
1200 // check z plane
1201 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1202 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1203 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1204 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1205 return false;
1206 }
1207
1208 SkRRect rrect;
1209 SkRect rect;
1210 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001211 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001212 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1213 if (!isRRect &&
1214 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1215 rect.width() > SK_ScalarNearlyZero) {
1216 rrect.setOval(rect);
1217 isRRect = true;
1218 }
1219 if (!isRRect && path.isRect(&rect)) {
1220 rrect.setRect(rect);
1221 isRRect = true;
1222 }
1223
1224 if (!isRRect) {
1225 return false;
1226 }
1227
Jim Van Verthc5903412016-11-17 15:27:09 -05001228 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001229 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001230 }
1231
Robert Phillips72152832017-01-25 17:31:35 -05001232 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001233
Jim Van Verth3af1af92017-05-18 15:06:54 -04001234 // transform light
1235 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1236
1237 // 1/scale
1238 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001239 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001240 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1241 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1242
1243 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001244 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1245
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001246 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001247 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1248 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1249 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001250
1251 // Outset the shadow rrect to the border of the penumbra
1252 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1253 SkRRect ambientRRect;
1254 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1255 // If the rrect was an oval then its outset will also be one.
1256 // We set it explicitly to avoid errors.
1257 if (rrect.isOval()) {
1258 ambientRRect = SkRRect::MakeOval(outsetRect);
1259 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001260 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001261 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1262 }
1263
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001264 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001265 if (transparent) {
1266 // set a large inset to force a fill
1267 devSpaceInsetWidth = ambientRRect.width();
1268 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001269
Robert Phillips7c525e62018-06-12 10:11:12 -04001270 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1271 ambientColor,
1272 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001273 ambientRRect,
1274 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001275 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001276 if (op) {
1277 this->addDrawOp(clip, std::move(op));
1278 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001279 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001280
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001281 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001282 SkScalar devSpaceSpotBlur;
1283 SkScalar spotScale;
1284 SkVector spotOffset;
1285 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1286 devLightPos.fZ, rec.fLightRadius,
1287 &devSpaceSpotBlur, &spotScale, &spotOffset);
1288 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001289 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1290
Jim Van Verth3af1af92017-05-18 15:06:54 -04001291 // Adjust translate for the effect of the scale.
1292 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1293 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1294 // This offset is in dev space, need to transform it into source space.
1295 SkMatrix ctmInverse;
1296 if (viewMatrix.invert(&ctmInverse)) {
1297 ctmInverse.mapPoints(&spotOffset, 1);
1298 } else {
1299 // Since the matrix is a similarity, this should never happen, but just in case...
1300 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1301 SkASSERT(false);
1302 }
1303
1304 // Compute the transformed shadow rrect
1305 SkRRect spotShadowRRect;
1306 SkMatrix shadowTransform;
1307 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1308 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001309 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001310
1311 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001312 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001313 SkScalar insetWidth = blurOutset;
1314 if (transparent) {
1315 // If transparent, just do a fill
1316 insetWidth += spotShadowRRect.width();
1317 } else {
1318 // For shadows, instead of using a stroke we specify an inset from the penumbra
1319 // border. We want to extend this inset area so that it meets up with the caster
1320 // geometry. The inset geometry will by default already be inset by the blur width.
1321 //
1322 // We compare the min and max corners inset by the radius between the original
1323 // rrect and the shadow rrect. The distance between the two plus the difference
1324 // between the scaled radius and the original radius gives the distance from the
1325 // transformed shadow shape to the original shape in that corner. The max
1326 // of these gives the maximum distance we need to cover.
1327 //
1328 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1329 // that to get the full insetWidth.
1330 SkScalar maxOffset;
1331 if (rrect.isRect()) {
1332 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001333 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001334 rrect.rect().fLeft),
1335 SkTAbs(spotShadowRRect.rect().fTop -
1336 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001337 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001338 rrect.rect().fRight),
1339 SkTAbs(spotShadowRRect.rect().fBottom -
1340 rrect.rect().fBottom)));
1341 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001342 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001343 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1344 rrect.rect().fLeft + dr,
1345 spotShadowRRect.rect().fTop -
1346 rrect.rect().fTop + dr);
1347 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1348 rrect.rect().fRight - dr,
1349 spotShadowRRect.rect().fBottom -
1350 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001351 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001352 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001353 }
Brian Osman788b9162020-02-07 10:36:46 -05001354 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001355 }
1356
1357 // Outset the shadow rrect to the border of the penumbra
1358 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1359 if (spotShadowRRect.isOval()) {
1360 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1361 } else {
1362 SkScalar outsetRad = spotRadius + blurOutset;
1363 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1364 }
1365
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001366 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001367
Robert Phillips7c525e62018-06-12 10:11:12 -04001368 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1369 spotColor,
1370 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001371 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001372 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001373 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001374 if (op) {
1375 this->addDrawOp(clip, std::move(op));
1376 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001377 }
1378
1379 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001380}
1381
1382///////////////////////////////////////////////////////////////////////////////
1383
Brian Osman11052242016-10-27 14:47:55 -04001384bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001385 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001386 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001387 const SkMatrix& viewMatrix,
1388 const SkRRect& origOuter,
1389 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001390 SkASSERT(!origInner.isEmpty());
1391 SkASSERT(!origOuter.isEmpty());
1392
Brian Salomon65749212017-12-01 16:01:47 -05001393 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1394
Chris Dalton7d6748e2019-03-13 00:34:52 -06001395 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001396
1397 if (GrAAType::kMSAA == aaType) {
1398 return false;
1399 }
1400
Greg Daniel2655ede2019-04-10 00:49:28 +00001401 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1402 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001403 auto outerR = outer->width() / 2.f;
1404 auto innerR = inner->width() / 2.f;
1405 auto cx = outer->getBounds().fLeft + outerR;
1406 auto cy = outer->getBounds().fTop + outerR;
1407 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1408 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1409 auto avgR = (innerR + outerR) / 2.f;
1410 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1411 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1412 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001413 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001414 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001415 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001416 if (op) {
1417 this->addDrawOp(clip, std::move(op));
1418 return true;
1419 }
Mike Klein16885072018-12-11 09:54:31 -05001420 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001421 }
1422 }
1423
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001424 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001426 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1427 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001428 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001429 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1430 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001431 }
robertphillips00095892016-02-29 13:50:40 -08001432
robertphillips00095892016-02-29 13:50:40 -08001433 SkMatrix inverseVM;
1434 if (!viewMatrix.isIdentity()) {
1435 if (!origInner.transform(viewMatrix, inner.writable())) {
1436 return false;
1437 }
1438 if (!origOuter.transform(viewMatrix, outer.writable())) {
1439 return false;
1440 }
1441 if (!viewMatrix.invert(&inverseVM)) {
1442 return false;
1443 }
1444 } else {
1445 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001446 }
robertphillips00095892016-02-29 13:50:40 -08001447
Ethan Nicholaseace9352018-10-15 20:09:54 +00001448 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001449 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001450 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001451 if (!innerEffect) {
1452 return false;
1453 }
1454
Ethan Nicholaseace9352018-10-15 20:09:54 +00001455 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001456 if (!outerEffect) {
1457 return false;
1458 }
1459
Brian Salomon82f44312017-01-11 13:42:54 -05001460 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1461 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001462
1463 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001464 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001465 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1466 }
halcanary9d524f22016-03-29 09:03:52 -07001467
Brian Salomon82f44312017-01-11 13:42:54 -05001468 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1469 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001470 return true;
1471}
1472
Brian Osman11052242016-10-27 14:47:55 -04001473void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001476 const SkMatrix& viewMatrix,
1477 const SkRRect& outer,
1478 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001479 ASSERT_SINGLE_OWNER
1480 RETURN_IF_ABANDONED
1481 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001482 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001483
1484 SkASSERT(!outer.isEmpty());
1485 SkASSERT(!inner.isEmpty());
1486
Robert Phillips72152832017-01-25 17:31:35 -05001487 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001488
Brian Salomon82f44312017-01-11 13:42:54 -05001489 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001490 return;
1491 }
Mike Klein16885072018-12-11 09:54:31 -05001492 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001493
1494 SkPath path;
1495 path.setIsVolatile(true);
1496 path.addRRect(inner);
1497 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001498 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001499 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001500}
1501
robertphillipsea461502015-05-26 11:38:03 -07001502///////////////////////////////////////////////////////////////////////////////
1503
Brian Osman11052242016-10-27 14:47:55 -04001504void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001505 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001506 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001507 const SkMatrix& viewMatrix,
1508 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001509 const GrStyle& style,
1510 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001511 ASSERT_SINGLE_OWNER
1512 RETURN_IF_ABANDONED
1513 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001514 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001515
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001516 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001517 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001518 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001519 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001520 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1521 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001522 aa = GrAA::kNo;
1523 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001524 }
msarettcc319b92016-08-25 18:07:18 -07001525 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001526 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001527 SkPath path;
1528 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001529 path.setIsVolatile(true);
1530
Brian Salomon82f44312017-01-11 13:42:54 -05001531 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001532 }
1533
Chris Dalton7d6748e2019-03-13 00:34:52 -06001534 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001535 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1536 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001537 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001538}
1539
Brian Osman11052242016-10-27 14:47:55 -04001540void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001541 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001542 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001543 const SkMatrix& viewMatrix,
1544 const SkRect& oval,
1545 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001546 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001547 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001548 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001549 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001550
Robert Phillips7484d202018-07-03 09:09:08 -04001551 const SkStrokeRec& stroke = style.strokeRec();
1552
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001553 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001554 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1555 return;
1556 }
1557
1558 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001559 return;
robertphillipsea461502015-05-26 11:38:03 -07001560 }
1561
Robert Phillips72152832017-01-25 17:31:35 -05001562 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001563
Chris Dalton7d6748e2019-03-13 00:34:52 -06001564 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001565
1566 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001567 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1568 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001569 // We don't draw true circles as round rects in coverage mode, because it can
1570 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1571 assert_alive(paint);
1572 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1573 this->caps()->shaderCaps());
1574 }
1575 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001576 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1577 // the arc equation. This same special geometry and fragment branch also turn out to be a
1578 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1579 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1580 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001581 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001582 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1583 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001584 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001585 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001586 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001587 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1588 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001589 }
1590 if (op) {
1591 this->addDrawOp(clip, std::move(op));
1592 return;
robertphillipsea461502015-05-26 11:38:03 -07001593 }
robertphillipsb56f9272016-02-25 11:03:52 -08001594
Mike Klein16885072018-12-11 09:54:31 -05001595 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001596 this->drawShapeUsingPathRenderer(
1597 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001598 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001599}
1600
Brian Osman11052242016-10-27 14:47:55 -04001601void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001602 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001603 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001604 const SkMatrix& viewMatrix,
1605 const SkRect& oval,
1606 SkScalar startAngle,
1607 SkScalar sweepAngle,
1608 bool useCenter,
1609 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001610 ASSERT_SINGLE_OWNER
1611 RETURN_IF_ABANDONED
1612 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001613 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001614
1615 AutoCheckFlush acf(this->drawingManager());
1616
Chris Dalton7d6748e2019-03-13 00:34:52 -06001617 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001618 if (GrAAType::kCoverage == aaType) {
1619 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1620 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1621 std::move(paint),
1622 viewMatrix,
1623 oval,
1624 startAngle,
1625 sweepAngle,
1626 useCenter,
1627 style,
1628 shaderCaps);
1629 if (op) {
1630 this->addDrawOp(clip, std::move(op));
1631 return;
1632 }
1633 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001634 }
Brian Salomone4949402018-04-26 15:22:04 -04001635 this->drawShapeUsingPathRenderer(
1636 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001637 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001638}
1639
Brian Osman11052242016-10-27 14:47:55 -04001640void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001641 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001642 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001643 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001644 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001645 sk_sp<GrColorSpaceXform> csxf,
1646 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001647 std::unique_ptr<SkLatticeIter> iter,
1648 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001649 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001650 RETURN_IF_ABANDONED
1651 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001652 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001653
Robert Phillips72152832017-01-25 17:31:35 -05001654 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001655
Brian Salomon2a943df2018-05-04 13:43:19 -04001656 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001657 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001658 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001659 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001660}
1661
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001662void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1663 const SkRect& bounds) {
1664 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1665 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001666 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001667}
1668
Brian Salomon031b0ba2019-05-23 11:05:26 -04001669void GrRenderTargetContext::asyncRescaleAndReadPixels(
1670 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1671 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1672 auto direct = fContext->priv().asDirectContext();
1673 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001674 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001675 return;
1676 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001677 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001678 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001679 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001680 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001681 if (this->asRenderTargetProxy()->framebufferOnly()) {
1682 callback(context, nullptr);
1683 return;
1684 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001685 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001686 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001687 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001688 return;
1689 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001690 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001691 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001692 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001693 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001694 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001695 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1696 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001697 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001698 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001699 backendFormatOfFinalContext, dstCT);
1700 // Fail if we can't read from the source surface's color type.
1701 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001702 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001703 return;
1704 }
Brian Salomon624f9062019-07-02 14:23:00 -04001705 // Fail if read color type does not have all of dstCT's color channels and those missing color
1706 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001707 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1708 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1709 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1710 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001711 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001712 return;
1713 }
1714
Brian Salomonbf6b9792019-08-21 09:38:10 -04001715 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 int x = srcRect.fLeft;
1717 int y = srcRect.fTop;
1718 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001719 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1720 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001721 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001722 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001723 return;
1724 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001725 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001726 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001727 x = y = 0;
1728 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001729 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1730 this->colorInfo().alphaType(),
1731 info.colorSpace(),
1732 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001733 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1734 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001735 // We flip or color convert by drawing and we don't currently support drawing to
1736 // kPremul.
1737 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001738 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001739 return;
1740 }
Greg Daniel40903af2020-01-30 14:55:05 -05001741 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001742 SkRect srcRectToDraw = SkRect::Make(srcRect);
1743 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001744 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001745 texProxyView =
1746 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1747 SkBackingFit::kApprox, SkBudgeted::kNo);
1748 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001749 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001750 return;
1751 }
Brian Salomonc5243782020-04-02 12:50:34 -04001752 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001753 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1754 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001755 tempRTC = GrRenderTargetContext::Make(
1756 direct, this->colorInfo().colorType(), info.refColorSpace(),
1757 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001758 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001759 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001760 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001761 return;
1762 }
Greg Daniel40903af2020-01-30 14:55:05 -05001763 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1764 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1765 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001766 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1767 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1768 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001769 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001770 }
1771 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001772 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001773 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1774 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001775}
1776
Brian Salomon9241a6d2019-10-03 13:26:54 -04001777class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1778public:
1779 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1780 ~AsyncReadResult() override {
1781 for (int i = 0; i < fPlanes.count(); ++i) {
1782 if (!fPlanes[i].fMappedBuffer) {
1783 delete[] static_cast<const char*>(fPlanes[i].fData);
1784 } else {
1785 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1786 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1787 }
1788 }
1789 }
1790
1791 int count() const override { return fPlanes.count(); }
1792 const void* data(int i) const override { return fPlanes[i].fData; }
1793 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1794
1795 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001796 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001797 size_t rowBytes,
1798 GrClientMappedBufferManager* manager) {
1799 SkASSERT(!result.fTransferBuffer->isMapped());
1800 const void* mappedData = result.fTransferBuffer->map();
1801 if (!mappedData) {
1802 return false;
1803 }
1804 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001805 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001806 result.fPixelConverter(convertedData.get(), mappedData);
1807 this->addCpuPlane(std::move(convertedData), rowBytes);
1808 result.fTransferBuffer->unmap();
1809 } else {
1810 manager->insert(result.fTransferBuffer);
1811 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1812 }
1813 return true;
1814 }
1815
1816 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1817 SkASSERT(data);
1818 SkASSERT(rowBytes > 0);
1819 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1820 }
1821
1822private:
1823 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1824 SkASSERT(data);
1825 SkASSERT(rowBytes > 0);
1826 SkASSERT(mappedBuffer);
1827 SkASSERT(mappedBuffer->isMapped());
1828 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1829 }
1830
1831 struct Plane {
1832 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1833 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1834 const void* fData;
1835 size_t fRowBytes;
1836 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1837 sk_sp<GrGpuBuffer> fMappedBuffer;
1838 };
1839 SkSTArray<3, Plane> fPlanes;
1840 uint32_t fInboxID;
1841};
1842
Brian Salomon024bd002019-06-11 11:38:16 -04001843void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1844 ReadPixelsCallback callback,
1845 ReadPixelsContext context) {
1846 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1847 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1848
Greg Danielca9dbe22020-01-02 13:44:30 -05001849 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001850 callback(context, nullptr);
1851 return;
1852 }
1853
Brian Salomon9241a6d2019-10-03 13:26:54 -04001854 auto directContext = fContext->priv().asDirectContext();
1855 SkASSERT(directContext);
1856 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1857
Brian Salomon024bd002019-06-11 11:38:16 -04001858 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1859
1860 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001861 auto ii = SkImageInfo::Make(rect.size(), colorType,
1862 this->colorInfo().alphaType(),
1863 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001864 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001865 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1866 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1867 result->addCpuPlane(std::move(data), pm.rowBytes());
1868
Brian Salomon1d435302019-07-01 13:05:28 -04001869 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001870 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001871 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001872 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001873 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001874 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001875 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001876
Brian Salomonab32f652019-05-10 14:24:50 -04001877 struct FinishContext {
1878 ReadPixelsCallback* fClientCallback;
1879 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001880 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001881 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001882 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001883 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001884 };
1885 // Assumption is that the caller would like to flush. We could take a parameter or require an
1886 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1887 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001888 auto* finishContext = new FinishContext{callback,
1889 context,
1890 rect.size(),
1891 colorType,
1892 mappedBufferManager,
1893 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001894 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001895 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001896 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001897 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1898 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1899 context->fMappedBufferManager)) {
1900 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001901 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001902 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001903 delete context;
1904 };
1905 GrFlushInfo flushInfo;
1906 flushInfo.fFinishedContext = finishContext;
1907 flushInfo.fFinishedProc = finishCallback;
1908 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Greg Daniel04283f32020-05-20 13:16:00 -04001909 directContext->submit();
Brian Salomon024bd002019-06-11 11:38:16 -04001910}
1911
Brian Salomon9241a6d2019-10-03 13:26:54 -04001912void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1913 sk_sp<SkColorSpace> dstColorSpace,
1914 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001915 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001916 RescaleGamma rescaleGamma,
1917 SkFilterQuality rescaleQuality,
1918 ReadPixelsCallback callback,
1919 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001920 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1921 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001922 SkASSERT(!dstSize.isZero());
1923 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1924
Brian Salomon024bd002019-06-11 11:38:16 -04001925 auto direct = fContext->priv().asDirectContext();
1926 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001927 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001928 return;
1929 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001930 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001931 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001932 return;
1933 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001934 if (this->asRenderTargetProxy()->framebufferOnly()) {
1935 callback(context, nullptr);
1936 return;
1937 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001938 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001939 callback(context, nullptr);
1940 return;
1941 }
Brian Salomon024bd002019-06-11 11:38:16 -04001942 int x = srcRect.fLeft;
1943 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001944 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001945 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001946 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001947 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001948 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001949 dstColorSpace);
1950 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001951 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1952 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001953 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;
Greg Daniel40903af2020-01-30 14:55:05 -05001960 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001961 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001962 srcView = this->readSurfaceView();
1963 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001964 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1965 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1966 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001967 // If we can't get a texture copy of the contents then give up.
1968 callback(context, nullptr);
1969 return;
1970 }
Brian Salomonc5243782020-04-02 12:50:34 -04001971 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001972 x = y = 0;
1973 }
Brian Salomon4d036892019-07-02 15:10:58 -04001974 // We assume the caller wants kPremul. There is no way to indicate a preference.
1975 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001976 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1977 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001978 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001979 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1980 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001981 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1982 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001983 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001984 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001985 return;
1986 }
Greg Daniel40903af2020-01-30 14:55:05 -05001987 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1988 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1989 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1990 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1991 SkMatrix::I(), std::move(xform));
1992 srcView = tempRTC->readSurfaceView();
1993 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001994 x = y = 0;
1995 }
1996 }
Greg Danielc594e622019-10-15 14:01:49 -04001997
Greg Daniele20fcad2020-01-08 11:52:34 -05001998 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1999 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2000 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002001 int halfW = dstSize.width()/2;
2002 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002003 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2004 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2005 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2006 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2007 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2008 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002009 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002010 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002011 return;
2012 }
2013
Brian Osman28ba5282019-10-30 14:18:07 -04002014 float baseM[20];
2015 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2016
Brian Salomon024bd002019-06-11 11:38:16 -04002017 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2018
Mike Reed1f607332020-05-21 12:11:27 -04002019 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04002020
Brian Salomon9241a6d2019-10-03 13:26:54 -04002021 SkRect dstRectY = SkRect::Make(dstSize);
2022 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002023
Brian Salomon6aa65052020-01-28 12:16:53 -05002024 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2025 PixelTransferResult yTransfer, uTransfer, vTransfer;
2026
Brian Salomon024bd002019-06-11 11:38:16 -04002027 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2028 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002029 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002030 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002031 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002032 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002033 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002034 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2035 yPaint.addColorFragmentProcessor(std::move(yFP));
2036 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002037 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2038 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002039 if (!doSynchronousRead) {
2040 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2041 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2042 if (!yTransfer.fTransferBuffer) {
2043 callback(context, nullptr);
2044 return;
2045 }
Brian Salomon024bd002019-06-11 11:38:16 -04002046 }
2047
2048 texMatrix.preScale(2.f, 2.f);
2049 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2050 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002051 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002052 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002053 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002054 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002055 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002056 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2057 uPaint.addColorFragmentProcessor(std::move(uFP));
2058 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002059 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2060 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002061 if (!doSynchronousRead) {
2062 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2063 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2064 if (!uTransfer.fTransferBuffer) {
2065 callback(context, nullptr);
2066 return;
2067 }
Brian Salomon024bd002019-06-11 11:38:16 -04002068 }
2069
2070 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2071 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002072 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002073 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002074 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002075 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2076 this->colorInfo().alphaType(), texMatrix,
2077 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002078 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2079 vPaint.addColorFragmentProcessor(std::move(vFP));
2080 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002081 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2082 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002083 if (!doSynchronousRead) {
2084 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2085 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2086 if (!vTransfer.fTransferBuffer) {
2087 callback(context, nullptr);
2088 return;
2089 }
2090 }
2091
2092 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002093 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2094 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2095 size_t yRB = yInfo.minRowBytes();
2096 size_t uvRB = uvInfo.minRowBytes();
2097 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2098 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2099 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2100 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2101 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2102 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002103 callback(context, nullptr);
2104 return;
2105 }
2106 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002107 result->addCpuPlane(std::move(y), yRB );
2108 result->addCpuPlane(std::move(u), uvRB);
2109 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002110 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002111 return;
2112 }
2113
2114 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002115 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002116 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002117 GrClientMappedBufferManager* fMappedBufferManager;
2118 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002119 PixelTransferResult fYTransfer;
2120 PixelTransferResult fUTransfer;
2121 PixelTransferResult fVTransfer;
2122 };
2123 // Assumption is that the caller would like to flush. We could take a parameter or require an
2124 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2125 // callback to GrGpu until after the next flush that flushes our op list, though.
2126 auto* finishContext = new FinishContext{callback,
2127 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002128 direct->priv().clientMappedBufferManager(),
2129 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002130 std::move(yTransfer),
2131 std::move(uTransfer),
2132 std::move(vTransfer)};
2133 auto finishCallback = [](GrGpuFinishedContext c) {
2134 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002135 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002136 auto manager = context->fMappedBufferManager;
2137 size_t rowBytes = SkToSizeT(context->fSize.width());
2138 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2139 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002140 delete context;
2141 return;
2142 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002143 rowBytes /= 2;
2144 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2145 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2146 (*context->fClientCallback)(context->fClientContext, nullptr);
2147 delete context;
2148 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002149 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002150 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2151 (*context->fClientCallback)(context->fClientContext, nullptr);
2152 delete context;
2153 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002154 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002155 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002156 delete context;
2157 };
2158 GrFlushInfo flushInfo;
2159 flushInfo.fFinishedContext = finishContext;
2160 flushInfo.fFinishedProc = finishCallback;
2161 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Greg Daniel04283f32020-05-20 13:16:00 -04002162 direct->submit();
Brian Salomonab32f652019-05-10 14:24:50 -04002163}
2164
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002165GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2166 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002167 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002168 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002169 if (info.fSubmittedProc) {
2170 info.fSubmittedProc(info.fSubmittedContext, false);
2171 }
2172 if (info.fFinishedProc) {
2173 info.fFinishedProc(info.fFinishedContext);
2174 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002175 return GrSemaphoresSubmitted::kNo;
2176 }
robertphillips8c523e02016-07-26 07:41:00 -07002177 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002178 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002179
Greg Daniel55f040b2020-02-13 15:38:32 +00002180 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002181}
2182
Greg Danielc64ee462017-06-15 16:59:49 -04002183bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002184 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002185 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002186 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002187 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002188 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002189
2190 AutoCheckFlush acf(this->drawingManager());
2191
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002192 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002193 return false;
2194 }
2195
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002196 auto direct = fContext->priv().asDirectContext();
2197 if (!direct) {
2198 return false;
2199 }
2200
2201 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002202
Greg Daniel301015c2019-11-18 14:06:46 -05002203 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2204 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002205 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002206 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002207 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2208 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002209 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002210 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2211 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002212 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002213}
joshualitt33a5fce2015-11-18 13:28:51 -08002214
Brian Osman11052242016-10-27 14:47:55 -04002215void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002216 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002217 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002218 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002219 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002220 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002221 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002222 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002223 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002224 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2225
Michael Ludwig2686d692020-04-17 20:21:37 +00002226 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002227
Robert Phillips27927a52018-08-20 13:18:12 -04002228 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002229}
2230
2231void GrRenderTargetContext::drawShape(const GrClip& clip,
2232 GrPaint&& paint,
2233 GrAA aa,
2234 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002235 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002236 ASSERT_SINGLE_OWNER
2237 RETURN_IF_ABANDONED
2238 SkDEBUGCODE(this->validate();)
2239 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2240
Brian Salomon2fad74a2017-12-20 13:28:55 -05002241 if (shape.isEmpty()) {
2242 if (shape.inverseFilled()) {
2243 this->drawPaint(clip, std::move(paint), viewMatrix);
2244 }
2245 return;
robertphillipsea461502015-05-26 11:38:03 -07002246 }
2247
Robert Phillips72152832017-01-25 17:31:35 -05002248 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002249
Brian Salomon2fad74a2017-12-20 13:28:55 -05002250 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002251 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002252 SkRRect rrect;
2253 // We can ignore the starting point and direction since there is no path effect.
2254 bool inverted;
2255 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2256 if (rrect.isRect()) {
2257 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2258 &shape.style());
2259 return;
2260 } else if (rrect.isOval()) {
2261 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002262 return;
2263 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002264 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2265 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002266 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2267 viewMatrix.rectStaysRect()) {
2268 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2269 // the matrix to all the points individually rather than just to the rect
2270 SkRect rects[2];
2271 if (shape.asNestedRects(rects)) {
2272 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002273 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002274 fContext, std::move(paint), viewMatrix, rects);
2275 if (op) {
2276 this->addDrawOp(clip, std::move(op));
2277 }
2278 // Returning here indicates that there is nothing to draw in this case.
2279 return;
2280 }
robertphillipsea461502015-05-26 11:38:03 -07002281 }
2282 }
robertphillips4bc31812016-03-01 12:22:49 -08002283
Michael Ludwig6a6de652020-04-30 20:16:36 -04002284 // If we get here in drawShape(), we definitely need to use path rendering
2285 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2286 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002287}
2288
Chris Daltonbbfd5162017-11-07 13:35:22 -07002289bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002290 const GrUserStencilSettings* ss,
2291 SkRegion::Op op,
2292 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002293 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002294 const SkMatrix& viewMatrix,
2295 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002296 ASSERT_SINGLE_OWNER_PRIV
2297 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002298 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002299 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2300 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002301
2302 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002303 GrPaint paint;
2304 paint.setCoverageSetOpXPFactory(op, invert);
2305 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2306 SkRect::MakeIWH(fRenderTargetContext->width(),
2307 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002308 return true;
2309 }
2310
Robert Phillips72152832017-01-25 17:31:35 -05002311 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002312
2313 // An Assumption here is that path renderer would use some form of tweaking
2314 // the src color (either the input alpha or in the frag shader) to implement
2315 // aa. If we have some future driver-mojo path AA that can do the right
2316 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002317 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002318 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002319
Michael Ludwigc002d562020-05-13 14:17:57 -04002320 SkIRect clipConservativeBounds = clip.getConservativeBounds(fRenderTargetContext->width(),
2321 fRenderTargetContext->height());
Chris Daltondb91c6e2017-09-08 16:25:08 -06002322
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002323 GrPaint paint;
2324 paint.setCoverageSetOpXPFactory(op, invert);
2325
Michael Ludwig2686d692020-04-17 20:21:37 +00002326 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002327 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002328 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002329 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002330 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002331 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002332 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002333 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002334 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002335 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2336 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002337 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002338
2339 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002340 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002341 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002342 if (!pr) {
2343 return false;
2344 }
2345
Brian Salomonf3569f02017-10-24 12:52:33 -04002346 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2347 std::move(paint),
2348 ss,
2349 fRenderTargetContext,
2350 &clip,
2351 &clipConservativeBounds,
2352 &viewMatrix,
2353 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002354 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002355 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002356 pr->drawPath(args);
2357 return true;
2358}
2359
Brian Osman11052242016-10-27 14:47:55 -04002360SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002361 ASSERT_SINGLE_OWNER_PRIV
2362
Robert Phillips6a6de562019-02-15 15:19:15 -05002363 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002364 return SkBudgeted::kNo;
2365 }
2366
Brian Osman11052242016-10-27 14:47:55 -04002367 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002368
Greg Daniel3912a4b2020-01-14 09:56:04 -05002369 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002370}
2371
Brian Salomon2fad74a2017-12-20 13:28:55 -05002372void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2373 GrPaint&& paint,
2374 GrAA aa,
2375 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002376 const GrStyledShape& originalShape,
2377 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002378 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002379 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002380 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2381
Jim Van Verthf86073a2018-10-02 13:05:38 -04002382 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2383 return;
2384 }
2385
Michael Ludwig6a6de652020-04-30 20:16:36 -04002386 if (attemptShapeFallback && originalShape.simplified()) {
2387 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2388 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2389 // we ought to try again instead of going right to path rendering.
2390 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2391 return;
2392 }
2393
Michael Ludwigc002d562020-05-13 14:17:57 -04002394 SkIRect clipConservativeBounds = clip.getConservativeBounds(this->width(), this->height());
Chris Daltondb91c6e2017-09-08 16:25:08 -06002395
Michael Ludwig2686d692020-04-17 20:21:37 +00002396 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002397 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002398
robertphillips68737822015-10-29 12:12:21 -07002399 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002400 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002401 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002402 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002403 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002404 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002405 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002406 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002407 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002408
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002409 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002410 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002411 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002412 return;
2413 }
2414
Chris Dalton6ce447a2019-06-23 18:07:38 -06002415 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002416
2417 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2418 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2419 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2420
Brian Salomon2fad74a2017-12-20 13:28:55 -05002421 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002422 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002423 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2424 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002425 return;
2426 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002427 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002428 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002429 }
2430 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002431 if (canDrawArgs.fShape->style().applies()) {
2432 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2433 styleScale);
2434 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002435 return;
2436 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002437 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002438 // This time, allow SW renderer
2439 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2440 } else {
2441 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002442#if GR_PATH_RENDERER_SPEW
2443 SkDebugf("falling back to: %s\n", pr->name());
2444#endif
bsalomon6663acf2016-05-10 09:14:17 -07002445 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002446 }
robertphillipsea461502015-05-26 11:38:03 -07002447
bsalomon8acedde2016-06-24 10:42:16 -07002448 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002449#ifdef SK_DEBUG
2450 SkDebugf("Unable to find path renderer compatible with path.\n");
2451#endif
2452 return;
2453 }
2454
Robert Phillips256c37b2017-03-01 14:32:46 -05002455 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002456 std::move(paint),
2457 &GrUserStencilSettings::kUnused,
2458 this,
2459 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002460 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002461 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002462 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002463 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002464 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002465 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002466}
2467
Brian Salomon467921e2017-03-06 16:17:12 -05002468static void op_bounds(SkRect* bounds, const GrOp* op) {
2469 *bounds = op->bounds();
2470 if (op->hasZeroArea()) {
2471 if (op->hasAABloat()) {
2472 bounds->outset(0.5f, 0.5f);
2473 } else {
2474 // We don't know which way the particular GPU will snap lines or points at integer
2475 // coords. So we ensure that the bounds is large enough for either snap.
2476 SkRect before = *bounds;
2477 bounds->roundOut(bounds);
2478 if (bounds->fLeft == before.fLeft) {
2479 bounds->fLeft -= 1;
2480 }
2481 if (bounds->fTop == before.fTop) {
2482 bounds->fTop -= 1;
2483 }
2484 if (bounds->fRight == before.fRight) {
2485 bounds->fRight += 1;
2486 }
2487 if (bounds->fBottom == before.fBottom) {
2488 bounds->fBottom += 1;
2489 }
2490 }
2491 }
2492}
2493
Chris Dalton08755122019-08-05 16:13:47 -06002494void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002495 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002496 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2497}
2498
Brian Salomon348a0372018-10-31 10:42:18 -04002499void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2500 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002501 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002502 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002503 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002504 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002505 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002506 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002507 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002508 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002509
Brian Salomon467921e2017-03-06 16:17:12 -05002510 // Setup clip
2511 SkRect bounds;
2512 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002513 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002514 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002515 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002516 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002517
Chris Dalton9acf6822019-11-12 11:52:40 -07002518 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002519 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002520 }
2521
Chris Dalton9acf6822019-11-12 11:52:40 -07002522 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002523 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002524 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002525 }
2526
Chris Dalton9acf6822019-11-12 11:52:40 -07002527 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2528 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2529
2530 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2531 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2532 // that uses a stencil test when the stencil buffer is multisampled.)
2533 bool hasMixedSampledCoverage = (
2534 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002535 SkASSERT(!hasMixedSampledCoverage ||
2536 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002537
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002538 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002539 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002540 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2541
Greg Daniel524e28b2019-11-01 11:48:53 -04002542 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002543 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002544 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002545 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002546 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002547 }
2548 }
2549
2550 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002551 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002552 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002553 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002554 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002555 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002556 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002557}
2558
Greg Daniel524e28b2019-11-01 11:48:53 -04002559bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2560 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002561 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2562 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2563 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002564 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002565 return false;
2566 }
2567
Greg Daniel3912a4b2020-01-14 09:56:04 -05002568 if (this->caps()->textureBarrierSupport() &&
2569 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002570 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002571 // The render target is a texture, so we can read from it directly in the shader. The XP
2572 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002573 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002574 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002575 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002576 }
2577 }
2578
Greg Daniel3912a4b2020-01-14 09:56:04 -05002579 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002580
Michael Ludwigc002d562020-05-13 14:17:57 -04002581 SkIRect clippedRect = clip.getConservativeBounds(this->width(), this->height());
Brian Salomon09181ef2018-11-14 13:39:51 -05002582 SkRect opBounds = op.bounds();
2583 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2584 // 0.5 pixels.
2585 if (op.hasAABloat() || op.hasZeroArea()) {
2586 opBounds.outset(0.5f, 0.5f);
2587 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2588 // performance we may ignore the clip when the draw is entirely inside the clip is float
2589 // space but will hit pixels just outside the clip when actually rasterizing.
2590 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002591 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002592 }
2593 SkIRect opIBounds;
2594 opBounds.roundOut(&opIBounds);
2595 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002596#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002597 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002598#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002600 }
2601
Greg Daniel3155f7f2020-01-16 16:54:26 -05002602 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002603 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2604 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002605 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002606 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002607
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002608 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002609 copyRect = clippedRect;
2610 }
Brian Salomon467921e2017-03-06 16:17:12 -05002611
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002612 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002613 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002614 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002615 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002616 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002617 } else {
Eric Karl74480882017-04-03 14:49:05 -07002618 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002619 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002620 }
Brian Salomonc5243782020-04-02 12:50:34 -04002621 auto copy =
2622 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2623 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2624 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002625
Brian Salomonc5243782020-04-02 12:50:34 -04002626 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002627 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002628 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002629}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002630
Greg Daniel573312e2020-02-07 17:22:35 -05002631bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002632 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002633 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002634 SkIRect clippedSrcRect;
2635 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002636 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2637 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002638 return false;
2639 }
2640
2641 GrPaint paint;
2642 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002643
Greg Danield2ccbb52020-02-05 10:45:39 -05002644 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002645 if (!fp) {
2646 return false;
2647 }
2648 paint.addColorFragmentProcessor(std::move(fp));
2649
2650 this->fillRectToRect(
2651 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2652 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2653 clippedSrcRect.height()),
2654 SkRect::Make(clippedSrcRect));
2655 return true;
2656}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002657
2658void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2659 SkASSERT(&task == fOpsTask.get());
2660 fOpsTask.reset();
2661}