blob: 0918fb5de111f2fa24d2b6f3bb70e259cd328642 [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
Michael Ludwig7c12e282020-05-29 09:54:07 -040084 void addDrawOp(const GrClip* clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040085 fRenderTargetContext->addDrawOp(clip, std::move(op));
86 }
87
Michael Ludwig7c12e282020-05-29 09:54:07 -040088 void drawShape(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -040089 const SkPaint& paint,
90 const SkMatrixProvider& matrixProvider,
91 const GrStyledShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040092 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
Brian Osman449b1152020-04-15 16:43:00 -040093 clip, paint, matrixProvider, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 }
95
Brian Osman449b1152020-04-15 16:43:00 -040096 void makeGrPaint(GrMaskFormat maskFormat,
97 const SkPaint& skPaint,
98 const SkMatrixProvider& matrixProvider,
Robert Phillips7c525e62018-06-12 10:11:12 -040099 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -0500100 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400101 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102 if (kARGB_GrMaskFormat == maskFormat) {
Brian Osman449b1152020-04-15 16:43:00 -0400103 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, matrixProvider,
104 grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400105 } else {
Brian Osman449b1152020-04-15 16:43:00 -0400106 SkPaintToGrPaint(context, colorInfo, skPaint, matrixProvider, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107 }
108 }
109
Robert Phillips69893702019-02-22 11:16:30 -0500110 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 return fRenderTargetContext->fContext;
112 }
113
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter* glyphPainter() override {
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())
Adlai Holler33dbd652020-06-01 12:35:42 -0400125#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
126#define ASSERT_SINGLE_OWNER_PRIV GR_ASSERT_SINGLE_OWNER(fRenderTargetContext->singleOwner())
Robert Phillips69893702019-02-22 11:16:30 -0500127#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
128#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
129#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
130#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
131#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700132
Brian Salomone225b562017-06-14 13:00:03 -0400133//////////////////////////////////////////////////////////////////////////////
134
robertphillipsea461502015-05-26 11:38:03 -0700135class AutoCheckFlush {
136public:
halcanary9d524f22016-03-29 09:03:52 -0700137 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700138 SkASSERT(fDrawingManager);
139 }
bsalomonb77a9072016-09-07 10:02:04 -0700140 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700141
142private:
robertphillips77a2e522015-10-17 07:43:27 -0700143 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700144};
145
Greg Daniele20fcad2020-01-08 11:52:34 -0500146std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
147 GrRecordingContext* context,
148 GrColorType colorType,
149 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500150 sk_sp<GrSurfaceProxy> proxy,
151 GrSurfaceOrigin origin,
152 const SkSurfaceProps* surfaceProps,
153 bool managedOps) {
154 if (!proxy) {
155 return nullptr;
156 }
157
158 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400159 GrSwizzle readSwizzle, writeSwizzle;
160 if (colorType != GrColorType::kUnknown) {
161 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
162 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
163 }
Greg Danielba0ff782020-01-07 15:42:57 -0500164
Greg Daniel3912a4b2020-01-14 09:56:04 -0500165 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400166 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500167
168 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400169 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500170 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500171}
172
173std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
174 GrRecordingContext* context,
175 GrColorType colorType,
176 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500177 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500178 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500179 const GrBackendFormat& format,
180 int sampleCnt,
181 GrMipMapped mipMapped,
182 GrProtected isProtected,
183 GrSurfaceOrigin origin,
184 SkBudgeted budgeted,
185 const SkSurfaceProps* surfaceProps) {
186 // It is probably not necessary to check if the context is abandoned here since uses of the
187 // GrRenderTargetContext which need the context will mostly likely fail later on without an
188 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
189 // an abandoned context correctly. It also lets us early out of some extra work.
190 if (context->priv().abandoned()) {
191 return nullptr;
192 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500193
Greg Daniele20fcad2020-01-08 11:52:34 -0500194 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400195 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500196 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500197 if (!proxy) {
198 return nullptr;
199 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500200
Greg Danielba0ff782020-01-07 15:42:57 -0500201 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
202 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500203 if (!rtc) {
204 return nullptr;
205 }
206 rtc->discard();
207 return rtc;
208}
209
210std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
211 GrRecordingContext* context,
212 GrColorType colorType,
213 sk_sp<SkColorSpace> colorSpace,
214 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500215 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500216 int sampleCnt,
217 GrMipMapped mipMapped,
218 GrProtected isProtected,
219 GrSurfaceOrigin origin,
220 SkBudgeted budgeted,
221 const SkSurfaceProps* surfaceProps) {
222 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
223 if (!format.isValid()) {
224 return nullptr;
225 }
226
227 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
228 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
229 surfaceProps);
230}
231
232static inline GrColorType color_type_fallback(GrColorType ct) {
233 switch (ct) {
234 // kRGBA_8888 is our default fallback for many color types that may not have renderable
235 // backend formats.
236 case GrColorType::kAlpha_8:
237 case GrColorType::kBGR_565:
238 case GrColorType::kABGR_4444:
239 case GrColorType::kBGRA_8888:
240 case GrColorType::kRGBA_1010102:
Robert Phillips9a30ee02020-04-29 08:58:39 -0400241 case GrColorType::kBGRA_1010102:
Greg Daniele20fcad2020-01-08 11:52:34 -0500242 case GrColorType::kRGBA_F16:
243 case GrColorType::kRGBA_F16_Clamped:
244 return GrColorType::kRGBA_8888;
245 case GrColorType::kAlpha_F16:
246 return GrColorType::kRGBA_F16;
247 case GrColorType::kGray_8:
248 return GrColorType::kRGB_888x;
249 default:
250 return GrColorType::kUnknown;
251 }
252}
253
Brian Salomond005b692020-04-01 15:47:05 -0400254std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400255 GrImageContext* context, GrColorType colorType, int sampleCnt) {
256 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400257 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400258 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
259 // We continue to the fallback color type if there no default renderable format or we
260 // requested msaa and the format doesn't support msaa.
261 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400262 return {colorType, format};
263 }
264 colorType = color_type_fallback(colorType);
265 } while (colorType != GrColorType::kUnknown);
266 return {GrColorType::kUnknown, {}};
267}
268
Greg Daniele20fcad2020-01-08 11:52:34 -0500269std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
270 GrRecordingContext* context,
271 GrColorType colorType,
272 sk_sp<SkColorSpace> colorSpace,
273 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500274 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500275 int sampleCnt,
276 GrMipMapped mipMapped,
277 GrProtected isProtected,
278 GrSurfaceOrigin origin,
279 SkBudgeted budgeted,
280 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400281 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400282 if (ct == GrColorType::kUnknown) {
283 return nullptr;
284 }
285 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
286 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500287}
288
Greg Danielba0ff782020-01-07 15:42:57 -0500289std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
290 GrRecordingContext* context,
291 GrColorType colorType,
292 sk_sp<SkColorSpace> colorSpace,
293 const GrBackendTexture& tex,
294 int sampleCnt,
295 GrSurfaceOrigin origin,
296 const SkSurfaceProps* surfaceProps,
297 ReleaseProc releaseProc,
298 ReleaseContext releaseCtx) {
299 SkASSERT(sampleCnt > 0);
300 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400301 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500302 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500303 if (!proxy) {
304 return nullptr;
305 }
306
307 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
308 origin, surfaceProps);
309}
310
311std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
312 GrRecordingContext* context,
313 GrColorType colorType,
314 sk_sp<SkColorSpace> colorSpace,
315 const GrBackendTexture& tex,
316 int sampleCnt,
317 GrSurfaceOrigin origin,
318 const SkSurfaceProps* surfaceProps) {
319 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400320 sk_sp<GrSurfaceProxy> proxy(
321 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500322 if (!proxy) {
323 return nullptr;
324 }
325
326 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
327 origin, surfaceProps);
328}
329
330std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
331 GrRecordingContext* context,
332 GrColorType colorType,
333 sk_sp<SkColorSpace> colorSpace,
334 const GrBackendRenderTarget& rt,
335 GrSurfaceOrigin origin,
336 const SkSurfaceProps* surfaceProps,
337 ReleaseProc releaseProc,
338 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400339 sk_sp<GrSurfaceProxy> proxy(
340 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500341 if (!proxy) {
342 return nullptr;
343 }
344
345 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
346 origin, surfaceProps);
347}
348
349std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
350 GrRecordingContext* context,
351 const SkImageInfo& imageInfo,
352 const GrVkDrawableInfo& vkInfo,
353 const SkSurfaceProps* props) {
354 sk_sp<GrSurfaceProxy> proxy(
355 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
356 vkInfo));
357 if (!proxy) {
358 return nullptr;
359 }
360
Greg Danielba0ff782020-01-07 15:42:57 -0500361 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
362 imageInfo.refColorSpace(), std::move(proxy),
363 kTopLeft_GrSurfaceOrigin, props);
364}
365
Greg Danielf41b2bd2019-08-22 16:19:24 -0400366// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
367// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
368// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
369// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500370GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500371 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400372 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400373 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400374 sk_sp<SkColorSpace> colorSpace,
375 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400376 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500377 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
378 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400379 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500380 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400381 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400382 , fManagedOpsTask(managedOpsTask) {
Brian Salomon3b8486a2020-04-21 12:43:26 -0400383 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400384 fOpsTask->addClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400385 }
Brian Salomon8afde5f2020-04-01 16:22:00 -0400386 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
387 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500388
Brian Salomonf18b1d82017-10-27 11:30:49 -0400389 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700390 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700391}
392
robertphillips2e1e51f2015-10-15 08:01:48 -0700393#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500394void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400395 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400396 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700397 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700398}
399#endif
400
Brian Osman11052242016-10-27 14:47:55 -0400401GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800402 ASSERT_SINGLE_OWNER
Brian Salomon3b8486a2020-04-21 12:43:26 -0400403 if (fOpsTask) {
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400404 fOpsTask->removeClosedObserver(this);
Brian Salomon3b8486a2020-04-21 12:43:26 -0400405 }
robertphillipsa106c622015-10-16 09:07:06 -0700406}
407
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600409 if (GrAA::kNo == aa) {
410 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
411 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600412 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600413 return GrAAType::kMSAA;
414 }
415 return GrAAType::kNone;
416 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600417 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600418}
419
Greg Daniele252f082017-10-23 16:05:23 -0400420GrMipMapped GrRenderTargetContext::mipMapped() const {
421 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
422 return proxy->mipMapped();
423 }
424 return GrMipMapped::kNo;
425}
426
Greg Danielf41b2bd2019-08-22 16:19:24 -0400427GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800428 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700429 SkDEBUGCODE(this->validate();)
430
Brian Salomon3b8486a2020-04-21 12:43:26 -0400431 if (!fOpsTask) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600432 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400433 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600434 if (fOpsTask && fNumStencilSamples > 0) {
435 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600436 fOpsTask->setMustPreserveStencil();
437 // Reload the stencil buffer content at the beginning of newOpsTask.
438 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
439 // values?
440 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
441 }
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400442 newOpsTask->addClosedObserver(this);
Chris Dalton674f77a2019-09-30 20:49:39 -0600443 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700444 }
Brian Salomon3b8486a2020-04-21 12:43:26 -0400445 SkASSERT(!fOpsTask->isClosed());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400446 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700447}
448
Michael Ludwig7c12e282020-05-29 09:54:07 -0400449void GrRenderTargetContext::drawGlyphRunList(const GrClip* clip,
Brian Osman449b1152020-04-15 16:43:00 -0400450 const SkMatrixProvider& matrixProvider,
451 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800452 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700453 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700454 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700456
Greg Danielbe7fc462019-01-03 16:40:42 -0500457 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
458 // secondary command buffers because it would require stopping and starting a render pass which
459 // we don't have access to.
460 if (this->wrapsVkSecondaryCB()) {
461 return;
462 }
463
Herb Derby26cbe512018-05-24 14:39:01 -0400464 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Brian Osman449b1152020-04-15 16:43:00 -0400465 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, matrixProvider,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400466 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700467}
468
Brian Osman11052242016-10-27 14:47:55 -0400469void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800470 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700471 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700472 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400473 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700474
Robert Phillips72152832017-01-25 17:31:35 -0500475 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400476
Greg Danielf41b2bd2019-08-22 16:19:24 -0400477 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700478}
479
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500480static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
481 paint->setColor4f(color);
482 if (color.isOpaque()) {
483 // Can just rely on the src-over blend mode to do the right thing
484 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
485 } else {
486 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
487 // were src blended
488 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
489 }
490}
491
Michael Ludwig81d41722020-05-26 16:57:38 -0400492// NOTE: We currently pass the premul color unmodified to the gpu, since we assume the GrRTC has a
493// premul alpha type. If we ever support different alpha type render targets, this function should
494// transform the color as appropriate.
495void GrRenderTargetContext::internalClear(const SkIRect* scissor,
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500496 const SkPMColor4f& color,
Michael Ludwig81d41722020-05-26 16:57:38 -0400497 bool upgradePartialToFull) {
498 ASSERT_SINGLE_OWNER
499 RETURN_IF_ABANDONED
500 SkDEBUGCODE(this->validate();)
501 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
502
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400503 // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
504 // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
505 // supports then. Drawing an axis-aligned rect is the fallback path.
506 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
507 if (scissor && !scissorState.set(*scissor)) {
508 // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
509 // except clear ops are not draw ops).
510 return;
511 }
512
513 // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
514 // the test. We only do this when the clear would be handled by a load op or natively.
515 if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
516 if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
517 this->caps()->shouldInitializeTextures())) {
518 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
519 // only clear the entire target if we knew it had not been cleared before. As
520 // is this could end up doing a lot of redundant clears.
521 scissorState.setDisabled();
522 } else {
523 // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
524 // render target to overflow into any approx-fit padding of the backing store dimensions
525 scissorState.relaxTest(this->dimensions());
Michael Ludwig81d41722020-05-26 16:57:38 -0400526 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500527 }
528
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400529 if (!scissorState.enabled()) {
530 // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
531 // discard all prior ops in the current task since the color buffer will be overwritten.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400532 GrOpsTask* opsTask = this->getOpsTask();
533 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500534 !this->caps()->performColorClearsAsDraws()) {
535 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400536 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500537 return;
538 } else {
539 // Will use an op for the clear, reset the load op to discard since the op will
540 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400541 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500542 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400543 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500544
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400545 // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
546 // as a load op.
547 bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
548 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
549 if (clearAsDraw) {
550 GrPaint paint;
551 clear_to_grpaint(color, &paint);
552 this->addDrawOp(nullptr,
553 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
554 SkRect::Make(scissorState.rect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500555 } else {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400556 this->addOp(GrClearOp::Make(fContext, scissorState, color));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500557 }
558}
559
Michael Ludwig7c12e282020-05-29 09:54:07 -0400560void GrRenderTargetContext::drawPaint(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500561 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400562 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000563 // Start with the render target, since that is the maximum content we could possibly fill.
564 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500565 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400566 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000567 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
568 // inverse view matrix.
569 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
570 } else {
571 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
572 SkMatrix localMatrix;
573 if (!viewMatrix.invert(&localMatrix)) {
574 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400575 }
Michael Ludwig61328202019-06-19 14:48:58 +0000576 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
577 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700578 }
robertphillipsea461502015-05-26 11:38:03 -0700579}
580
Michael Ludwig61328202019-06-19 14:48:58 +0000581enum class GrRenderTargetContext::QuadOptimization {
582 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
583 kDiscarded,
584 // The rect to draw was converted to some other op and appended to the oplist, so no additional
585 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
586 // a constColor is provided.
587 kSubmitted,
588 // The clip was folded into the device quad, with updated edge flags and local coords, and
589 // caller is responsible for adding an appropriate op.
590 kClipApplied,
591 // No change to clip, but quad updated to better fit clip/render target, and caller is
592 // responsible for adding an appropriate op.
593 kCropped
594};
Michael Ludwig61a16512019-01-15 11:15:13 -0500595
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400596static bool make_vertex_finite(float* value) {
597 if (SkScalarIsNaN(*value)) {
598 return false;
599 }
600
601 if (!SkScalarIsFinite(*value)) {
602 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
603 // left when calculating crops.
604 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
605 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
606 }
607
608 return true;
609}
610
Michael Ludwig7c12e282020-05-29 09:54:07 -0400611static SkIRect get_clip_bounds(const GrRenderTargetContext* rtc, const GrClip* clip) {
612 return clip ? clip->getConservativeBounds(rtc->width(), rtc->height())
613 : SkIRect::MakeWH(rtc->width(), rtc->height());
614}
615
Michael Ludwig61328202019-06-19 14:48:58 +0000616GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400617 const GrClip* clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500618 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000619 // Optimization requirements:
620 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500621 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
622 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
623 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
624 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
625 // 5. kCropped in all other scenarios (although a crop may be a no-op)
626
627 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
628 // better to just keep the old flags instead of introducing mixed edge flags.
629 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000630
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400631 // Use the logical size of the render target, which allows for "fullscreen" clears even if
632 // the render target has an approximate backing fit
633 SkRect rtRect = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400634
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500635 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000636 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400637 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
638 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500639 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400640 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500641 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
642 !make_vertex_finite(quad->fDevice.ys() + i) ||
643 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400644 // Discard if we see a nan
645 return QuadOptimization::kDiscarded;
646 }
647 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500648 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400649 }
650 } else {
651 // CropToRect requires the quads to be finite. If they are not finite and we have local
652 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500653 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400654 return QuadOptimization::kDiscarded;
655 }
Brian Salomon7694b902019-06-18 21:00:21 +0000656 }
657
Michael Ludwig61328202019-06-19 14:48:58 +0000658 // If the quad is entirely off screen, it doesn't matter what the clip does
659 if (!rtRect.intersects(drawBounds)) {
660 return QuadOptimization::kDiscarded;
661 }
Brian Salomon7694b902019-06-18 21:00:21 +0000662
Michael Ludwig61328202019-06-19 14:48:58 +0000663 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
664 // the render target).
665 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400666 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
667 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
668 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
669 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000670 bool axisAlignedClip = true;
Michael Ludwig7c12e282020-05-29 09:54:07 -0400671 if (clip && !clip->quickContains(rtRect)) {
672 if (!clip->isRRect(rtRect, &clipRRect, &clipAA)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000673 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000674 }
Brian Salomon7694b902019-06-18 21:00:21 +0000675 }
676
Michael Ludwig61328202019-06-19 14:48:58 +0000677 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
678 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400679 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000680 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
681 // of the render target and the clip rect)
682 SkRect clipBounds = rtRect;
683 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
684 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000685 }
686
Michael Ludwig61328202019-06-19 14:48:58 +0000687 if (clipRRect.isRect()) {
688 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500689 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700690 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500691 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000692 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500693 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000694 if (drawBounds.contains(rtRect)) {
695 // Fullscreen clear
Michael Ludwig81d41722020-05-26 16:57:38 -0400696 this->clear(*constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000697 return QuadOptimization::kSubmitted;
698 } else if (GrClip::IsPixelAligned(drawBounds) &&
699 drawBounds.width() > 256 && drawBounds.height() > 256) {
700 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
701 SkIRect scissorRect;
702 drawBounds.round(&scissorRect);
Michael Ludwig81d41722020-05-26 16:57:38 -0400703 this->clear(scissorRect, *constColor);
Michael Ludwig61328202019-06-19 14:48:58 +0000704 return QuadOptimization::kSubmitted;
705 }
706 }
707
708 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000709 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500710 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000711 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
712 // properly reflect the smooth edge of the clip.
713 *aa = GrAA::kYes;
714 }
715 // We intentionally do not downgrade AA here because we don't know if we need to
716 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
717 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
718
719 // deviceQuad is exactly the intersection of original quad and clip, so it can be
720 // drawn with no clip (submitted by caller)
721 return QuadOptimization::kClipApplied;
722 } else {
723 // The quads have been updated to better fit the clip bounds, but can't get rid of
724 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500725 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000726 return QuadOptimization::kCropped;
727 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700728 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000729 // Rounded corners and constant filled color (limit ourselves to solid colors because
730 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500731 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
732 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
733 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000734 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
735 // we can draw the rrect directly and ignore the edge flags
736 GrPaint paint;
737 clear_to_grpaint(*constColor, &paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400738 this->drawRRect(nullptr, std::move(paint), clipAA, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +0000739 clipRRect, GrStyle::SimpleFill());
740 return QuadOptimization::kSubmitted;
741 } else {
742 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500743 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000744 return QuadOptimization::kCropped;
745 }
Brian Salomon7694b902019-06-18 21:00:21 +0000746 }
Brian Salomon7694b902019-06-18 21:00:21 +0000747 }
748
Michael Ludwig61328202019-06-19 14:48:58 +0000749 // Crop the quad to the conservative bounds of the clip.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400750 SkRect clipBounds = SkRect::Make(get_clip_bounds(this, clip));
Michael Ludwig61328202019-06-19 14:48:58 +0000751
752 // One final check for discarding, since we may have gone here directly due to a complex clip
753 if (!clipBounds.intersects(drawBounds)) {
754 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000755 }
756
Michael Ludwig61328202019-06-19 14:48:58 +0000757 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
758 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500759 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
760 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000761
762 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000763}
764
Michael Ludwig7c12e282020-05-29 09:54:07 -0400765void GrRenderTargetContext::drawFilledQuad(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500766 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500767 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500768 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400769 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000770 ASSERT_SINGLE_OWNER
771 RETURN_IF_ABANDONED
772 SkDEBUGCODE(this->validate();)
773 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500774
Michael Ludwig61328202019-06-19 14:48:58 +0000775 AutoCheckFlush acf(this->drawingManager());
776
777 SkPMColor4f* constColor = nullptr;
778 SkPMColor4f paintColor;
779 if (!ss && !paint.numCoverageFragmentProcessors() &&
780 paint.isConstantBlendedColor(&paintColor)) {
781 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
782 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500783 }
784
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500785 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000786 if (opt >= QuadOptimization::kClipApplied) {
787 // These optimizations require caller to add an op themselves
Michael Ludwig7c12e282020-05-29 09:54:07 -0400788 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwig61328202019-06-19 14:48:58 +0000789 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
790 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500791 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
792 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700793 }
Michael Ludwig61328202019-06-19 14:48:58 +0000794 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800795}
796
Michael Ludwig7c12e282020-05-29 09:54:07 -0400797void GrRenderTargetContext::drawTexturedQuad(const GrClip* clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400798 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500799 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000800 sk_sp<GrColorSpaceXform> textureXform,
801 GrSamplerState::Filter filter,
802 const SkPMColor4f& color,
803 SkBlendMode blendMode,
804 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500805 DrawQuad* quad,
Brian Salomon2432d062020-04-16 20:48:09 -0400806 const SkRect* subset) {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000807 ASSERT_SINGLE_OWNER
808 RETURN_IF_ABANDONED
809 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400810 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000811 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
812
813 AutoCheckFlush acf(this->drawingManager());
814
815 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
816 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500817 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000818
819 SkASSERT(opt != QuadOptimization::kSubmitted);
820 if (opt != QuadOptimization::kDiscarded) {
821 // And the texture op if not discarded
Michael Ludwig7c12e282020-05-29 09:54:07 -0400822 const GrClip* finalClip = opt == QuadOptimization::kClipApplied ? nullptr : clip;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000823 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400824 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400825 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
826 : GrTextureOp::Saturate::kNo;
Brian Salomon2432d062020-04-16 20:48:09 -0400827 // Use the provided subset, although hypothetically we could detect that the cropped local
828 // quad is sufficiently inside the subset and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500829 this->addDrawOp(finalClip,
830 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
831 std::move(textureXform), filter, color, saturate,
Brian Salomon2432d062020-04-16 20:48:09 -0400832 blendMode, aaType, quad, subset));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000833 }
834}
835
Michael Ludwig7c12e282020-05-29 09:54:07 -0400836void GrRenderTargetContext::drawRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500837 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500838 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400839 const SkMatrix& viewMatrix,
840 const SkRect& rect,
841 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700842 if (!style) {
843 style = &GrStyle::SimpleFill();
844 }
joshualitt1de610a2016-01-06 08:26:09 -0800845 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700846 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700847 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400848 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700849
bsalomon6663acf2016-05-10 09:14:17 -0700850 // Path effects should've been devolved to a path in SkGpuDevice
851 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700852
Robert Phillips72152832017-01-25 17:31:35 -0500853 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700854
bsalomon6663acf2016-05-10 09:14:17 -0700855 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400856 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000857 // Fills the rect, using rect as its own local coordinates
858 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500859 return;
Michael Ludwigf38b7112020-04-30 13:47:00 -0400860 } else if ((stroke.getStyle() == SkStrokeRec::kStroke_Style ||
861 stroke.getStyle() == SkStrokeRec::kHairline_Style) &&
862 (rect.width() && rect.height())) {
863 // Only use the StrokeRectOp for non-empty rectangles. Empty rectangles will be processed by
864 // GrStyledShape to handle stroke caps and dashing properly.
Brian Salomonbaaf4392017-06-15 09:59:23 -0400865 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700866
Chris Dalton7d6748e2019-03-13 00:34:52 -0600867 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500868 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
869 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
870 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500871 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400872 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700873 return;
robertphillips4bc31812016-03-01 12:22:49 -0800874 }
robertphillips4bc31812016-03-01 12:22:49 -0800875 }
Mike Klein16885072018-12-11 09:54:31 -0500876 assert_alive(paint);
Michael Ludwig2686d692020-04-17 20:21:37 +0000877 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
878 GrStyledShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700879}
880
Michael Ludwig7c12e282020-05-29 09:54:07 -0400881void GrRenderTargetContext::drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa,
Michael Ludwig69858532018-11-28 15:34:34 -0500882 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
883 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600884 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500885
886 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
887 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500888}
889
Robert Phillipsec2249f2016-11-09 08:54:35 -0500890int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500891 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400892 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500893}
894
Greg Danielf41b2bd2019-08-22 16:19:24 -0400895GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600896 ) const {
897#if GR_TEST_UTILS
898 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400899 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600900 }
901#endif
902 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
903 // would normally be overwritten. The one exception is if the render target context is marked as
904 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
905 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
906 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
907 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400908 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600909}
910
Chris Dalton858cf232019-10-14 16:20:00 -0600911void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600912 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600913 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600914 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600915
Chris Daltoneffee202019-07-01 22:28:03 -0600916 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600917 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500918 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600919 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400920 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600921 }
922 SkASSERT(numRequiredSamples > 0);
923
924 if (numRequiredSamples > fNumStencilSamples) {
925 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500926 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600927 }
Chris Dalton6b982802019-06-27 13:53:46 -0600928
Chris Dalton674f77a2019-09-30 20:49:39 -0600929 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600930 if (this->caps()->performStencilClearsAsDraws()) {
931 // There is a driver bug with clearing stencil. We must use an op to manually clear the
932 // stencil buffer before the op that required 'setNeedsStencil'.
Michael Ludwig81d41722020-05-26 16:57:38 -0400933 this->internalStencilClear(nullptr, /* inside mask */ false);
Chris Dalton6b982802019-06-27 13:53:46 -0600934 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600935 this->getOpsTask()->setInitialStencilContent(
936 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600937 }
938 }
939}
940
Michael Ludwig81d41722020-05-26 16:57:38 -0400941void GrRenderTargetContext::internalStencilClear(const SkIRect* scissor, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600942 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
943
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400944 GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
945 if (scissor && !scissorState.set(*scissor)) {
946 // The requested clear region is off screen, so nothing to do.
947 return;
948 }
949
Michael Ludwig1e632792020-05-21 12:45:31 -0400950 bool clearWithDraw = this->caps()->performStencilClearsAsDraws() ||
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400951 (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
Michael Ludwig81d41722020-05-26 16:57:38 -0400952 if (clearWithDraw) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500953 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500954
955 // Configure the paint to have no impact on the color buffer
956 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400957 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400958 this->addDrawOp(nullptr,
959 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
960 SkRect::Make(scissorState.rect()), ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500961 } else {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400962 this->addOp(GrClearStencilClipOp::Make(fContext, scissorState, insideStencilMask));
Robert Phillipse60ad622016-11-17 10:22:48 -0500963 }
robertphillips976f5f02016-06-03 10:59:20 -0700964}
965
Michael Ludwig7c12e282020-05-29 09:54:07 -0400966void GrRenderTargetContextPriv::stencilPath(const GrHardClip* clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600967 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400968 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400969 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500970 ASSERT_SINGLE_OWNER_PRIV
971 RETURN_IF_ABANDONED_PRIV
972 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400973 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
974 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500975
Brian Salomon467921e2017-03-06 16:17:12 -0500976 // TODO: extract portions of checkDraw that are relevant to path stenciling.
977 SkASSERT(path);
978 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
979
980 // FIXME: Use path bounds instead of this WAR once
981 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
982 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
983
Michael Ludwig7c12e282020-05-29 09:54:07 -0400984 // Setup clip and reject offscreen paths; we do this explicitly instead of relying on addDrawOp
985 // because GrStencilPathOp is not a draw op as its state depends directly on the choices made
986 // during this clip application.
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400987 GrAppliedHardClip appliedClip(fRenderTargetContext->dimensions(),
988 fRenderTargetContext->asSurfaceProxy()->backingStoreDimensions());
989
Michael Ludwig7c12e282020-05-29 09:54:07 -0400990 if (clip && !clip->apply(fRenderTargetContext->width(), fRenderTargetContext->height(),
991 &appliedClip, &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500992 return;
993 }
Michael Ludwig7c12e282020-05-29 09:54:07 -0400994 // else see FIXME above; we'd normally want to check path bounds with render target bounds,
995 // but as it is, we're just using the full render target so intersecting the two bounds would
996 // do nothing.
Brian Salomon467921e2017-03-06 16:17:12 -0500997
Robert Phillips7c525e62018-06-12 10:11:12 -0400998 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
999 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001000 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001001 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001002 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001003 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001004 if (!op) {
1005 return;
1006 }
Brian Salomon97180af2017-03-14 13:42:58 -04001007 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001008
Chris Daltoneffee202019-07-01 22:28:03 -06001009 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001010 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001011}
1012
Michael Ludwig7c12e282020-05-29 09:54:07 -04001013void GrRenderTargetContext::drawTextureSet(const GrClip* clip, TextureSetEntry set[],
Michael Ludwig379e4962019-12-06 13:21:26 -05001014 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001015 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001016 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1017 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001018 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001019 ASSERT_SINGLE_OWNER
1020 RETURN_IF_ABANDONED
1021 SkDEBUGCODE(this->validate();)
1022 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001023
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001024 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1025 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1026 AutoCheckFlush acf(this->drawingManager());
1027 GrAAType aaType = this->chooseAAType(aa);
1028 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1029 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1030 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001031 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1032 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001033}
1034
Michael Ludwig7c12e282020-05-29 09:54:07 -04001035void GrRenderTargetContext::drawVertices(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001036 GrPaint&& paint,
Brian Osman449b1152020-04-15 16:43:00 -04001037 const SkMatrixProvider& matrixProvider,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001038 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001039 GrPrimitiveType* overridePrimType,
Brian Osman449b1152020-04-15 16:43:00 -04001040 const SkRuntimeEffect* effect) {
Brian Salomon199fb872017-02-06 09:41:10 -05001041 ASSERT_SINGLE_OWNER
1042 RETURN_IF_ABANDONED
1043 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001044 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001045
1046 AutoCheckFlush acf(this->drawingManager());
1047
1048 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001049 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001050 std::unique_ptr<GrDrawOp> op =
Brian Osman449b1152020-04-15 16:43:00 -04001051 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), matrixProvider,
Brian Osmand1afef62020-04-09 16:24:23 -04001052 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
Brian Osman449b1152020-04-15 16:43:00 -04001053 overridePrimType, effect);
Brian Salomonc2f42542017-07-12 14:11:22 -04001054 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001055}
1056
1057///////////////////////////////////////////////////////////////////////////////
1058
Michael Ludwig7c12e282020-05-29 09:54:07 -04001059void GrRenderTargetContext::drawAtlas(const GrClip* clip,
Brian Osman4d92b892019-03-24 00:53:23 +00001060 GrPaint&& paint,
1061 const SkMatrix& viewMatrix,
1062 int spriteCount,
1063 const SkRSXform xform[],
1064 const SkRect texRect[],
1065 const SkColor colors[]) {
1066 ASSERT_SINGLE_OWNER
1067 RETURN_IF_ABANDONED
1068 SkDEBUGCODE(this->validate();)
1069 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1070
1071 AutoCheckFlush acf(this->drawingManager());
1072
1073 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1074 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1075 aaType, spriteCount, xform, texRect, colors);
1076 this->addDrawOp(clip, std::move(op));
1077}
1078
1079///////////////////////////////////////////////////////////////////////////////
1080
Michael Ludwig7c12e282020-05-29 09:54:07 -04001081void GrRenderTargetContext::drawRRect(const GrClip* origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001082 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001083 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001084 const SkMatrix& viewMatrix,
1085 const SkRRect& rrect,
1086 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001087 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001088 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001089 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001090 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001091
1092 const SkStrokeRec& stroke = style.strokeRec();
1093 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001094 return;
1095 }
1096
Michael Ludwig7c12e282020-05-29 09:54:07 -04001097 const GrClip* clip = origClip;
bsalomon7f0d9f32016-08-15 14:49:10 -07001098#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1099 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001100 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001101 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1102 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001103 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1104 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001105 SkRRect devRRect;
Michael Ludwig7c12e282020-05-29 09:54:07 -04001106 if (clip && stroke.getStyle() == SkStrokeRec::kFill_Style &&
1107 rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1108 clip = nullptr;
bsalomon7f0d9f32016-08-15 14:49:10 -07001109 }
1110#endif
bsalomon6663acf2016-05-10 09:14:17 -07001111 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001112
Robert Phillips72152832017-01-25 17:31:35 -05001113 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001114
Chris Dalton7d6748e2019-03-13 00:34:52 -06001115 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001116
Chris Dalton0dffbab2019-03-27 13:08:50 -06001117 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001118 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1119 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1120 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1121 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1122 // to avoid perf regressions on some platforms.
1123 assert_alive(paint);
1124 op = GrOvalOpFactory::MakeCircularRRectOp(
1125 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1126 }
1127 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001128 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001129 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001130 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001131 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001132 assert_alive(paint);
1133 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001134 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001135 }
1136 if (op) {
Michael Ludwig7c12e282020-05-29 09:54:07 -04001137 this->addDrawOp(clip, std::move(op));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001138 return;
robertphillipsea461502015-05-26 11:38:03 -07001139 }
robertphillipsb56f9272016-02-25 11:03:52 -08001140
Mike Klein16885072018-12-11 09:54:31 -05001141 assert_alive(paint);
Michael Ludwig7c12e282020-05-29 09:54:07 -04001142 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001143 GrStyledShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001144}
1145
Jim Van Verthc5903412016-11-17 15:27:09 -05001146///////////////////////////////////////////////////////////////////////////////
1147
Jim Van Verth3af1af92017-05-18 15:06:54 -04001148static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1149 SkPoint3 result;
1150 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1151 result.fZ = pt.fZ;
1152 return result;
1153}
1154
Michael Ludwig7c12e282020-05-29 09:54:07 -04001155bool GrRenderTargetContext::drawFastShadow(const GrClip* clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001156 const SkMatrix& viewMatrix,
1157 const SkPath& path,
1158 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001159 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001160 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001161 return true;
1162 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001163 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001164 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001165
1166 // check z plane
1167 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1168 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1169 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1170 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1171 return false;
1172 }
1173
1174 SkRRect rrect;
1175 SkRect rect;
1176 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001177 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001178 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1179 if (!isRRect &&
1180 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1181 rect.width() > SK_ScalarNearlyZero) {
1182 rrect.setOval(rect);
1183 isRRect = true;
1184 }
1185 if (!isRRect && path.isRect(&rect)) {
1186 rrect.setRect(rect);
1187 isRRect = true;
1188 }
1189
1190 if (!isRRect) {
1191 return false;
1192 }
1193
Jim Van Verthc5903412016-11-17 15:27:09 -05001194 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001195 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001196 }
1197
Robert Phillips72152832017-01-25 17:31:35 -05001198 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001199
Jim Van Verth3af1af92017-05-18 15:06:54 -04001200 // transform light
1201 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1202
1203 // 1/scale
1204 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001205 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001206 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1207 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1208
1209 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001210 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1211
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001212 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001213 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1214 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1215 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001216
1217 // Outset the shadow rrect to the border of the penumbra
1218 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1219 SkRRect ambientRRect;
1220 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1221 // If the rrect was an oval then its outset will also be one.
1222 // We set it explicitly to avoid errors.
1223 if (rrect.isOval()) {
1224 ambientRRect = SkRRect::MakeOval(outsetRect);
1225 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001226 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001227 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1228 }
1229
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001230 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001231 if (transparent) {
1232 // set a large inset to force a fill
1233 devSpaceInsetWidth = ambientRRect.width();
1234 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001235
Robert Phillips7c525e62018-06-12 10:11:12 -04001236 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1237 ambientColor,
1238 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001239 ambientRRect,
1240 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001241 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001242 if (op) {
1243 this->addDrawOp(clip, std::move(op));
1244 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001245 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001246
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001247 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001248 SkScalar devSpaceSpotBlur;
1249 SkScalar spotScale;
1250 SkVector spotOffset;
1251 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1252 devLightPos.fZ, rec.fLightRadius,
1253 &devSpaceSpotBlur, &spotScale, &spotOffset);
1254 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001255 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1256
Jim Van Verth3af1af92017-05-18 15:06:54 -04001257 // Adjust translate for the effect of the scale.
1258 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1259 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1260 // This offset is in dev space, need to transform it into source space.
1261 SkMatrix ctmInverse;
1262 if (viewMatrix.invert(&ctmInverse)) {
1263 ctmInverse.mapPoints(&spotOffset, 1);
1264 } else {
1265 // Since the matrix is a similarity, this should never happen, but just in case...
1266 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1267 SkASSERT(false);
1268 }
1269
1270 // Compute the transformed shadow rrect
1271 SkRRect spotShadowRRect;
1272 SkMatrix shadowTransform;
1273 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1274 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001275 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001276
1277 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001278 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001279 SkScalar insetWidth = blurOutset;
1280 if (transparent) {
1281 // If transparent, just do a fill
1282 insetWidth += spotShadowRRect.width();
1283 } else {
1284 // For shadows, instead of using a stroke we specify an inset from the penumbra
1285 // border. We want to extend this inset area so that it meets up with the caster
1286 // geometry. The inset geometry will by default already be inset by the blur width.
1287 //
1288 // We compare the min and max corners inset by the radius between the original
1289 // rrect and the shadow rrect. The distance between the two plus the difference
1290 // between the scaled radius and the original radius gives the distance from the
1291 // transformed shadow shape to the original shape in that corner. The max
1292 // of these gives the maximum distance we need to cover.
1293 //
1294 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1295 // that to get the full insetWidth.
1296 SkScalar maxOffset;
1297 if (rrect.isRect()) {
1298 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001299 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001300 rrect.rect().fLeft),
1301 SkTAbs(spotShadowRRect.rect().fTop -
1302 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001303 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001304 rrect.rect().fRight),
1305 SkTAbs(spotShadowRRect.rect().fBottom -
1306 rrect.rect().fBottom)));
1307 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001308 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001309 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1310 rrect.rect().fLeft + dr,
1311 spotShadowRRect.rect().fTop -
1312 rrect.rect().fTop + dr);
1313 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1314 rrect.rect().fRight - dr,
1315 spotShadowRRect.rect().fBottom -
1316 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001317 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001318 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001319 }
Brian Osman788b9162020-02-07 10:36:46 -05001320 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001321 }
1322
1323 // Outset the shadow rrect to the border of the penumbra
1324 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1325 if (spotShadowRRect.isOval()) {
1326 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1327 } else {
1328 SkScalar outsetRad = spotRadius + blurOutset;
1329 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1330 }
1331
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001332 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001333
Robert Phillips7c525e62018-06-12 10:11:12 -04001334 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1335 spotColor,
1336 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001337 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001338 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001339 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001340 if (op) {
1341 this->addDrawOp(clip, std::move(op));
1342 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001343 }
1344
1345 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001346}
1347
1348///////////////////////////////////////////////////////////////////////////////
1349
Michael Ludwig7c12e282020-05-29 09:54:07 -04001350bool GrRenderTargetContext::drawFilledDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001351 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001352 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001353 const SkMatrix& viewMatrix,
1354 const SkRRect& origOuter,
1355 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001356 SkASSERT(!origInner.isEmpty());
1357 SkASSERT(!origOuter.isEmpty());
1358
Brian Salomon65749212017-12-01 16:01:47 -05001359 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1360
Chris Dalton7d6748e2019-03-13 00:34:52 -06001361 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001362
1363 if (GrAAType::kMSAA == aaType) {
1364 return false;
1365 }
1366
Greg Daniel2655ede2019-04-10 00:49:28 +00001367 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1368 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001369 auto outerR = outer->width() / 2.f;
1370 auto innerR = inner->width() / 2.f;
1371 auto cx = outer->getBounds().fLeft + outerR;
1372 auto cy = outer->getBounds().fTop + outerR;
1373 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1374 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1375 auto avgR = (innerR + outerR) / 2.f;
1376 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1377 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1378 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001379 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001380 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001381 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001382 if (op) {
1383 this->addDrawOp(clip, std::move(op));
1384 return true;
1385 }
Mike Klein16885072018-12-11 09:54:31 -05001386 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001387 }
1388 }
1389
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001390 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001391 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001392 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1393 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001394 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001395 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1396 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001397 }
robertphillips00095892016-02-29 13:50:40 -08001398
robertphillips00095892016-02-29 13:50:40 -08001399 SkMatrix inverseVM;
1400 if (!viewMatrix.isIdentity()) {
1401 if (!origInner.transform(viewMatrix, inner.writable())) {
1402 return false;
1403 }
1404 if (!origOuter.transform(viewMatrix, outer.writable())) {
1405 return false;
1406 }
1407 if (!viewMatrix.invert(&inverseVM)) {
1408 return false;
1409 }
1410 } else {
1411 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001412 }
robertphillips00095892016-02-29 13:50:40 -08001413
Ethan Nicholaseace9352018-10-15 20:09:54 +00001414 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001415 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001416 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001417 if (!innerEffect) {
1418 return false;
1419 }
1420
Ethan Nicholaseace9352018-10-15 20:09:54 +00001421 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001422 if (!outerEffect) {
1423 return false;
1424 }
1425
Brian Salomon82f44312017-01-11 13:42:54 -05001426 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1427 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001428
1429 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001430 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001431 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1432 }
halcanary9d524f22016-03-29 09:03:52 -07001433
Brian Salomon82f44312017-01-11 13:42:54 -05001434 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1435 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001436 return true;
1437}
1438
Michael Ludwig7c12e282020-05-29 09:54:07 -04001439void GrRenderTargetContext::drawDRRect(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001440 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001441 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001442 const SkMatrix& viewMatrix,
1443 const SkRRect& outer,
1444 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001445 ASSERT_SINGLE_OWNER
1446 RETURN_IF_ABANDONED
1447 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001448 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001449
1450 SkASSERT(!outer.isEmpty());
1451 SkASSERT(!inner.isEmpty());
1452
Robert Phillips72152832017-01-25 17:31:35 -05001453 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001454
Brian Salomon82f44312017-01-11 13:42:54 -05001455 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001456 return;
1457 }
Mike Klein16885072018-12-11 09:54:31 -05001458 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001459
1460 SkPath path;
1461 path.setIsVolatile(true);
1462 path.addRRect(inner);
1463 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001464 path.setFillType(SkPathFillType::kEvenOdd);
Michael Ludwig2686d692020-04-17 20:21:37 +00001465 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrStyledShape(path));
robertphillips00095892016-02-29 13:50:40 -08001466}
1467
robertphillipsea461502015-05-26 11:38:03 -07001468///////////////////////////////////////////////////////////////////////////////
1469
Michael Ludwig7c12e282020-05-29 09:54:07 -04001470void GrRenderTargetContext::drawRegion(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001471 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001472 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001473 const SkMatrix& viewMatrix,
1474 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001475 const GrStyle& style,
1476 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001477 ASSERT_SINGLE_OWNER
1478 RETURN_IF_ABANDONED
1479 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001480 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001481
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001482 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001483 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001484 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001485 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001486 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1487 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001488 aa = GrAA::kNo;
1489 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001490 }
msarettcc319b92016-08-25 18:07:18 -07001491 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001492 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001493 SkPath path;
1494 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001495 path.setIsVolatile(true);
1496
Brian Salomon82f44312017-01-11 13:42:54 -05001497 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001498 }
1499
Chris Dalton7d6748e2019-03-13 00:34:52 -06001500 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001501 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1502 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001503 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001504}
1505
Michael Ludwig7c12e282020-05-29 09:54:07 -04001506void GrRenderTargetContext::drawOval(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001507 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001508 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001509 const SkMatrix& viewMatrix,
1510 const SkRect& oval,
1511 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001512 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001513 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001514 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001515 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001516
Robert Phillips7484d202018-07-03 09:09:08 -04001517 const SkStrokeRec& stroke = style.strokeRec();
1518
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001519 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001520 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1521 return;
1522 }
1523
1524 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001525 return;
robertphillipsea461502015-05-26 11:38:03 -07001526 }
1527
Robert Phillips72152832017-01-25 17:31:35 -05001528 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001529
Chris Dalton7d6748e2019-03-13 00:34:52 -06001530 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001531
1532 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001533 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1534 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001535 // We don't draw true circles as round rects in coverage mode, because it can
1536 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1537 assert_alive(paint);
1538 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1539 this->caps()->shaderCaps());
1540 }
1541 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001542 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1543 // the arc equation. This same special geometry and fragment branch also turn out to be a
1544 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1545 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1546 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001547 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001548 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1549 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001550 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001551 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001552 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001553 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1554 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001555 }
1556 if (op) {
1557 this->addDrawOp(clip, std::move(op));
1558 return;
robertphillipsea461502015-05-26 11:38:03 -07001559 }
robertphillipsb56f9272016-02-25 11:03:52 -08001560
Mike Klein16885072018-12-11 09:54:31 -05001561 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001562 this->drawShapeUsingPathRenderer(
1563 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001564 GrStyledShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001565}
1566
Michael Ludwig7c12e282020-05-29 09:54:07 -04001567void GrRenderTargetContext::drawArc(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001568 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001569 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001570 const SkMatrix& viewMatrix,
1571 const SkRect& oval,
1572 SkScalar startAngle,
1573 SkScalar sweepAngle,
1574 bool useCenter,
1575 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001576 ASSERT_SINGLE_OWNER
1577 RETURN_IF_ABANDONED
1578 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001579 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001580
1581 AutoCheckFlush acf(this->drawingManager());
1582
Chris Dalton7d6748e2019-03-13 00:34:52 -06001583 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001584 if (GrAAType::kCoverage == aaType) {
1585 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1586 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1587 std::move(paint),
1588 viewMatrix,
1589 oval,
1590 startAngle,
1591 sweepAngle,
1592 useCenter,
1593 style,
1594 shaderCaps);
1595 if (op) {
1596 this->addDrawOp(clip, std::move(op));
1597 return;
1598 }
1599 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001600 }
Brian Salomone4949402018-04-26 15:22:04 -04001601 this->drawShapeUsingPathRenderer(
1602 clip, std::move(paint), aa, viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00001603 GrStyledShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001604}
1605
Michael Ludwig7c12e282020-05-29 09:54:07 -04001606void GrRenderTargetContext::drawImageLattice(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001607 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001608 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001609 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001610 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001611 sk_sp<GrColorSpaceXform> csxf,
1612 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001613 std::unique_ptr<SkLatticeIter> iter,
1614 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001615 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001616 RETURN_IF_ABANDONED
1617 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001618 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001619
Robert Phillips72152832017-01-25 17:31:35 -05001620 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001621
Brian Salomon2a943df2018-05-04 13:43:19 -04001622 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001623 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001624 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001625 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001626}
1627
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001628void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1629 const SkRect& bounds) {
1630 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1631 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001632 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001633}
1634
Brian Salomon031b0ba2019-05-23 11:05:26 -04001635void GrRenderTargetContext::asyncRescaleAndReadPixels(
1636 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1637 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1638 auto direct = fContext->priv().asDirectContext();
1639 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001640 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001641 return;
1642 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001643 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001644 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001645 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001646 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001647 if (this->asRenderTargetProxy()->framebufferOnly()) {
1648 callback(context, nullptr);
1649 return;
1650 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001651 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001652 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001653 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001654 return;
1655 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001656 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001657 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001658 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001659 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001660 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001661 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1662 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001663 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001664 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001665 backendFormatOfFinalContext, dstCT);
1666 // Fail if we can't read from the source surface's color type.
1667 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001668 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001669 return;
1670 }
Brian Salomon624f9062019-07-02 14:23:00 -04001671 // Fail if read color type does not have all of dstCT's color channels and those missing color
1672 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001673 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1674 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1675 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1676 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001677 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001678 return;
1679 }
1680
Brian Salomonbf6b9792019-08-21 09:38:10 -04001681 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001682 int x = srcRect.fLeft;
1683 int y = srcRect.fTop;
1684 if (needsRescale) {
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001685 tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1686 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001687 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001688 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001689 return;
1690 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001691 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001692 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001693 x = y = 0;
1694 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001695 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1696 this->colorInfo().alphaType(),
1697 info.colorSpace(),
1698 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001699 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1700 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001701 // We flip or color convert by drawing and we don't currently support drawing to
1702 // kPremul.
1703 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001704 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001705 return;
1706 }
Greg Daniel40903af2020-01-30 14:55:05 -05001707 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 SkRect srcRectToDraw = SkRect::Make(srcRect);
1709 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001710 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001711 texProxyView =
1712 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1713 SkBackingFit::kApprox, SkBudgeted::kNo);
1714 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001715 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001716 return;
1717 }
Brian Salomonc5243782020-04-02 12:50:34 -04001718 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001719 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1720 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001721 tempRTC = GrRenderTargetContext::Make(
1722 direct, this->colorInfo().colorType(), info.refColorSpace(),
1723 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001724 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001725 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001726 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001727 return;
1728 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001729 tempRTC->drawTexture(nullptr, std::move(texProxyView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001730 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1731 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001732 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1733 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1734 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001735 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001736 }
1737 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001738 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001739 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1740 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001741}
1742
Brian Salomon9241a6d2019-10-03 13:26:54 -04001743class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1744public:
1745 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1746 ~AsyncReadResult() override {
1747 for (int i = 0; i < fPlanes.count(); ++i) {
1748 if (!fPlanes[i].fMappedBuffer) {
1749 delete[] static_cast<const char*>(fPlanes[i].fData);
1750 } else {
1751 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1752 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1753 }
1754 }
1755 }
1756
1757 int count() const override { return fPlanes.count(); }
1758 const void* data(int i) const override { return fPlanes[i].fData; }
1759 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1760
1761 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001762 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001763 size_t rowBytes,
1764 GrClientMappedBufferManager* manager) {
1765 SkASSERT(!result.fTransferBuffer->isMapped());
1766 const void* mappedData = result.fTransferBuffer->map();
1767 if (!mappedData) {
1768 return false;
1769 }
1770 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001771 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001772 result.fPixelConverter(convertedData.get(), mappedData);
1773 this->addCpuPlane(std::move(convertedData), rowBytes);
1774 result.fTransferBuffer->unmap();
1775 } else {
1776 manager->insert(result.fTransferBuffer);
1777 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1778 }
1779 return true;
1780 }
1781
1782 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1783 SkASSERT(data);
1784 SkASSERT(rowBytes > 0);
1785 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1786 }
1787
1788private:
1789 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1790 SkASSERT(data);
1791 SkASSERT(rowBytes > 0);
1792 SkASSERT(mappedBuffer);
1793 SkASSERT(mappedBuffer->isMapped());
1794 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1795 }
1796
1797 struct Plane {
1798 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1799 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1800 const void* fData;
1801 size_t fRowBytes;
1802 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1803 sk_sp<GrGpuBuffer> fMappedBuffer;
1804 };
1805 SkSTArray<3, Plane> fPlanes;
1806 uint32_t fInboxID;
1807};
1808
Brian Salomon024bd002019-06-11 11:38:16 -04001809void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1810 ReadPixelsCallback callback,
1811 ReadPixelsContext context) {
1812 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1813 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1814
Greg Danielca9dbe22020-01-02 13:44:30 -05001815 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001816 callback(context, nullptr);
1817 return;
1818 }
1819
Brian Salomon9241a6d2019-10-03 13:26:54 -04001820 auto directContext = fContext->priv().asDirectContext();
1821 SkASSERT(directContext);
1822 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1823
Brian Salomon024bd002019-06-11 11:38:16 -04001824 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1825
1826 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001827 auto ii = SkImageInfo::Make(rect.size(), colorType,
1828 this->colorInfo().alphaType(),
1829 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001830 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001831 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1832 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1833 result->addCpuPlane(std::move(data), pm.rowBytes());
1834
Brian Salomon1d435302019-07-01 13:05:28 -04001835 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001836 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001837 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001838 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001839 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001840 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001841 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001842
Brian Salomonab32f652019-05-10 14:24:50 -04001843 struct FinishContext {
1844 ReadPixelsCallback* fClientCallback;
1845 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001846 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001847 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001848 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001849 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001850 };
1851 // Assumption is that the caller would like to flush. We could take a parameter or require an
1852 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1853 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001854 auto* finishContext = new FinishContext{callback,
1855 context,
1856 rect.size(),
1857 colorType,
1858 mappedBufferManager,
1859 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001860 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001861 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001862 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001863 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1864 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1865 context->fMappedBufferManager)) {
1866 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001867 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001868 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001869 delete context;
1870 };
1871 GrFlushInfo flushInfo;
1872 flushInfo.fFinishedContext = finishContext;
1873 flushInfo.fFinishedProc = finishCallback;
1874 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1875}
1876
Brian Salomon9241a6d2019-10-03 13:26:54 -04001877void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1878 sk_sp<SkColorSpace> dstColorSpace,
1879 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001880 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001881 RescaleGamma rescaleGamma,
1882 SkFilterQuality rescaleQuality,
1883 ReadPixelsCallback callback,
1884 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001885 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1886 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001887 SkASSERT(!dstSize.isZero());
1888 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1889
Brian Salomon024bd002019-06-11 11:38:16 -04001890 auto direct = fContext->priv().asDirectContext();
1891 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001892 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001893 return;
1894 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001895 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001896 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001897 return;
1898 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001899 if (this->asRenderTargetProxy()->framebufferOnly()) {
1900 callback(context, nullptr);
1901 return;
1902 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001903 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001904 callback(context, nullptr);
1905 return;
1906 }
Brian Salomon024bd002019-06-11 11:38:16 -04001907 int x = srcRect.fLeft;
1908 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001909 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001910 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001911 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001912 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001913 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001914 dstColorSpace);
1915 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomon11ad4cc2020-05-15 12:07:59 -04001916 auto tempRTC = this->rescale(info, kTopLeft_GrSurfaceOrigin, srcRect, rescaleGamma,
1917 rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001918 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001919 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001920 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001921 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001922 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001923 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001924 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001925 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001926 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001927 srcView = this->readSurfaceView();
1928 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001929 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1930 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1931 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001932 // If we can't get a texture copy of the contents then give up.
1933 callback(context, nullptr);
1934 return;
1935 }
Brian Salomonc5243782020-04-02 12:50:34 -04001936 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001937 x = y = 0;
1938 }
Brian Salomon4d036892019-07-02 15:10:58 -04001939 // We assume the caller wants kPremul. There is no way to indicate a preference.
1940 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001941 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1942 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001943 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001944 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1945 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001946 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1947 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001948 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001949 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001950 return;
1951 }
Michael Ludwig7c12e282020-05-29 09:54:07 -04001952 tempRTC->drawTexture(nullptr, std::move(srcView), this->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -05001953 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1954 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
1955 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1956 SkMatrix::I(), std::move(xform));
1957 srcView = tempRTC->readSurfaceView();
1958 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04001959 x = y = 0;
1960 }
1961 }
Greg Danielc594e622019-10-15 14:01:49 -04001962
Greg Daniele20fcad2020-01-08 11:52:34 -05001963 auto yRTC = GrRenderTargetContext::MakeWithFallback(
1964 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
1965 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001966 int halfW = dstSize.width()/2;
1967 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05001968 auto uRTC = GrRenderTargetContext::MakeWithFallback(
1969 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1970 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
1971 auto vRTC = GrRenderTargetContext::MakeWithFallback(
1972 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
1973 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001974 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001975 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001976 return;
1977 }
1978
Brian Osman28ba5282019-10-30 14:18:07 -04001979 float baseM[20];
1980 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1981
Brian Salomon024bd002019-06-11 11:38:16 -04001982 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1983
Mike Reed1f607332020-05-21 12:11:27 -04001984 auto texMatrix = SkMatrix::Translate(x, y);
Brian Salomon024bd002019-06-11 11:38:16 -04001985
Brian Salomon9241a6d2019-10-03 13:26:54 -04001986 SkRect dstRectY = SkRect::Make(dstSize);
1987 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00001988
Brian Salomon6aa65052020-01-28 12:16:53 -05001989 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
1990 PixelTransferResult yTransfer, uTransfer, vTransfer;
1991
Brian Salomon024bd002019-06-11 11:38:16 -04001992 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1993 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001994 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001995 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001996 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05001997 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05001998 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04001999 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2000 yPaint.addColorFragmentProcessor(std::move(yFP));
2001 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002002 yRTC->fillRectToRect(nullptr, std::move(yPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002003 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002004 if (!doSynchronousRead) {
2005 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2006 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2007 if (!yTransfer.fTransferBuffer) {
2008 callback(context, nullptr);
2009 return;
2010 }
Brian Salomon024bd002019-06-11 11:38:16 -04002011 }
2012
2013 texMatrix.preScale(2.f, 2.f);
2014 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2015 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002016 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002017 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002018 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002019 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002020 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002021 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2022 uPaint.addColorFragmentProcessor(std::move(uFP));
2023 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002024 uRTC->fillRectToRect(nullptr, std::move(uPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002025 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002026 if (!doSynchronousRead) {
2027 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2028 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2029 if (!uTransfer.fTransferBuffer) {
2030 callback(context, nullptr);
2031 return;
2032 }
Brian Salomon024bd002019-06-11 11:38:16 -04002033 }
2034
2035 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2036 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002037 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002038 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002039 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002040 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2041 this->colorInfo().alphaType(), texMatrix,
2042 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002043 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2044 vPaint.addColorFragmentProcessor(std::move(vFP));
2045 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig7c12e282020-05-29 09:54:07 -04002046 vRTC->fillRectToRect(nullptr, std::move(vPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwig61328202019-06-19 14:48:58 +00002047 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002048 if (!doSynchronousRead) {
2049 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2050 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2051 if (!vTransfer.fTransferBuffer) {
2052 callback(context, nullptr);
2053 return;
2054 }
2055 }
2056
2057 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002058 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2059 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2060 size_t yRB = yInfo.minRowBytes();
2061 size_t uvRB = uvInfo.minRowBytes();
2062 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2063 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2064 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2065 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2066 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2067 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002068 callback(context, nullptr);
2069 return;
2070 }
2071 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002072 result->addCpuPlane(std::move(y), yRB );
2073 result->addCpuPlane(std::move(u), uvRB);
2074 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002075 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002076 return;
2077 }
2078
2079 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002080 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002081 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002082 GrClientMappedBufferManager* fMappedBufferManager;
2083 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002084 PixelTransferResult fYTransfer;
2085 PixelTransferResult fUTransfer;
2086 PixelTransferResult fVTransfer;
2087 };
2088 // Assumption is that the caller would like to flush. We could take a parameter or require an
2089 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2090 // callback to GrGpu until after the next flush that flushes our op list, though.
2091 auto* finishContext = new FinishContext{callback,
2092 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002093 direct->priv().clientMappedBufferManager(),
2094 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002095 std::move(yTransfer),
2096 std::move(uTransfer),
2097 std::move(vTransfer)};
2098 auto finishCallback = [](GrGpuFinishedContext c) {
2099 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002100 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002101 auto manager = context->fMappedBufferManager;
2102 size_t rowBytes = SkToSizeT(context->fSize.width());
2103 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2104 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002105 delete context;
2106 return;
2107 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002108 rowBytes /= 2;
2109 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2110 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2111 (*context->fClientCallback)(context->fClientContext, nullptr);
2112 delete context;
2113 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002114 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002115 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2116 (*context->fClientCallback)(context->fClientContext, nullptr);
2117 delete context;
2118 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002119 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002120 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002121 delete context;
2122 };
2123 GrFlushInfo flushInfo;
2124 flushInfo.fFinishedContext = finishContext;
2125 flushInfo.fFinishedProc = finishCallback;
2126 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002127}
2128
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002129GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2130 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002131 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002132 if (fContext->priv().abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -04002133 if (info.fSubmittedProc) {
2134 info.fSubmittedProc(info.fSubmittedContext, false);
2135 }
2136 if (info.fFinishedProc) {
2137 info.fFinishedProc(info.fFinishedContext);
2138 }
Robert Phillipsa9162df2019-02-11 14:12:03 -05002139 return GrSemaphoresSubmitted::kNo;
2140 }
robertphillips8c523e02016-07-26 07:41:00 -07002141 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002142 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002143
Greg Daniel55f040b2020-02-13 15:38:32 +00002144 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002145}
2146
Greg Danielc64ee462017-06-15 16:59:49 -04002147bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002148 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002149 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002150 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002151 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002152 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002153
2154 AutoCheckFlush acf(this->drawingManager());
2155
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002156 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002157 return false;
2158 }
2159
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002160 auto direct = fContext->priv().asDirectContext();
2161 if (!direct) {
2162 return false;
2163 }
2164
2165 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002166
Greg Daniel301015c2019-11-18 14:06:46 -05002167 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2168 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002169 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002170 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002171 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2172 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002173 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002174 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2175 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002176 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002177}
joshualitt33a5fce2015-11-18 13:28:51 -08002178
Michael Ludwig7c12e282020-05-29 09:54:07 -04002179void GrRenderTargetContext::drawPath(const GrClip* clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002180 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002181 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002182 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002183 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002184 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002185 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002186 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002187 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002188 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2189
Michael Ludwig2686d692020-04-17 20:21:37 +00002190 GrStyledShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002191
Robert Phillips27927a52018-08-20 13:18:12 -04002192 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002193}
2194
Michael Ludwig7c12e282020-05-29 09:54:07 -04002195void GrRenderTargetContext::drawShape(const GrClip* clip,
Robert Phillips20390c32018-08-17 11:01:03 -04002196 GrPaint&& paint,
2197 GrAA aa,
2198 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +00002199 const GrStyledShape& shape) {
Robert Phillips20390c32018-08-17 11:01:03 -04002200 ASSERT_SINGLE_OWNER
2201 RETURN_IF_ABANDONED
2202 SkDEBUGCODE(this->validate();)
2203 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2204
Brian Salomon2fad74a2017-12-20 13:28:55 -05002205 if (shape.isEmpty()) {
2206 if (shape.inverseFilled()) {
2207 this->drawPaint(clip, std::move(paint), viewMatrix);
2208 }
2209 return;
robertphillipsea461502015-05-26 11:38:03 -07002210 }
2211
Robert Phillips72152832017-01-25 17:31:35 -05002212 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002213
Brian Salomon2fad74a2017-12-20 13:28:55 -05002214 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002215 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002216 SkRRect rrect;
2217 // We can ignore the starting point and direction since there is no path effect.
2218 bool inverted;
2219 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2220 if (rrect.isRect()) {
2221 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2222 &shape.style());
2223 return;
2224 } else if (rrect.isOval()) {
2225 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002226 return;
2227 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002228 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2229 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002230 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2231 viewMatrix.rectStaysRect()) {
2232 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2233 // the matrix to all the points individually rather than just to the rect
2234 SkRect rects[2];
2235 if (shape.asNestedRects(rects)) {
2236 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002237 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002238 fContext, std::move(paint), viewMatrix, rects);
2239 if (op) {
2240 this->addDrawOp(clip, std::move(op));
2241 }
2242 // Returning here indicates that there is nothing to draw in this case.
2243 return;
2244 }
robertphillipsea461502015-05-26 11:38:03 -07002245 }
2246 }
robertphillips4bc31812016-03-01 12:22:49 -08002247
Michael Ludwig6a6de652020-04-30 20:16:36 -04002248 // If we get here in drawShape(), we definitely need to use path rendering
2249 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape,
2250 /* attempt fallback */ false);
robertphillipsea461502015-05-26 11:38:03 -07002251}
2252
Michael Ludwig7c12e282020-05-29 09:54:07 -04002253bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip* clip,
Brian Osman11052242016-10-27 14:47:55 -04002254 const GrUserStencilSettings* ss,
2255 SkRegion::Op op,
2256 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002257 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002258 const SkMatrix& viewMatrix,
2259 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002260 ASSERT_SINGLE_OWNER_PRIV
2261 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002262 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002263 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2264 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002265
2266 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002267 GrPaint paint;
2268 paint.setCoverageSetOpXPFactory(op, invert);
2269 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2270 SkRect::MakeIWH(fRenderTargetContext->width(),
2271 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002272 return true;
2273 }
2274
Robert Phillips72152832017-01-25 17:31:35 -05002275 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002276
2277 // An Assumption here is that path renderer would use some form of tweaking
2278 // the src color (either the input alpha or in the frag shader) to implement
2279 // aa. If we have some future driver-mojo path AA that can do the right
2280 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002281 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002282 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002283
Michael Ludwig7c12e282020-05-29 09:54:07 -04002284 SkIRect clipConservativeBounds = get_clip_bounds(fRenderTargetContext, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002285
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002286 GrPaint paint;
2287 paint.setCoverageSetOpXPFactory(op, invert);
2288
Michael Ludwig2686d692020-04-17 20:21:37 +00002289 GrStyledShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002290 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002291 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002292 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002293 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002294 canDrawArgs.fShape = &shape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002295 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002296 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002297 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002298 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2299 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002300 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002301
2302 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002303 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002304 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002305 if (!pr) {
2306 return false;
2307 }
2308
Brian Salomonf3569f02017-10-24 12:52:33 -04002309 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2310 std::move(paint),
2311 ss,
2312 fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002313 clip,
Brian Salomonf3569f02017-10-24 12:52:33 -04002314 &clipConservativeBounds,
2315 &viewMatrix,
2316 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002317 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002318 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002319 pr->drawPath(args);
2320 return true;
2321}
2322
Brian Osman11052242016-10-27 14:47:55 -04002323SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002324 ASSERT_SINGLE_OWNER_PRIV
2325
Robert Phillips6a6de562019-02-15 15:19:15 -05002326 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002327 return SkBudgeted::kNo;
2328 }
2329
Brian Osman11052242016-10-27 14:47:55 -04002330 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002331
Greg Daniel3912a4b2020-01-14 09:56:04 -05002332 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002333}
2334
Michael Ludwig7c12e282020-05-29 09:54:07 -04002335void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip* clip,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002336 GrPaint&& paint,
2337 GrAA aa,
2338 const SkMatrix& viewMatrix,
Michael Ludwig6a6de652020-04-30 20:16:36 -04002339 const GrStyledShape& originalShape,
2340 bool attemptShapeFallback) {
joshualitt1de610a2016-01-06 08:26:09 -08002341 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002342 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002343 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2344
Jim Van Verthf86073a2018-10-02 13:05:38 -04002345 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2346 return;
2347 }
2348
Michael Ludwig6a6de652020-04-30 20:16:36 -04002349 if (attemptShapeFallback && originalShape.simplified()) {
2350 // Usually we enter drawShapeUsingPathRenderer() because the shape+style was too
2351 // complex for dedicated draw ops. However, if GrStyledShape was able to reduce something
2352 // we ought to try again instead of going right to path rendering.
2353 this->drawShape(clip, std::move(paint), aa, viewMatrix, originalShape);
2354 return;
2355 }
2356
Michael Ludwig7c12e282020-05-29 09:54:07 -04002357 SkIRect clipConservativeBounds = get_clip_bounds(this, clip);
Chris Daltondb91c6e2017-09-08 16:25:08 -06002358
Michael Ludwig2686d692020-04-17 20:21:37 +00002359 GrStyledShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002360 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002361
robertphillips68737822015-10-29 12:12:21 -07002362 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002363 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002364 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002365 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002366 canDrawArgs.fShape = &originalShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -04002367 canDrawArgs.fPaint = &paint;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002368 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002369 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002370 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002371
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002372 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002373 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002374 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002375 return;
2376 }
2377
Chris Dalton6ce447a2019-06-23 18:07:38 -06002378 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002379
2380 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2381 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2382 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2383
Brian Salomon2fad74a2017-12-20 13:28:55 -05002384 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002385 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002386 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2387 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002388 return;
2389 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002390 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002391 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002392 }
2393 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002394 if (canDrawArgs.fShape->style().applies()) {
2395 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2396 styleScale);
2397 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002398 return;
2399 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002400 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002401 // This time, allow SW renderer
2402 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2403 } else {
2404 pr = this->drawingManager()->getSoftwarePathRenderer();
Robert Phillipsd81379d2020-04-21 10:39:02 -04002405#if GR_PATH_RENDERER_SPEW
2406 SkDebugf("falling back to: %s\n", pr->name());
2407#endif
bsalomon6663acf2016-05-10 09:14:17 -07002408 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002409 }
robertphillipsea461502015-05-26 11:38:03 -07002410
bsalomon8acedde2016-06-24 10:42:16 -07002411 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002412#ifdef SK_DEBUG
2413 SkDebugf("Unable to find path renderer compatible with path.\n");
2414#endif
2415 return;
2416 }
2417
Robert Phillips256c37b2017-03-01 14:32:46 -05002418 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002419 std::move(paint),
2420 &GrUserStencilSettings::kUnused,
2421 this,
Michael Ludwig7c12e282020-05-29 09:54:07 -04002422 clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002423 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002424 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002425 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002426 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002427 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002428 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002429}
2430
Brian Salomon467921e2017-03-06 16:17:12 -05002431static void op_bounds(SkRect* bounds, const GrOp* op) {
2432 *bounds = op->bounds();
2433 if (op->hasZeroArea()) {
2434 if (op->hasAABloat()) {
2435 bounds->outset(0.5f, 0.5f);
2436 } else {
2437 // We don't know which way the particular GPU will snap lines or points at integer
2438 // coords. So we ensure that the bounds is large enough for either snap.
2439 SkRect before = *bounds;
2440 bounds->roundOut(bounds);
2441 if (bounds->fLeft == before.fLeft) {
2442 bounds->fLeft -= 1;
2443 }
2444 if (bounds->fTop == before.fTop) {
2445 bounds->fTop -= 1;
2446 }
2447 if (bounds->fRight == before.fRight) {
2448 bounds->fRight += 1;
2449 }
2450 if (bounds->fBottom == before.fBottom) {
2451 bounds->fBottom += 1;
2452 }
2453 }
2454 }
2455}
2456
Chris Dalton08755122019-08-05 16:13:47 -06002457void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002458 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002459 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2460}
2461
Michael Ludwig7c12e282020-05-29 09:54:07 -04002462void GrRenderTargetContext::addDrawOp(const GrClip* clip, std::unique_ptr<GrDrawOp> op,
Brian Salomon348a0372018-10-31 10:42:18 -04002463 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002464 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002465 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002466 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002467 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002468 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002469 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002470 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002471 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002472
Brian Salomon467921e2017-03-06 16:17:12 -05002473 // Setup clip
2474 SkRect bounds;
2475 op_bounds(&bounds, op.get());
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002476 GrAppliedClip appliedClip(this->dimensions(), this->asSurfaceProxy()->backingStoreDimensions());
Brian Salomon54d212e2017-03-21 14:22:38 -04002477 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002478 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002479 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002480
Chris Dalton9acf6822019-11-12 11:52:40 -07002481 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002482 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002483 }
2484
Michael Ludwig7c12e282020-05-29 09:54:07 -04002485 bool skipDraw = false;
2486 if (clip) {
2487 // Have a complex clip, so defer to its early clip culling
2488 if (!clip->apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
2489 skipDraw = true;
2490 }
2491 } else {
2492 // No clipping, so just clip the bounds against the logical render target dimensions
2493 if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
2494 skipDraw = true;
2495 }
2496 }
2497
2498 if (skipDraw) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002499 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002500 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002501 }
2502
Chris Dalton9acf6822019-11-12 11:52:40 -07002503 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2504 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2505
2506 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2507 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2508 // that uses a stencil test when the stencil buffer is multisampled.)
2509 bool hasMixedSampledCoverage = (
2510 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002511 SkASSERT(!hasMixedSampledCoverage ||
2512 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002513
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002514 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002515 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002516 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2517
Greg Daniel524e28b2019-11-01 11:48:53 -04002518 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002519 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002520 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002521 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002522 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002523 }
2524 }
2525
2526 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002527 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002528 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002529 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002530 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002531 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002532 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002533}
2534
Michael Ludwig7c12e282020-05-29 09:54:07 -04002535bool GrRenderTargetContext::setupDstProxyView(const GrClip* clip, const GrOp& op,
Greg Daniel524e28b2019-11-01 11:48:53 -04002536 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002537 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2538 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2539 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002540 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002541 return false;
2542 }
2543
Greg Daniel3912a4b2020-01-14 09:56:04 -05002544 if (this->caps()->textureBarrierSupport() &&
2545 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002546 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002547 // The render target is a texture, so we can read from it directly in the shader. The XP
2548 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002549 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002550 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002551 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002552 }
2553 }
2554
Greg Daniel3912a4b2020-01-14 09:56:04 -05002555 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002556
Michael Ludwig7c12e282020-05-29 09:54:07 -04002557 SkIRect clippedRect = get_clip_bounds(this, clip);
Brian Salomon09181ef2018-11-14 13:39:51 -05002558 SkRect opBounds = op.bounds();
2559 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2560 // 0.5 pixels.
2561 if (op.hasAABloat() || op.hasZeroArea()) {
2562 opBounds.outset(0.5f, 0.5f);
2563 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2564 // performance we may ignore the clip when the draw is entirely inside the clip is float
2565 // space but will hit pixels just outside the clip when actually rasterizing.
2566 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002567 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002568 }
2569 SkIRect opIBounds;
2570 opBounds.roundOut(&opIBounds);
2571 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002572#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002573 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002574#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002575 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002576 }
2577
Greg Daniel3155f7f2020-01-16 16:54:26 -05002578 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002579 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2580 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002581 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002582 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002583
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002584 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002585 copyRect = clippedRect;
2586 }
Brian Salomon467921e2017-03-06 16:17:12 -05002587
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002588 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002589 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002590 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002591 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002592 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002593 } else {
Eric Karl74480882017-04-03 14:49:05 -07002594 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002595 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002596 }
Brian Salomonc5243782020-04-02 12:50:34 -04002597 auto copy =
2598 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2599 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2600 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002601
Brian Salomonc5243782020-04-02 12:50:34 -04002602 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002603 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002604 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002605}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002606
Greg Daniel573312e2020-02-07 17:22:35 -05002607bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002608 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002609 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002610 SkIRect clippedSrcRect;
2611 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002612 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2613 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002614 return false;
2615 }
2616
2617 GrPaint paint;
2618 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002619
Greg Danield2ccbb52020-02-05 10:45:39 -05002620 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002621 if (!fp) {
2622 return false;
2623 }
2624 paint.addColorFragmentProcessor(std::move(fp));
2625
2626 this->fillRectToRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -04002627 nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
Greg Daniel46cfbc62019-06-07 11:43:30 -04002628 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2629 clippedSrcRect.height()),
2630 SkRect::Make(clippedSrcRect));
2631 return true;
2632}
Brian Salomon3b8486a2020-04-21 12:43:26 -04002633
2634void GrRenderTargetContext::wasClosed(const GrOpsTask& task) {
2635 SkASSERT(&task == fOpsTask.get());
2636 fOpsTask.reset();
2637}