blob: 79563e66901182e7b467936d6b07bd4ebf8f071a [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 Ludwig663afe52019-06-03 16:46:19 -040055#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "src/gpu/ops/GrAtlasTextOp.h"
57#include "src/gpu/ops/GrClearOp.h"
58#include "src/gpu/ops/GrClearStencilClipOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050059#include "src/gpu/ops/GrDrawAtlasOp.h"
60#include "src/gpu/ops/GrDrawOp.h"
61#include "src/gpu/ops/GrDrawVerticesOp.h"
62#include "src/gpu/ops/GrDrawableOp.h"
63#include "src/gpu/ops/GrFillRRectOp.h"
64#include "src/gpu/ops/GrFillRectOp.h"
65#include "src/gpu/ops/GrLatticeOp.h"
66#include "src/gpu/ops/GrOp.h"
67#include "src/gpu/ops/GrOvalOpFactory.h"
68#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050069#include "src/gpu/ops/GrShadowRRectOp.h"
70#include "src/gpu/ops/GrStencilPathOp.h"
71#include "src/gpu/ops/GrStrokeRectOp.h"
72#include "src/gpu/ops/GrTextureOp.h"
73#include "src/gpu/text/GrTextContext.h"
74#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040075
Herb Derbyc1b482c2018-08-09 15:02:27 -040076class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040077public:
78 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040079 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040081 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040082 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040083
Robert Phillips7c525e62018-06-12 10:11:12 -040084 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040085 fRenderTargetContext->addDrawOp(clip, std::move(op));
86 }
87
Robert Phillips46a13382018-08-23 13:53:01 -040088 void drawShape(const GrClip& clip, const SkPaint& paint,
89 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040090 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
91 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040092 }
93
94 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040095 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050096 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040097 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040098 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040099 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400100 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400101 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102 }
103 }
104
Robert Phillips69893702019-02-22 11:16:30 -0500105 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400106 return fRenderTargetContext->fContext;
107 }
108
Herb Derby65956872018-08-21 16:55:04 -0400109 SkGlyphRunListPainter* glyphPainter() override {
110 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400111 }
112
Brian Salomonf18b1d82017-10-27 11:30:49 -0400113private:
114 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400115 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400116
Brian Salomonf18b1d82017-10-27 11:30:49 -0400117};
joshualittbc907352016-01-13 06:45:40 -0800118
Robert Phillips72152832017-01-25 17:31:35 -0500119#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800120#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400121 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800122#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400123 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500124#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
125#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
126#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
127#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
128#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700129
Brian Salomone225b562017-06-14 13:00:03 -0400130//////////////////////////////////////////////////////////////////////////////
131
robertphillipsea461502015-05-26 11:38:03 -0700132class AutoCheckFlush {
133public:
halcanary9d524f22016-03-29 09:03:52 -0700134 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700135 SkASSERT(fDrawingManager);
136 }
bsalomonb77a9072016-09-07 10:02:04 -0700137 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700138
139private:
robertphillips77a2e522015-10-17 07:43:27 -0700140 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700141};
142
Greg Daniele20fcad2020-01-08 11:52:34 -0500143std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
144 GrRecordingContext* context,
145 GrColorType colorType,
146 sk_sp<SkColorSpace> colorSpace,
Greg Danielba0ff782020-01-07 15:42:57 -0500147 sk_sp<GrSurfaceProxy> proxy,
148 GrSurfaceOrigin origin,
149 const SkSurfaceProps* surfaceProps,
150 bool managedOps) {
151 if (!proxy) {
152 return nullptr;
153 }
154
155 const GrBackendFormat& format = proxy->backendFormat();
Brian Salomonc5243782020-04-02 12:50:34 -0400156 GrSwizzle readSwizzle, writeSwizzle;
157 if (colorType != GrColorType::kUnknown) {
158 readSwizzle = context->priv().caps()->getReadSwizzle(format, colorType);
159 writeSwizzle = context->priv().caps()->getWriteSwizzle(format, colorType);
160 }
Greg Danielba0ff782020-01-07 15:42:57 -0500161
Greg Daniel3912a4b2020-01-14 09:56:04 -0500162 GrSurfaceProxyView readView(proxy, origin, readSwizzle);
Brian Salomon8afde5f2020-04-01 16:22:00 -0400163 GrSurfaceProxyView writeView(std::move(proxy), origin, writeSwizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500164
165 return std::make_unique<GrRenderTargetContext>(context, std::move(readView),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400166 std::move(writeView), colorType,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500167 std::move(colorSpace), surfaceProps, managedOps);
Greg Danielba0ff782020-01-07 15:42:57 -0500168}
169
170std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
171 GrRecordingContext* context,
172 GrColorType colorType,
173 sk_sp<SkColorSpace> colorSpace,
Greg Daniele20fcad2020-01-08 11:52:34 -0500174 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500175 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500176 const GrBackendFormat& format,
177 int sampleCnt,
178 GrMipMapped mipMapped,
179 GrProtected isProtected,
180 GrSurfaceOrigin origin,
181 SkBudgeted budgeted,
182 const SkSurfaceProps* surfaceProps) {
183 // It is probably not necessary to check if the context is abandoned here since uses of the
184 // GrRenderTargetContext which need the context will mostly likely fail later on without an
185 // issue. However having this hear adds some reassurance in case there is a path doesn't handle
186 // an abandoned context correctly. It also lets us early out of some extra work.
187 if (context->priv().abandoned()) {
188 return nullptr;
189 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500190
Greg Daniele20fcad2020-01-08 11:52:34 -0500191 sk_sp<GrTextureProxy> proxy = context->priv().proxyProvider()->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400192 format, dimensions, GrRenderable::kYes, sampleCnt, mipMapped, fit, budgeted,
Greg Daniel3a365112020-02-14 10:47:18 -0500193 isProtected);
Greg Daniele20fcad2020-01-08 11:52:34 -0500194 if (!proxy) {
195 return nullptr;
196 }
Greg Daniele20fcad2020-01-08 11:52:34 -0500197
Greg Danielba0ff782020-01-07 15:42:57 -0500198 auto rtc = GrRenderTargetContext::Make(context, colorType, std::move(colorSpace),
199 std::move(proxy), origin, surfaceProps, true);
Greg Daniele20fcad2020-01-08 11:52:34 -0500200 if (!rtc) {
201 return nullptr;
202 }
203 rtc->discard();
204 return rtc;
205}
206
207std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::Make(
208 GrRecordingContext* context,
209 GrColorType colorType,
210 sk_sp<SkColorSpace> colorSpace,
211 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500212 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500213 int sampleCnt,
214 GrMipMapped mipMapped,
215 GrProtected isProtected,
216 GrSurfaceOrigin origin,
217 SkBudgeted budgeted,
218 const SkSurfaceProps* surfaceProps) {
219 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
220 if (!format.isValid()) {
221 return nullptr;
222 }
223
224 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), fit, dimensions,
225 format, sampleCnt, mipMapped, isProtected, origin, budgeted,
226 surfaceProps);
227}
228
229static inline GrColorType color_type_fallback(GrColorType ct) {
230 switch (ct) {
231 // kRGBA_8888 is our default fallback for many color types that may not have renderable
232 // backend formats.
233 case GrColorType::kAlpha_8:
234 case GrColorType::kBGR_565:
235 case GrColorType::kABGR_4444:
236 case GrColorType::kBGRA_8888:
237 case GrColorType::kRGBA_1010102:
238 case GrColorType::kRGBA_F16:
239 case GrColorType::kRGBA_F16_Clamped:
240 return GrColorType::kRGBA_8888;
241 case GrColorType::kAlpha_F16:
242 return GrColorType::kRGBA_F16;
243 case GrColorType::kGray_8:
244 return GrColorType::kRGB_888x;
245 default:
246 return GrColorType::kUnknown;
247 }
248}
249
Brian Salomond005b692020-04-01 15:47:05 -0400250std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
Brian Salomon0029db02020-04-03 10:41:24 -0400251 GrImageContext* context, GrColorType colorType, int sampleCnt) {
252 auto caps = context->priv().caps();
Brian Salomond005b692020-04-01 15:47:05 -0400253 do {
Brian Salomon0029db02020-04-03 10:41:24 -0400254 auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
255 // We continue to the fallback color type if there no default renderable format or we
256 // requested msaa and the format doesn't support msaa.
257 if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
Brian Salomond005b692020-04-01 15:47:05 -0400258 return {colorType, format};
259 }
260 colorType = color_type_fallback(colorType);
261 } while (colorType != GrColorType::kUnknown);
262 return {GrColorType::kUnknown, {}};
263}
264
Greg Daniele20fcad2020-01-08 11:52:34 -0500265std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
266 GrRecordingContext* context,
267 GrColorType colorType,
268 sk_sp<SkColorSpace> colorSpace,
269 SkBackingFit fit,
Brian Salomona56a7462020-02-07 14:17:25 -0500270 SkISize dimensions,
Greg Daniele20fcad2020-01-08 11:52:34 -0500271 int sampleCnt,
272 GrMipMapped mipMapped,
273 GrProtected isProtected,
274 GrSurfaceOrigin origin,
275 SkBudgeted budgeted,
276 const SkSurfaceProps* surfaceProps) {
Brian Salomon0029db02020-04-03 10:41:24 -0400277 auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
Brian Salomond005b692020-04-01 15:47:05 -0400278 if (ct == GrColorType::kUnknown) {
279 return nullptr;
280 }
281 return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
282 mipMapped, isProtected, origin, budgeted, surfaceProps);
Greg Daniele20fcad2020-01-08 11:52:34 -0500283}
284
Greg Danielba0ff782020-01-07 15:42:57 -0500285std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(
286 GrRecordingContext* context,
287 GrColorType colorType,
288 sk_sp<SkColorSpace> colorSpace,
289 const GrBackendTexture& tex,
290 int sampleCnt,
291 GrSurfaceOrigin origin,
292 const SkSurfaceProps* surfaceProps,
293 ReleaseProc releaseProc,
294 ReleaseContext releaseCtx) {
295 SkASSERT(sampleCnt > 0);
296 sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400297 tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
Greg Daniel3a365112020-02-14 10:47:18 -0500298 releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500299 if (!proxy) {
300 return nullptr;
301 }
302
303 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
304 origin, surfaceProps);
305}
306
307std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTextureAsRenderTarget(
308 GrRecordingContext* context,
309 GrColorType colorType,
310 sk_sp<SkColorSpace> colorSpace,
311 const GrBackendTexture& tex,
312 int sampleCnt,
313 GrSurfaceOrigin origin,
314 const SkSurfaceProps* surfaceProps) {
315 SkASSERT(sampleCnt > 0);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400316 sk_sp<GrSurfaceProxy> proxy(
317 context->priv().proxyProvider()->wrapBackendTextureAsRenderTarget(tex, sampleCnt));
Greg Danielba0ff782020-01-07 15:42:57 -0500318 if (!proxy) {
319 return nullptr;
320 }
321
322 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
323 origin, surfaceProps);
324}
325
326std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendRenderTarget(
327 GrRecordingContext* context,
328 GrColorType colorType,
329 sk_sp<SkColorSpace> colorSpace,
330 const GrBackendRenderTarget& rt,
331 GrSurfaceOrigin origin,
332 const SkSurfaceProps* surfaceProps,
333 ReleaseProc releaseProc,
334 ReleaseContext releaseCtx) {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400335 sk_sp<GrSurfaceProxy> proxy(
336 context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
Greg Danielba0ff782020-01-07 15:42:57 -0500337 if (!proxy) {
338 return nullptr;
339 }
340
341 return GrRenderTargetContext::Make(context, colorType, std::move(colorSpace), std::move(proxy),
342 origin, surfaceProps);
343}
344
345std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromVulkanSecondaryCB(
346 GrRecordingContext* context,
347 const SkImageInfo& imageInfo,
348 const GrVkDrawableInfo& vkInfo,
349 const SkSurfaceProps* props) {
350 sk_sp<GrSurfaceProxy> proxy(
351 context->priv().proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
352 vkInfo));
353 if (!proxy) {
354 return nullptr;
355 }
356
Greg Danielba0ff782020-01-07 15:42:57 -0500357 return GrRenderTargetContext::Make(context, SkColorTypeToGrColorType(imageInfo.colorType()),
358 imageInfo.refColorSpace(), std::move(proxy),
359 kTopLeft_GrSurfaceOrigin, props);
360}
361
Greg Danielf41b2bd2019-08-22 16:19:24 -0400362// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
363// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
364// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
365// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500366GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500367 GrSurfaceProxyView readView,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400368 GrSurfaceProxyView writeView,
Brian Salomond6287472019-06-24 15:50:07 -0400369 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400370 sk_sp<SkColorSpace> colorSpace,
371 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400372 bool managedOpsTask)
Greg Daniel3912a4b2020-01-14 09:56:04 -0500373 : GrSurfaceContext(context, std::move(readView), colorType, kPremul_SkAlphaType,
374 std::move(colorSpace))
Brian Salomon8afde5f2020-04-01 16:22:00 -0400375 , fWriteView(std::move(writeView))
Greg Daniel3912a4b2020-01-14 09:56:04 -0500376 , fOpsTask(sk_ref_sp(this->asSurfaceProxy()->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400377 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400378 , fManagedOpsTask(managedOpsTask) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400379 SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
380 SkASSERT(this->origin() == fWriteView.origin());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500381
Brian Salomonf18b1d82017-10-27 11:30:49 -0400382 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700383 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700384}
385
robertphillips2e1e51f2015-10-15 08:01:48 -0700386#ifdef SK_DEBUG
Greg Daniel46e366a2019-12-16 14:38:36 -0500387void GrRenderTargetContext::onValidate() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400388 if (fOpsTask && !fOpsTask->isClosed()) {
Brian Salomon8afde5f2020-04-01 16:22:00 -0400389 SkASSERT(fWriteView.proxy()->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700390 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700391}
392#endif
393
Brian Osman11052242016-10-27 14:47:55 -0400394GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800395 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700396}
397
Chris Dalton7d6748e2019-03-13 00:34:52 -0600398inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600399 if (GrAA::kNo == aa) {
400 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
401 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600402 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600403 return GrAAType::kMSAA;
404 }
405 return GrAAType::kNone;
406 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600407 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600408}
409
Greg Daniele252f082017-10-23 16:05:23 -0400410GrMipMapped GrRenderTargetContext::mipMapped() const {
411 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
412 return proxy->mipMapped();
413 }
414 return GrMipMapped::kNo;
415}
416
Greg Danielf41b2bd2019-08-22 16:19:24 -0400417GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800418 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700419 SkDEBUGCODE(this->validate();)
420
Greg Danielf41b2bd2019-08-22 16:19:24 -0400421 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600422 sk_sp<GrOpsTask> newOpsTask =
Brian Salomon8afde5f2020-04-01 16:22:00 -0400423 this->drawingManager()->newOpsTask(this->writeSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600424 if (fOpsTask && fNumStencilSamples > 0) {
425 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600426 fOpsTask->setMustPreserveStencil();
427 // Reload the stencil buffer content at the beginning of newOpsTask.
428 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
429 // values?
430 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
431 }
432 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700433 }
434
Greg Danielf41b2bd2019-08-22 16:19:24 -0400435 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700436}
437
Herb Derbycddab252018-07-16 11:19:04 -0400438void GrRenderTargetContext::drawGlyphRunList(
439 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400440 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800441 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700442 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700443 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400444 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700445
Greg Danielbe7fc462019-01-03 16:40:42 -0500446 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
447 // secondary command buffers because it would require stopping and starting a render pass which
448 // we don't have access to.
449 if (this->wrapsVkSecondaryCB()) {
450 return;
451 }
452
Herb Derby26cbe512018-05-24 14:39:01 -0400453 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400454 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400455 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700456}
457
Brian Osman11052242016-10-27 14:47:55 -0400458void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800459 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700460 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700461 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400462 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700463
Robert Phillips72152832017-01-25 17:31:35 -0500464 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400465
Greg Danielf41b2bd2019-08-22 16:19:24 -0400466 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700467}
468
Brian Osman11052242016-10-27 14:47:55 -0400469void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500470 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700471 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800472 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700473 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700474 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400475 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700476
Robert Phillips72152832017-01-25 17:31:35 -0500477 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700478 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
479 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700480}
robertphillips9199a9f2016-07-13 07:48:43 -0700481
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500482void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
483 const SkPMColor4f& color,
484 CanClearFullscreen canClearFullscreen) {
485 ASSERT_SINGLE_OWNER_PRIV
486 RETURN_IF_ABANDONED_PRIV
487 SkDEBUGCODE(fRenderTargetContext->validate();)
488 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
489 fRenderTargetContext->fContext);
490
491 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
492 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
493}
494
495static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
496 paint->setColor4f(color);
497 if (color.isOpaque()) {
498 // Can just rely on the src-over blend mode to do the right thing
499 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
500 } else {
501 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
502 // were src blended
503 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
504 }
505}
506
507void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
508 const SkPMColor4f& color,
509 CanClearFullscreen canClearFullscreen) {
510 bool isFull = false;
511 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400512 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
513 // only clear the entire target if we knew it had not been cleared before. As
514 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500515 isFull = !clip.scissorEnabled() ||
516 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400517 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500518 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
519 }
520
521 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400522 GrOpsTask* opsTask = this->getOpsTask();
523 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500524 !this->caps()->performColorClearsAsDraws()) {
525 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400526 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500527 return;
528 } else {
529 // Will use an op for the clear, reset the load op to discard since the op will
530 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400531 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500532 }
533
534 // Must add an op to the list (either because we couldn't use a load op, or because the
535 // clear load op isn't supported)
536 if (this->caps()->performColorClearsAsDraws()) {
537 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
538 GrPaint paint;
539 clear_to_grpaint(color, &paint);
540 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400541 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
542 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500543 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600544 this->addOp(GrClearOp::Make(
545 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500546 }
547 } else {
548 if (this->caps()->performPartialClearsAsDraws()) {
549 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500550 GrPaint paint;
551 clear_to_grpaint(color, &paint);
552
Michael Ludwig64b28a72019-05-28 12:02:00 -0400553 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400554 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
555 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500556 } else {
557 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
558 this->asSurfaceProxy()));
559 // This version of the clear op factory can return null if the clip doesn't intersect
560 // with the surface proxy's boundary
561 if (!op) {
562 return;
563 }
Chris Dalton08755122019-08-05 16:13:47 -0600564 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500565 }
566 }
567}
568
Brian Osman11052242016-10-27 14:47:55 -0400569void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500570 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400571 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000572 // Start with the render target, since that is the maximum content we could possibly fill.
573 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500574 SkRect r = this->asSurfaceProxy()->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400575 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000576 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
577 // inverse view matrix.
578 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
579 } else {
580 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
581 SkMatrix localMatrix;
582 if (!viewMatrix.invert(&localMatrix)) {
583 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400584 }
Michael Ludwig61328202019-06-19 14:48:58 +0000585 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
586 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700587 }
robertphillipsea461502015-05-26 11:38:03 -0700588}
589
Michael Ludwig61328202019-06-19 14:48:58 +0000590enum class GrRenderTargetContext::QuadOptimization {
591 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
592 kDiscarded,
593 // The rect to draw was converted to some other op and appended to the oplist, so no additional
594 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
595 // a constColor is provided.
596 kSubmitted,
597 // The clip was folded into the device quad, with updated edge flags and local coords, and
598 // caller is responsible for adding an appropriate op.
599 kClipApplied,
600 // No change to clip, but quad updated to better fit clip/render target, and caller is
601 // responsible for adding an appropriate op.
602 kCropped
603};
Michael Ludwig61a16512019-01-15 11:15:13 -0500604
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400605static bool make_vertex_finite(float* value) {
606 if (SkScalarIsNaN(*value)) {
607 return false;
608 }
609
610 if (!SkScalarIsFinite(*value)) {
611 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
612 // left when calculating crops.
613 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
614 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
615 }
616
617 return true;
618}
619
Michael Ludwig61328202019-06-19 14:48:58 +0000620GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400621 const GrClip& clip, const SkPMColor4f* constColor,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500622 const GrUserStencilSettings* stencilSettings, GrAA* aa, DrawQuad* quad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000623 // Optimization requirements:
624 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500625 // 2a. kSubmitted applies when constColor and final geom is pixel aligned rect;
626 // pixel aligned rect requires rect clip and (rect quad or quad covers clip) OR
627 // 2b. kSubmitted applies when constColor and rrect clip and quad covers clip
628 // 4. kClipApplied applies when rect clip and (rect quad or quad covers clip)
629 // 5. kCropped in all other scenarios (although a crop may be a no-op)
630
631 // Save the old AA flags since CropToRect will modify 'quad' and if kCropped is returned, it's
632 // better to just keep the old flags instead of introducing mixed edge flags.
633 GrQuadAAFlags oldFlags = quad->fEdgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000634
Michael Ludwige08b4432019-06-19 18:00:48 -0400635 SkRect rtRect;
636 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400637 // Must use size at which the rendertarget will ultimately be allocated so that stencil
638 // buffer updates on approximately sized render targets don't get corrupted.
Greg Daniel3912a4b2020-01-14 09:56:04 -0500639 rtRect = this->asSurfaceProxy()->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400640 } else {
641 // Use the logical size of the render target, which allows for "fullscreen" clears even if
642 // the render target has an approximate backing fit
643 rtRect = SkRect::MakeWH(this->width(), this->height());
644 }
645
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500646 SkRect drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000647 if (constColor) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400648 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
649 // will be cropped to the finite 'clip' or render target and there is no local space mapping
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500650 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400651 for (int i = 0; i < 4; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500652 if (!make_vertex_finite(quad->fDevice.xs() + i) ||
653 !make_vertex_finite(quad->fDevice.ys() + i) ||
654 !make_vertex_finite(quad->fDevice.ws() + i)) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400655 // Discard if we see a nan
656 return QuadOptimization::kDiscarded;
657 }
658 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500659 SkASSERT(quad->fDevice.isFinite());
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400660 }
661 } else {
662 // CropToRect requires the quads to be finite. If they are not finite and we have local
663 // coordinates, the mapping from local space to device space is poorly defined so drop it
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500664 if (!quad->fDevice.isFinite()) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400665 return QuadOptimization::kDiscarded;
666 }
Brian Salomon7694b902019-06-18 21:00:21 +0000667 }
668
Michael Ludwig61328202019-06-19 14:48:58 +0000669 // If the quad is entirely off screen, it doesn't matter what the clip does
670 if (!rtRect.intersects(drawBounds)) {
671 return QuadOptimization::kDiscarded;
672 }
Brian Salomon7694b902019-06-18 21:00:21 +0000673
Michael Ludwig61328202019-06-19 14:48:58 +0000674 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
675 // the render target).
676 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400677 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
678 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
679 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
680 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000681 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000682 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000683 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
684 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000685 }
Brian Salomon7694b902019-06-18 21:00:21 +0000686 }
687
Michael Ludwig61328202019-06-19 14:48:58 +0000688 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
689 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400690 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000691 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
692 // of the render target and the clip rect)
693 SkRect clipBounds = rtRect;
694 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
695 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000696 }
697
Michael Ludwig61328202019-06-19 14:48:58 +0000698 if (clipRRect.isRect()) {
699 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500700 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /*compute local*/ !constColor)) {
Chris Daltonbc3307c2020-02-04 13:21:03 -0700701 if (!stencilSettings && constColor &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500702 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig61328202019-06-19 14:48:58 +0000703 // Clear optimization is possible
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500704 drawBounds = quad->fDevice.bounds();
Michael Ludwig61328202019-06-19 14:48:58 +0000705 if (drawBounds.contains(rtRect)) {
706 // Fullscreen clear
707 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
708 return QuadOptimization::kSubmitted;
709 } else if (GrClip::IsPixelAligned(drawBounds) &&
710 drawBounds.width() > 256 && drawBounds.height() > 256) {
711 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
712 SkIRect scissorRect;
713 drawBounds.round(&scissorRect);
714 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
715 return QuadOptimization::kSubmitted;
716 }
717 }
718
719 // Update overall AA setting.
Michael Ludwig61328202019-06-19 14:48:58 +0000720 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500721 quad->fEdgeFlags != GrQuadAAFlags::kNone) {
Michael Ludwig61328202019-06-19 14:48:58 +0000722 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
723 // properly reflect the smooth edge of the clip.
724 *aa = GrAA::kYes;
725 }
726 // We intentionally do not downgrade AA here because we don't know if we need to
727 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
728 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
729
730 // deviceQuad is exactly the intersection of original quad and clip, so it can be
731 // drawn with no clip (submitted by caller)
732 return QuadOptimization::kClipApplied;
733 } else {
734 // The quads have been updated to better fit the clip bounds, but can't get rid of
735 // the clip entirely
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500736 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000737 return QuadOptimization::kCropped;
738 }
Chris Daltonbc3307c2020-02-04 13:21:03 -0700739 } else if (!stencilSettings && constColor) {
Michael Ludwig61328202019-06-19 14:48:58 +0000740 // Rounded corners and constant filled color (limit ourselves to solid colors because
741 // there is no way to use custom local coordinates with drawRRect).
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500742 if (GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ false) &&
743 quad->fDevice.quadType() == GrQuad::Type::kAxisAligned &&
744 quad->fDevice.bounds().contains(clipBounds)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000745 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
746 // we can draw the rrect directly and ignore the edge flags
747 GrPaint paint;
748 clear_to_grpaint(*constColor, &paint);
749 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
750 clipRRect, GrStyle::SimpleFill());
751 return QuadOptimization::kSubmitted;
752 } else {
753 // The quad has been updated to better fit clip bounds, but can't remove the clip
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500754 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000755 return QuadOptimization::kCropped;
756 }
Brian Salomon7694b902019-06-18 21:00:21 +0000757 }
Brian Salomon7694b902019-06-18 21:00:21 +0000758 }
759
Michael Ludwig61328202019-06-19 14:48:58 +0000760 // Crop the quad to the conservative bounds of the clip.
761 SkIRect clipDevBounds;
762 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
763 SkRect clipBounds = SkRect::Make(clipDevBounds);
764
765 // One final check for discarding, since we may have gone here directly due to a complex clip
766 if (!clipBounds.intersects(drawBounds)) {
767 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000768 }
769
Michael Ludwig61328202019-06-19 14:48:58 +0000770 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
771 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500772 GrQuadUtils::CropToRect(clipBounds, clipAA, quad, /* compute local */ !constColor);
773 quad->fEdgeFlags = oldFlags;
Michael Ludwig61328202019-06-19 14:48:58 +0000774
775 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000776}
777
Michael Ludwig61328202019-06-19 14:48:58 +0000778void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500779 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500780 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500781 DrawQuad* quad,
Brian Osman11052242016-10-27 14:47:55 -0400782 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000783 ASSERT_SINGLE_OWNER
784 RETURN_IF_ABANDONED
785 SkDEBUGCODE(this->validate();)
786 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500787
Michael Ludwig61328202019-06-19 14:48:58 +0000788 AutoCheckFlush acf(this->drawingManager());
789
790 SkPMColor4f* constColor = nullptr;
791 SkPMColor4f paintColor;
792 if (!ss && !paint.numCoverageFragmentProcessors() &&
793 paint.isConstantBlendedColor(&paintColor)) {
794 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
795 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500796 }
797
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500798 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, quad);
Michael Ludwig61328202019-06-19 14:48:58 +0000799 if (opt >= QuadOptimization::kClipApplied) {
800 // These optimizations require caller to add an op themselves
801 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
802 : clip;
803 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
804 : this->chooseAAType(aa);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500805 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType,
806 quad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700807 }
Michael Ludwig61328202019-06-19 14:48:58 +0000808 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800809}
810
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000811void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400812 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500813 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000814 sk_sp<GrColorSpaceXform> textureXform,
815 GrSamplerState::Filter filter,
816 const SkPMColor4f& color,
817 SkBlendMode blendMode,
818 GrAA aa,
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500819 DrawQuad* quad,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000820 const SkRect* domain) {
821 ASSERT_SINGLE_OWNER
822 RETURN_IF_ABANDONED
823 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400824 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000825 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
826
827 AutoCheckFlush acf(this->drawingManager());
828
829 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
830 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500831 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, quad);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000832
833 SkASSERT(opt != QuadOptimization::kSubmitted);
834 if (opt != QuadOptimization::kDiscarded) {
835 // And the texture op if not discarded
836 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
837 : clip;
838 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400839 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400840 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
841 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000842 // Use the provided domain, although hypothetically we could detect that the cropped local
843 // quad is sufficiently inside the domain and the constraint could be dropped.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500844 this->addDrawOp(finalClip,
845 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
846 std::move(textureXform), filter, color, saturate,
847 blendMode, aaType, quad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000848 }
849}
850
Brian Osman11052242016-10-27 14:47:55 -0400851void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500852 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500853 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400854 const SkMatrix& viewMatrix,
855 const SkRect& rect,
856 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700857 if (!style) {
858 style = &GrStyle::SimpleFill();
859 }
joshualitt1de610a2016-01-06 08:26:09 -0800860 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700861 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700862 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400863 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700864
bsalomon6663acf2016-05-10 09:14:17 -0700865 // Path effects should've been devolved to a path in SkGpuDevice
866 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700867
Robert Phillips72152832017-01-25 17:31:35 -0500868 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700869
bsalomon6663acf2016-05-10 09:14:17 -0700870 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400871 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000872 // Fills the rect, using rect as its own local coordinates
873 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500874 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700875 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
876 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
877 if ((!rect.width() || !rect.height()) &&
878 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
879 SkScalar r = stroke.getWidth() / 2;
880 // TODO: Move these stroke->fill fallbacks to GrShape?
881 switch (stroke.getJoin()) {
882 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500883 this->drawRect(
884 clip, std::move(paint), aa, viewMatrix,
885 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
886 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700887 return;
888 case SkPaint::kRound_Join:
889 // Raster draws nothing when both dimensions are empty.
890 if (rect.width() || rect.height()){
891 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500892 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
893 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700894 return;
895 }
896 case SkPaint::kBevel_Join:
897 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500898 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700899 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
900 &GrStyle::SimpleFill());
901 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500902 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700903 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
904 &GrStyle::SimpleFill());
905 }
906 return;
907 }
908 }
robertphillips44302392016-07-08 14:43:03 -0700909
Brian Salomonbaaf4392017-06-15 09:59:23 -0400910 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700911
Chris Dalton7d6748e2019-03-13 00:34:52 -0600912 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500913 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
914 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
915 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500916 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400917 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700918 return;
robertphillips4bc31812016-03-01 12:22:49 -0800919 }
robertphillips4bc31812016-03-01 12:22:49 -0800920 }
Mike Klein16885072018-12-11 09:54:31 -0500921 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500922 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700923}
924
Michael Ludwig69858532018-11-28 15:34:34 -0500925void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
926 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
927 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600928 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500929
930 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
931 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500932}
933
Robert Phillipsec2249f2016-11-09 08:54:35 -0500934int GrRenderTargetContextPriv::maxWindowRectangles() const {
Greg Daniel46e366a2019-12-16 14:38:36 -0500935 return fRenderTargetContext->asRenderTargetProxy()->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400936 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500937}
938
Greg Danielf41b2bd2019-08-22 16:19:24 -0400939GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600940 ) const {
941#if GR_TEST_UTILS
942 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400943 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600944 }
945#endif
946 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
947 // would normally be overwritten. The one exception is if the render target context is marked as
948 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
949 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
950 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
951 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400952 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600953}
954
Chris Dalton858cf232019-10-14 16:20:00 -0600955void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600956 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600957 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600958 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600959
Chris Daltoneffee202019-07-01 22:28:03 -0600960 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600961 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500962 SkASSERT(this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Chris Daltoneffee202019-07-01 22:28:03 -0600963 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400964 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600965 }
966 SkASSERT(numRequiredSamples > 0);
967
968 if (numRequiredSamples > fNumStencilSamples) {
969 fNumStencilSamples = numRequiredSamples;
Greg Daniel46e366a2019-12-16 14:38:36 -0500970 this->asRenderTargetProxy()->setNeedsStencil(fNumStencilSamples);
Chris Daltoneffee202019-07-01 22:28:03 -0600971 }
Chris Dalton6b982802019-06-27 13:53:46 -0600972
Chris Dalton674f77a2019-09-30 20:49:39 -0600973 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600974 if (this->caps()->performStencilClearsAsDraws()) {
975 // There is a driver bug with clearing stencil. We must use an op to manually clear the
976 // stencil buffer before the op that required 'setNeedsStencil'.
977 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
978 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600979 this->getOpsTask()->setInitialStencilContent(
980 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600981 }
982 }
983}
984
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000985void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700986 ASSERT_SINGLE_OWNER_PRIV
987 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400988 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400989 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
990 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700991
Robert Phillips72152832017-01-25 17:31:35 -0500992 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400993
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500994 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
995}
996
997void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600998 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
999
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001000 if (this->caps()->performStencilClearsAsDraws()) {
1001 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
1002 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
1003
1004 // Configure the paint to have no impact on the color buffer
1005 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -04001006 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04001007 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
1008 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001009 } else {
1010 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
Greg Daniel46e366a2019-12-16 14:38:36 -05001011 this->asRenderTargetProxy()));
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001012 if (!op) {
1013 return;
1014 }
Chris Dalton08755122019-08-05 16:13:47 -06001015 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -05001016 }
robertphillips976f5f02016-06-03 10:59:20 -07001017}
1018
Chris Daltonbbfd5162017-11-07 13:35:22 -07001019void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -06001020 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -04001021 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -04001022 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -05001023 ASSERT_SINGLE_OWNER_PRIV
1024 RETURN_IF_ABANDONED_PRIV
1025 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001026 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
1027 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -05001028
Brian Salomon467921e2017-03-06 16:17:12 -05001029 // TODO: extract portions of checkDraw that are relevant to path stenciling.
1030 SkASSERT(path);
1031 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
1032
1033 // FIXME: Use path bounds instead of this WAR once
1034 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
1035 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
1036
1037 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -07001038 GrAppliedHardClip appliedClip;
1039 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
1040 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001041 return;
1042 }
1043
Robert Phillips7c525e62018-06-12 10:11:12 -04001044 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
1045 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -06001046 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -05001047 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -05001048 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -04001049 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -04001050 if (!op) {
1051 return;
1052 }
Brian Salomon97180af2017-03-14 13:42:58 -04001053 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -06001054
Chris Daltoneffee202019-07-01 22:28:03 -06001055 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -06001056 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -07001057}
1058
Michael Ludwig379e4962019-12-06 13:21:26 -05001059void GrRenderTargetContext::drawTextureSet(const GrClip& clip, TextureSetEntry set[],
1060 int cnt, int proxyRunCnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001061 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -04001062 GrAA aa, SkCanvas::SrcRectConstraint constraint,
1063 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -05001064 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -04001065 ASSERT_SINGLE_OWNER
1066 RETURN_IF_ABANDONED
1067 SkDEBUGCODE(this->validate();)
1068 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -05001069
Michael Ludwigfe13ca32019-11-21 10:26:41 -05001070 // Create the minimum number of GrTextureOps needed to draw this set. Individual
1071 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
1072 AutoCheckFlush acf(this->drawingManager());
1073 GrAAType aaType = this->chooseAAType(aa);
1074 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
1075 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
1076 : GrTextureOp::Saturate::kNo;
Michael Ludwig379e4962019-12-06 13:21:26 -05001077 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, proxyRunCnt, filter, saturate,
1078 mode, aaType, constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -04001079}
1080
Brian Osman11052242016-10-27 14:47:55 -04001081void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001082 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001083 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001084 sk_sp<SkVertices> vertices,
Brian Osman3c358422020-03-23 10:44:12 -04001085 GrPrimitiveType* overridePrimType,
Brian Osmanaa9983a2020-04-02 16:49:59 -04001086 const SkRuntimeEffect* effect,
Brian Osmand1afef62020-04-09 16:24:23 -04001087 const SkMarkedMatrixProvider* matrixProvider) {
Brian Salomon199fb872017-02-06 09:41:10 -05001088 ASSERT_SINGLE_OWNER
1089 RETURN_IF_ABANDONED
1090 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001091 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001092
1093 AutoCheckFlush acf(this->drawingManager());
1094
1095 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -06001096 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Osmand1afef62020-04-09 16:24:23 -04001097 std::unique_ptr<GrDrawOp> op =
1098 GrDrawVerticesOp::Make(fContext, std::move(paint), std::move(vertices), viewMatrix,
1099 aaType, this->colorInfo().refColorSpaceXformFromSRGB(),
1100 overridePrimType, effect, matrixProvider);
Brian Salomonc2f42542017-07-12 14:11:22 -04001101 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001102}
1103
1104///////////////////////////////////////////////////////////////////////////////
1105
Brian Osman4d92b892019-03-24 00:53:23 +00001106void GrRenderTargetContext::drawAtlas(const GrClip& clip,
1107 GrPaint&& paint,
1108 const SkMatrix& viewMatrix,
1109 int spriteCount,
1110 const SkRSXform xform[],
1111 const SkRect texRect[],
1112 const SkColor colors[]) {
1113 ASSERT_SINGLE_OWNER
1114 RETURN_IF_ABANDONED
1115 SkDEBUGCODE(this->validate();)
1116 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1117
1118 AutoCheckFlush acf(this->drawingManager());
1119
1120 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1121 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1122 aaType, spriteCount, xform, texRect, colors);
1123 this->addDrawOp(clip, std::move(op));
1124}
1125
1126///////////////////////////////////////////////////////////////////////////////
1127
Brian Osman11052242016-10-27 14:47:55 -04001128void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001129 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001130 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001131 const SkMatrix& viewMatrix,
1132 const SkRRect& rrect,
1133 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001134 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001135 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001136 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001137 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001138
1139 const SkStrokeRec& stroke = style.strokeRec();
1140 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001141 return;
1142 }
1143
bsalomon7f0d9f32016-08-15 14:49:10 -07001144 GrNoClip noclip;
1145 const GrClip* clip = &origClip;
1146#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1147 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001148 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001149 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1150 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001151 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1152 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001153 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001154 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1155 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001156 clip = &noclip;
1157 }
1158#endif
bsalomon6663acf2016-05-10 09:14:17 -07001159 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001160
Robert Phillips72152832017-01-25 17:31:35 -05001161 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001162
Chris Dalton7d6748e2019-03-13 00:34:52 -06001163 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001164
Chris Dalton0dffbab2019-03-27 13:08:50 -06001165 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001166 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1167 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1168 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1169 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1170 // to avoid perf regressions on some platforms.
1171 assert_alive(paint);
1172 op = GrOvalOpFactory::MakeCircularRRectOp(
1173 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1174 }
1175 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001176 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001177 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, rrect, aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001178 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001179 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001180 assert_alive(paint);
1181 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001182 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001183 }
1184 if (op) {
1185 this->addDrawOp(*clip, std::move(op));
1186 return;
robertphillipsea461502015-05-26 11:38:03 -07001187 }
robertphillipsb56f9272016-02-25 11:03:52 -08001188
Mike Klein16885072018-12-11 09:54:31 -05001189 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001190 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1191 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001192}
1193
Jim Van Verthc5903412016-11-17 15:27:09 -05001194///////////////////////////////////////////////////////////////////////////////
1195
Jim Van Verth3af1af92017-05-18 15:06:54 -04001196static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1197 SkPoint3 result;
1198 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1199 result.fZ = pt.fZ;
1200 return result;
1201}
1202
1203bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001204 const SkMatrix& viewMatrix,
1205 const SkPath& path,
1206 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001207 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001208 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001209 return true;
1210 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001211 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001212 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001213
1214 // check z plane
1215 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1216 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1217 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1218 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1219 return false;
1220 }
1221
1222 SkRRect rrect;
1223 SkRect rect;
1224 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001225 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001226 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1227 if (!isRRect &&
1228 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1229 rect.width() > SK_ScalarNearlyZero) {
1230 rrect.setOval(rect);
1231 isRRect = true;
1232 }
1233 if (!isRRect && path.isRect(&rect)) {
1234 rrect.setRect(rect);
1235 isRRect = true;
1236 }
1237
1238 if (!isRRect) {
1239 return false;
1240 }
1241
Jim Van Verthc5903412016-11-17 15:27:09 -05001242 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001243 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001244 }
1245
Robert Phillips72152832017-01-25 17:31:35 -05001246 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001247
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 // transform light
1249 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1250
1251 // 1/scale
1252 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001253 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1255 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1256
1257 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001258 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1259
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001260 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001261 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1262 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1263 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001264
1265 // Outset the shadow rrect to the border of the penumbra
1266 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1267 SkRRect ambientRRect;
1268 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1269 // If the rrect was an oval then its outset will also be one.
1270 // We set it explicitly to avoid errors.
1271 if (rrect.isOval()) {
1272 ambientRRect = SkRRect::MakeOval(outsetRect);
1273 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001274 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001275 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1276 }
1277
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001278 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001279 if (transparent) {
1280 // set a large inset to force a fill
1281 devSpaceInsetWidth = ambientRRect.width();
1282 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001283
Robert Phillips7c525e62018-06-12 10:11:12 -04001284 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1285 ambientColor,
1286 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001287 ambientRRect,
1288 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001289 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001290 if (op) {
1291 this->addDrawOp(clip, std::move(op));
1292 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001293 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001294
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001295 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001296 SkScalar devSpaceSpotBlur;
1297 SkScalar spotScale;
1298 SkVector spotOffset;
1299 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1300 devLightPos.fZ, rec.fLightRadius,
1301 &devSpaceSpotBlur, &spotScale, &spotOffset);
1302 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001303 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1304
Jim Van Verth3af1af92017-05-18 15:06:54 -04001305 // Adjust translate for the effect of the scale.
1306 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1307 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1308 // This offset is in dev space, need to transform it into source space.
1309 SkMatrix ctmInverse;
1310 if (viewMatrix.invert(&ctmInverse)) {
1311 ctmInverse.mapPoints(&spotOffset, 1);
1312 } else {
1313 // Since the matrix is a similarity, this should never happen, but just in case...
1314 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1315 SkASSERT(false);
1316 }
1317
1318 // Compute the transformed shadow rrect
1319 SkRRect spotShadowRRect;
1320 SkMatrix shadowTransform;
1321 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1322 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001323 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001324
1325 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001326 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001327 SkScalar insetWidth = blurOutset;
1328 if (transparent) {
1329 // If transparent, just do a fill
1330 insetWidth += spotShadowRRect.width();
1331 } else {
1332 // For shadows, instead of using a stroke we specify an inset from the penumbra
1333 // border. We want to extend this inset area so that it meets up with the caster
1334 // geometry. The inset geometry will by default already be inset by the blur width.
1335 //
1336 // We compare the min and max corners inset by the radius between the original
1337 // rrect and the shadow rrect. The distance between the two plus the difference
1338 // between the scaled radius and the original radius gives the distance from the
1339 // transformed shadow shape to the original shape in that corner. The max
1340 // of these gives the maximum distance we need to cover.
1341 //
1342 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1343 // that to get the full insetWidth.
1344 SkScalar maxOffset;
1345 if (rrect.isRect()) {
1346 // Manhattan distance works better for rects
Brian Osman788b9162020-02-07 10:36:46 -05001347 maxOffset = std::max(std::max(SkTAbs(spotShadowRRect.rect().fLeft -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001348 rrect.rect().fLeft),
1349 SkTAbs(spotShadowRRect.rect().fTop -
1350 rrect.rect().fTop)),
Brian Osman788b9162020-02-07 10:36:46 -05001351 std::max(SkTAbs(spotShadowRRect.rect().fRight -
Jim Van Verth3af1af92017-05-18 15:06:54 -04001352 rrect.rect().fRight),
1353 SkTAbs(spotShadowRRect.rect().fBottom -
1354 rrect.rect().fBottom)));
1355 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001356 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001357 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1358 rrect.rect().fLeft + dr,
1359 spotShadowRRect.rect().fTop -
1360 rrect.rect().fTop + dr);
1361 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1362 rrect.rect().fRight - dr,
1363 spotShadowRRect.rect().fBottom -
1364 rrect.rect().fBottom - dr);
Brian Osman788b9162020-02-07 10:36:46 -05001365 maxOffset = SkScalarSqrt(std::max(SkPointPriv::LengthSqd(upperLeftOffset),
Cary Clarkdf429f32017-11-08 11:44:31 -05001366 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001367 }
Brian Osman788b9162020-02-07 10:36:46 -05001368 insetWidth += std::max(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001369 }
1370
1371 // Outset the shadow rrect to the border of the penumbra
1372 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1373 if (spotShadowRRect.isOval()) {
1374 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1375 } else {
1376 SkScalar outsetRad = spotRadius + blurOutset;
1377 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1378 }
1379
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001380 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001381
Robert Phillips7c525e62018-06-12 10:11:12 -04001382 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1383 spotColor,
1384 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001385 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001386 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001387 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001388 if (op) {
1389 this->addDrawOp(clip, std::move(op));
1390 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001391 }
1392
1393 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001394}
1395
1396///////////////////////////////////////////////////////////////////////////////
1397
Brian Osman11052242016-10-27 14:47:55 -04001398bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001399 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001400 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001401 const SkMatrix& viewMatrix,
1402 const SkRRect& origOuter,
1403 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001404 SkASSERT(!origInner.isEmpty());
1405 SkASSERT(!origOuter.isEmpty());
1406
Brian Salomon65749212017-12-01 16:01:47 -05001407 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1408
Chris Dalton7d6748e2019-03-13 00:34:52 -06001409 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001410
1411 if (GrAAType::kMSAA == aaType) {
1412 return false;
1413 }
1414
Greg Daniel2655ede2019-04-10 00:49:28 +00001415 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1416 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001417 auto outerR = outer->width() / 2.f;
1418 auto innerR = inner->width() / 2.f;
1419 auto cx = outer->getBounds().fLeft + outerR;
1420 auto cy = outer->getBounds().fTop + outerR;
1421 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1422 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1423 auto avgR = (innerR + outerR) / 2.f;
1424 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1425 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1426 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001427 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001428 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001429 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001430 if (op) {
1431 this->addDrawOp(clip, std::move(op));
1432 return true;
1433 }
Mike Klein16885072018-12-11 09:54:31 -05001434 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001435 }
1436 }
1437
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001438 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001439 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001440 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1441 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001442 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001443 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1444 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001445 }
robertphillips00095892016-02-29 13:50:40 -08001446
robertphillips00095892016-02-29 13:50:40 -08001447 SkMatrix inverseVM;
1448 if (!viewMatrix.isIdentity()) {
1449 if (!origInner.transform(viewMatrix, inner.writable())) {
1450 return false;
1451 }
1452 if (!origOuter.transform(viewMatrix, outer.writable())) {
1453 return false;
1454 }
1455 if (!viewMatrix.invert(&inverseVM)) {
1456 return false;
1457 }
1458 } else {
1459 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001460 }
robertphillips00095892016-02-29 13:50:40 -08001461
Ethan Nicholaseace9352018-10-15 20:09:54 +00001462 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001463 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001464 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001465 if (!innerEffect) {
1466 return false;
1467 }
1468
Ethan Nicholaseace9352018-10-15 20:09:54 +00001469 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001470 if (!outerEffect) {
1471 return false;
1472 }
1473
Brian Salomon82f44312017-01-11 13:42:54 -05001474 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1475 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001476
1477 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001478 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001479 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1480 }
halcanary9d524f22016-03-29 09:03:52 -07001481
Brian Salomon82f44312017-01-11 13:42:54 -05001482 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1483 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001484 return true;
1485}
1486
Brian Osman11052242016-10-27 14:47:55 -04001487void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001488 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001489 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001490 const SkMatrix& viewMatrix,
1491 const SkRRect& outer,
1492 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001493 ASSERT_SINGLE_OWNER
1494 RETURN_IF_ABANDONED
1495 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001496 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001497
1498 SkASSERT(!outer.isEmpty());
1499 SkASSERT(!inner.isEmpty());
1500
Robert Phillips72152832017-01-25 17:31:35 -05001501 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001502
Brian Salomon82f44312017-01-11 13:42:54 -05001503 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001504 return;
1505 }
Mike Klein16885072018-12-11 09:54:31 -05001506 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001507
1508 SkPath path;
1509 path.setIsVolatile(true);
1510 path.addRRect(inner);
1511 path.addRRect(outer);
Mike Reed7d34dc72019-11-26 12:17:17 -05001512 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001513 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001514}
1515
robertphillipsea461502015-05-26 11:38:03 -07001516///////////////////////////////////////////////////////////////////////////////
1517
Brian Osman11052242016-10-27 14:47:55 -04001518void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001519 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001520 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001521 const SkMatrix& viewMatrix,
1522 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001523 const GrStyle& style,
1524 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001525 ASSERT_SINGLE_OWNER
1526 RETURN_IF_ABANDONED
1527 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001528 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001529
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001530 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001531 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001532 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001533 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001534 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1535 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001536 aa = GrAA::kNo;
1537 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001538 }
msarettcc319b92016-08-25 18:07:18 -07001539 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001540 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001541 SkPath path;
1542 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001543 path.setIsVolatile(true);
1544
Brian Salomon82f44312017-01-11 13:42:54 -05001545 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001546 }
1547
Chris Dalton7d6748e2019-03-13 00:34:52 -06001548 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001549 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1550 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001551 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001552}
1553
Brian Osman11052242016-10-27 14:47:55 -04001554void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001555 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001556 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001557 const SkMatrix& viewMatrix,
1558 const SkRect& oval,
1559 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001560 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001561 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001562 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001563 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001564
Robert Phillips7484d202018-07-03 09:09:08 -04001565 const SkStrokeRec& stroke = style.strokeRec();
1566
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001567 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001568 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1569 return;
1570 }
1571
1572 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001573 return;
robertphillipsea461502015-05-26 11:38:03 -07001574 }
1575
Robert Phillips72152832017-01-25 17:31:35 -05001576 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001577
Chris Dalton7d6748e2019-03-13 00:34:52 -06001578 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001579
1580 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001581 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1582 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001583 // We don't draw true circles as round rects in coverage mode, because it can
1584 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1585 assert_alive(paint);
1586 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1587 this->caps()->shaderCaps());
1588 }
1589 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001590 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1591 // the arc equation. This same special geometry and fragment branch also turn out to be a
1592 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1593 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1594 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001595 assert_alive(paint);
Robert Phillips360ec182020-03-26 13:29:50 -04001596 op = GrFillRRectOp::Make(fContext, std::move(paint), viewMatrix, SkRRect::MakeOval(oval),
1597 aaType);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001598 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001599 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001600 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001601 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1602 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001603 }
1604 if (op) {
1605 this->addDrawOp(clip, std::move(op));
1606 return;
robertphillipsea461502015-05-26 11:38:03 -07001607 }
robertphillipsb56f9272016-02-25 11:03:52 -08001608
Mike Klein16885072018-12-11 09:54:31 -05001609 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001610 this->drawShapeUsingPathRenderer(
1611 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001612 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001613}
1614
Brian Osman11052242016-10-27 14:47:55 -04001615void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001616 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001617 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001618 const SkMatrix& viewMatrix,
1619 const SkRect& oval,
1620 SkScalar startAngle,
1621 SkScalar sweepAngle,
1622 bool useCenter,
1623 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001624 ASSERT_SINGLE_OWNER
1625 RETURN_IF_ABANDONED
1626 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001627 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001628
1629 AutoCheckFlush acf(this->drawingManager());
1630
Chris Dalton7d6748e2019-03-13 00:34:52 -06001631 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001632 if (GrAAType::kCoverage == aaType) {
1633 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1634 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1635 std::move(paint),
1636 viewMatrix,
1637 oval,
1638 startAngle,
1639 sweepAngle,
1640 useCenter,
1641 style,
1642 shaderCaps);
1643 if (op) {
1644 this->addDrawOp(clip, std::move(op));
1645 return;
1646 }
1647 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001648 }
Brian Salomone4949402018-04-26 15:22:04 -04001649 this->drawShapeUsingPathRenderer(
1650 clip, std::move(paint), aa, viewMatrix,
1651 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001652}
1653
Brian Osman11052242016-10-27 14:47:55 -04001654void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001655 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001656 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -05001657 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -05001658 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001659 sk_sp<GrColorSpaceXform> csxf,
1660 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001661 std::unique_ptr<SkLatticeIter> iter,
1662 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001663 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001664 RETURN_IF_ABANDONED
1665 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001666 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001667
Robert Phillips72152832017-01-25 17:31:35 -05001668 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001669
Brian Salomon2a943df2018-05-04 13:43:19 -04001670 std::unique_ptr<GrDrawOp> op =
Greg Danieled96bca2019-12-05 15:05:54 -05001671 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -05001672 alphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001673 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001674}
1675
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001676void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1677 const SkRect& bounds) {
1678 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1679 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001680 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001681}
1682
Brian Salomon031b0ba2019-05-23 11:05:26 -04001683void GrRenderTargetContext::asyncRescaleAndReadPixels(
1684 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1685 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1686 auto direct = fContext->priv().asDirectContext();
1687 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001688 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001689 return;
1690 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001691 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001692 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001693 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001694 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001695 if (this->asRenderTargetProxy()->framebufferOnly()) {
1696 callback(context, nullptr);
1697 return;
1698 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001699 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001700 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001701 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001702 return;
1703 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001704 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001705 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Greg Daniel3912a4b2020-01-14 09:56:04 -05001706 auto backendFormatOfFinalContext = this->asSurfaceProxy()->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001707 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001708 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001709 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1710 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001711 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001712 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001713 backendFormatOfFinalContext, dstCT);
1714 // Fail if we can't read from the source surface's color type.
1715 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001716 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001717 return;
1718 }
Brian Salomon624f9062019-07-02 14:23:00 -04001719 // Fail if read color type does not have all of dstCT's color channels and those missing color
1720 // channels are in the src.
Brian Salomon2f23ae62020-03-26 16:17:56 -04001721 uint32_t dstChannels = GrColorTypeChannelFlags(dstCT);
1722 uint32_t legalReadChannels = GrColorTypeChannelFlags(readInfo.fColorType);
1723 uint32_t srcChannels = GrColorTypeChannelFlags(this->colorInfo().colorType());
1724 if ((~legalReadChannels & dstChannels) & srcChannels) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001725 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001726 return;
1727 }
1728
Brian Salomonbf6b9792019-08-21 09:38:10 -04001729 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001730 int x = srcRect.fLeft;
1731 int y = srcRect.fTop;
1732 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001733 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1734 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001735 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001736 return;
1737 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001738 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001739 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001740 x = y = 0;
1741 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001742 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1743 this->colorInfo().alphaType(),
1744 info.colorSpace(),
1745 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1747 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001748 // We flip or color convert by drawing and we don't currently support drawing to
1749 // kPremul.
1750 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001751 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001752 return;
1753 }
Greg Daniel40903af2020-01-30 14:55:05 -05001754 GrSurfaceProxyView texProxyView = this->readSurfaceView();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001755 SkRect srcRectToDraw = SkRect::Make(srcRect);
1756 // If the src is not texturable first try to make a copy to a texture.
Greg Daniel40903af2020-01-30 14:55:05 -05001757 if (!texProxyView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001758 texProxyView =
1759 GrSurfaceProxyView::Copy(fContext, texProxyView, GrMipMapped::kNo, srcRect,
1760 SkBackingFit::kApprox, SkBudgeted::kNo);
1761 if (!texProxyView) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001762 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001763 return;
1764 }
Brian Salomonc5243782020-04-02 12:50:34 -04001765 SkASSERT(texProxyView.asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001766 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1767 }
Greg Daniele20fcad2020-01-08 11:52:34 -05001768 tempRTC = GrRenderTargetContext::Make(
1769 direct, this->colorInfo().colorType(), info.refColorSpace(),
1770 SkBackingFit::kApprox, srcRect.size(), 1, GrMipMapped::kNo, GrProtected::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001771 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001772 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001773 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001774 return;
1775 }
Greg Daniel40903af2020-01-30 14:55:05 -05001776 tempRTC->drawTexture(GrNoClip(), std::move(texProxyView), this->colorInfo().alphaType(),
1777 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1778 SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001779 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1780 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1781 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001782 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001783 }
1784 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001785 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001786 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1787 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001788}
1789
Brian Salomon9241a6d2019-10-03 13:26:54 -04001790class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1791public:
1792 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1793 ~AsyncReadResult() override {
1794 for (int i = 0; i < fPlanes.count(); ++i) {
1795 if (!fPlanes[i].fMappedBuffer) {
1796 delete[] static_cast<const char*>(fPlanes[i].fData);
1797 } else {
1798 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1799 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1800 }
1801 }
1802 }
1803
1804 int count() const override { return fPlanes.count(); }
1805 const void* data(int i) const override { return fPlanes[i].fData; }
1806 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1807
1808 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001809 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001810 size_t rowBytes,
1811 GrClientMappedBufferManager* manager) {
1812 SkASSERT(!result.fTransferBuffer->isMapped());
1813 const void* mappedData = result.fTransferBuffer->map();
1814 if (!mappedData) {
1815 return false;
1816 }
1817 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001818 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001819 result.fPixelConverter(convertedData.get(), mappedData);
1820 this->addCpuPlane(std::move(convertedData), rowBytes);
1821 result.fTransferBuffer->unmap();
1822 } else {
1823 manager->insert(result.fTransferBuffer);
1824 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1825 }
1826 return true;
1827 }
1828
1829 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1830 SkASSERT(data);
1831 SkASSERT(rowBytes > 0);
1832 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1833 }
1834
1835private:
1836 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1837 SkASSERT(data);
1838 SkASSERT(rowBytes > 0);
1839 SkASSERT(mappedBuffer);
1840 SkASSERT(mappedBuffer->isMapped());
1841 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1842 }
1843
1844 struct Plane {
1845 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1846 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1847 const void* fData;
1848 size_t fRowBytes;
1849 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1850 sk_sp<GrGpuBuffer> fMappedBuffer;
1851 };
1852 SkSTArray<3, Plane> fPlanes;
1853 uint32_t fInboxID;
1854};
1855
Brian Salomon024bd002019-06-11 11:38:16 -04001856void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1857 ReadPixelsCallback callback,
1858 ReadPixelsContext context) {
1859 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1860 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1861
Greg Danielca9dbe22020-01-02 13:44:30 -05001862 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001863 callback(context, nullptr);
1864 return;
1865 }
1866
Brian Salomon9241a6d2019-10-03 13:26:54 -04001867 auto directContext = fContext->priv().asDirectContext();
1868 SkASSERT(directContext);
1869 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1870
Brian Salomon024bd002019-06-11 11:38:16 -04001871 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1872
1873 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001874 auto ii = SkImageInfo::Make(rect.size(), colorType,
1875 this->colorInfo().alphaType(),
1876 this->colorInfo().refColorSpace());
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001877 auto result = std::make_unique<AsyncReadResult>(0);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001878 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1879 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1880 result->addCpuPlane(std::move(data), pm.rowBytes());
1881
Brian Salomon1d435302019-07-01 13:05:28 -04001882 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001883 callback(context, nullptr);
Brian Salomon4bb50262020-02-06 16:11:31 -05001884 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001885 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001886 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001887 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001888 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001889
Brian Salomonab32f652019-05-10 14:24:50 -04001890 struct FinishContext {
1891 ReadPixelsCallback* fClientCallback;
1892 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001893 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001894 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001895 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001896 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001897 };
1898 // Assumption is that the caller would like to flush. We could take a parameter or require an
1899 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1900 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001901 auto* finishContext = new FinishContext{callback,
1902 context,
1903 rect.size(),
1904 colorType,
1905 mappedBufferManager,
1906 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001907 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001908 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001909 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001910 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1911 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1912 context->fMappedBufferManager)) {
1913 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001914 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001915 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001916 delete context;
1917 };
1918 GrFlushInfo flushInfo;
1919 flushInfo.fFinishedContext = finishContext;
1920 flushInfo.fFinishedProc = finishCallback;
1921 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1922}
1923
Brian Salomon9241a6d2019-10-03 13:26:54 -04001924void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1925 sk_sp<SkColorSpace> dstColorSpace,
1926 const SkIRect& srcRect,
Brian Salomona56a7462020-02-07 14:17:25 -05001927 SkISize dstSize,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001928 RescaleGamma rescaleGamma,
1929 SkFilterQuality rescaleQuality,
1930 ReadPixelsCallback callback,
1931 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001932 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1933 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001934 SkASSERT(!dstSize.isZero());
1935 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1936
Brian Salomon024bd002019-06-11 11:38:16 -04001937 auto direct = fContext->priv().asDirectContext();
1938 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001939 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001940 return;
1941 }
Greg Daniel46e366a2019-12-16 14:38:36 -05001942 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001943 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001944 return;
1945 }
Stephen White3c0a50f2020-01-16 18:19:54 -05001946 if (this->asRenderTargetProxy()->framebufferOnly()) {
1947 callback(context, nullptr);
1948 return;
1949 }
Greg Danielca9dbe22020-01-02 13:44:30 -05001950 if (this->asSurfaceProxy()->isProtected() == GrProtected::kYes) {
Emircan Uysaler76974762019-10-25 13:28:54 -04001951 callback(context, nullptr);
1952 return;
1953 }
Brian Salomon024bd002019-06-11 11:38:16 -04001954 int x = srcRect.fLeft;
1955 int y = srcRect.fTop;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001956 bool needsRescale = srcRect.size() != dstSize;
Greg Daniel40903af2020-01-30 14:55:05 -05001957 GrSurfaceProxyView srcView;
Brian Salomon024bd002019-06-11 11:38:16 -04001958 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001959 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001960 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001961 dstColorSpace);
1962 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001963 auto tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001964 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001965 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001966 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001967 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001968 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001969 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001970 x = y = 0;
Greg Daniel40903af2020-01-30 14:55:05 -05001971 srcView = tempRTC->readSurfaceView();
Brian Salomon024bd002019-06-11 11:38:16 -04001972 } else {
Greg Daniel40903af2020-01-30 14:55:05 -05001973 srcView = this->readSurfaceView();
1974 if (!srcView.asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -04001975 srcView = GrSurfaceProxyView::Copy(fContext, std::move(srcView), GrMipMapped::kNo,
1976 srcRect, SkBackingFit::kApprox, SkBudgeted::kYes);
1977 if (!srcView) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001978 // If we can't get a texture copy of the contents then give up.
1979 callback(context, nullptr);
1980 return;
1981 }
Brian Salomonc5243782020-04-02 12:50:34 -04001982 SkASSERT(srcView.asTextureProxy());
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001983 x = y = 0;
1984 }
Brian Salomon4d036892019-07-02 15:10:58 -04001985 // We assume the caller wants kPremul. There is no way to indicate a preference.
1986 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001987 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1988 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001989 if (xform) {
Brian Salomona7e5c7c2020-01-27 15:41:40 -05001990 SkRect srcRectToDraw = SkRect::MakeXYWH(x, y, srcRect.width(), srcRect.height());
1991 auto tempRTC = GrRenderTargetContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -05001992 direct, this->colorInfo().colorType(), dstColorSpace, SkBackingFit::kApprox,
1993 dstSize, 1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001994 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001995 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001996 return;
1997 }
Greg Daniel40903af2020-01-30 14:55:05 -05001998 tempRTC->drawTexture(GrNoClip(), std::move(srcView), this->colorInfo().alphaType(),
1999 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
2000 SK_PMColor4fWHITE, srcRectToDraw, SkRect::Make(srcRect.size()),
2001 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
2002 SkMatrix::I(), std::move(xform));
2003 srcView = tempRTC->readSurfaceView();
2004 SkASSERT(srcView.asTextureProxy());
Brian Salomon024bd002019-06-11 11:38:16 -04002005 x = y = 0;
2006 }
2007 }
Greg Danielc594e622019-10-15 14:01:49 -04002008
Greg Daniele20fcad2020-01-08 11:52:34 -05002009 auto yRTC = GrRenderTargetContext::MakeWithFallback(
2010 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, dstSize, 1,
2011 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon9241a6d2019-10-03 13:26:54 -04002012 int halfW = dstSize.width()/2;
2013 int halfH = dstSize.height()/2;
Greg Daniele20fcad2020-01-08 11:52:34 -05002014 auto uRTC = GrRenderTargetContext::MakeWithFallback(
2015 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2016 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
2017 auto vRTC = GrRenderTargetContext::MakeWithFallback(
2018 direct, GrColorType::kAlpha_8, dstColorSpace, SkBackingFit::kApprox, {halfW, halfH}, 1,
2019 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002020 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002021 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002022 return;
2023 }
2024
Brian Osman28ba5282019-10-30 14:18:07 -04002025 float baseM[20];
2026 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
2027
Brian Salomon024bd002019-06-11 11:38:16 -04002028 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2029
2030 auto texMatrix = SkMatrix::MakeTrans(x, y);
2031
Brian Salomon9241a6d2019-10-03 13:26:54 -04002032 SkRect dstRectY = SkRect::Make(dstSize);
2033 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00002034
Brian Salomon6aa65052020-01-28 12:16:53 -05002035 bool doSynchronousRead = !this->caps()->transferFromSurfaceToBufferSupport();
2036 PixelTransferResult yTransfer, uTransfer, vTransfer;
2037
Brian Salomon024bd002019-06-11 11:38:16 -04002038 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2039 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002040 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002041 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002042 GrPaint yPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002043 yPaint.addColorFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -05002044 GrTextureEffect::Make(srcView, this->colorInfo().alphaType(), texMatrix));
Brian Salomon024bd002019-06-11 11:38:16 -04002045 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2046 yPaint.addColorFragmentProcessor(std::move(yFP));
2047 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002048 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2049 dstRectY, dstRectY);
Brian Salomon6aa65052020-01-28 12:16:53 -05002050 if (!doSynchronousRead) {
2051 yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2052 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2053 if (!yTransfer.fTransferBuffer) {
2054 callback(context, nullptr);
2055 return;
2056 }
Brian Salomon024bd002019-06-11 11:38:16 -04002057 }
2058
2059 texMatrix.preScale(2.f, 2.f);
2060 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2061 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002062 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002063 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002064 GrPaint uPaint;
Brian Salomonbfb72112020-01-13 10:51:50 -05002065 uPaint.addColorFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -05002066 srcView, this->colorInfo().alphaType(), texMatrix, GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002067 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2068 uPaint.addColorFragmentProcessor(std::move(uFP));
2069 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002070 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2071 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002072 if (!doSynchronousRead) {
2073 uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2074 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2075 if (!uTransfer.fTransferBuffer) {
2076 callback(context, nullptr);
2077 return;
2078 }
Brian Salomon024bd002019-06-11 11:38:16 -04002079 }
2080
2081 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2082 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002083 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04002084 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04002085 GrPaint vPaint;
Greg Danield2ccbb52020-02-05 10:45:39 -05002086 vPaint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(srcView),
2087 this->colorInfo().alphaType(), texMatrix,
2088 GrSamplerState::Filter::kBilerp));
Brian Salomon024bd002019-06-11 11:38:16 -04002089 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2090 vPaint.addColorFragmentProcessor(std::move(vFP));
2091 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002092 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2093 dstRectUV, dstRectUV);
Brian Salomon6aa65052020-01-28 12:16:53 -05002094 if (!doSynchronousRead) {
2095 vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2096 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2097 if (!vTransfer.fTransferBuffer) {
2098 callback(context, nullptr);
2099 return;
2100 }
2101 }
2102
2103 if (doSynchronousRead) {
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002104 GrImageInfo yInfo(GrColorType::kAlpha_8, kPremul_SkAlphaType, nullptr, dstSize);
2105 GrImageInfo uvInfo = yInfo.makeWH(halfW, halfH);
2106 size_t yRB = yInfo.minRowBytes();
2107 size_t uvRB = uvInfo.minRowBytes();
2108 std::unique_ptr<char[]> y(new char[yRB * yInfo.height()]);
2109 std::unique_ptr<char[]> u(new char[uvRB*uvInfo.height()]);
2110 std::unique_ptr<char[]> v(new char[uvRB*uvInfo.height()]);
2111 if (!yRTC->readPixels(yInfo, y.get(), yRB, {0, 0}, direct) ||
2112 !uRTC->readPixels(uvInfo, u.get(), uvRB, {0, 0}, direct) ||
2113 !vRTC->readPixels(uvInfo, v.get(), uvRB, {0, 0}, direct)) {
Brian Salomon6aa65052020-01-28 12:16:53 -05002114 callback(context, nullptr);
2115 return;
2116 }
2117 auto result = std::make_unique<AsyncReadResult>(direct->priv().contextID());
Brian Salomon2ea69aa2020-01-28 16:05:22 -05002118 result->addCpuPlane(std::move(y), yRB );
2119 result->addCpuPlane(std::move(u), uvRB);
2120 result->addCpuPlane(std::move(v), uvRB);
Brian Salomon6aa65052020-01-28 12:16:53 -05002121 callback(context, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04002122 return;
2123 }
2124
2125 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04002126 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04002127 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04002128 GrClientMappedBufferManager* fMappedBufferManager;
2129 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04002130 PixelTransferResult fYTransfer;
2131 PixelTransferResult fUTransfer;
2132 PixelTransferResult fVTransfer;
2133 };
2134 // Assumption is that the caller would like to flush. We could take a parameter or require an
2135 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2136 // callback to GrGpu until after the next flush that flushes our op list, though.
2137 auto* finishContext = new FinishContext{callback,
2138 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04002139 direct->priv().clientMappedBufferManager(),
2140 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04002141 std::move(yTransfer),
2142 std::move(uTransfer),
2143 std::move(vTransfer)};
2144 auto finishCallback = [](GrGpuFinishedContext c) {
2145 const auto* context = reinterpret_cast<const FinishContext*>(c);
Mike Kleinf46d5ca2019-12-11 10:45:01 -05002146 auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
Brian Salomon9241a6d2019-10-03 13:26:54 -04002147 auto manager = context->fMappedBufferManager;
2148 size_t rowBytes = SkToSizeT(context->fSize.width());
2149 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
2150 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04002151 delete context;
2152 return;
2153 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002154 rowBytes /= 2;
2155 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
2156 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
2157 (*context->fClientCallback)(context->fClientContext, nullptr);
2158 delete context;
2159 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002160 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002161 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
2162 (*context->fClientCallback)(context->fClientContext, nullptr);
2163 delete context;
2164 return;
Brian Salomon024bd002019-06-11 11:38:16 -04002165 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04002166 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04002167 delete context;
2168 };
2169 GrFlushInfo flushInfo;
2170 flushInfo.fFinishedContext = finishContext;
2171 flushInfo.fFinishedProc = finishCallback;
2172 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002173}
2174
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002175GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2176 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002177 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002178 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002179 return GrSemaphoresSubmitted::kNo;
2180 }
robertphillips8c523e02016-07-26 07:41:00 -07002181 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002182 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002183
Greg Daniel55f040b2020-02-13 15:38:32 +00002184 return this->drawingManager()->flushSurface(this->asSurfaceProxy(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002185}
2186
Greg Danielc64ee462017-06-15 16:59:49 -04002187bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002188 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002189 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002190 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002191 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002192 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002193
2194 AutoCheckFlush acf(this->drawingManager());
2195
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002196 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002197 return false;
2198 }
2199
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002200 auto direct = fContext->priv().asDirectContext();
2201 if (!direct) {
2202 return false;
2203 }
2204
2205 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002206
Greg Daniel301015c2019-11-18 14:06:46 -05002207 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
2208 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002209 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002210 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002211 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2212 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002213 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002214 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2215 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002216 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002217}
joshualitt33a5fce2015-11-18 13:28:51 -08002218
Brian Osman11052242016-10-27 14:47:55 -04002219void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002220 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002221 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002222 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002223 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002224 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002225 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002226 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002227 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002228 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2229
Brian Salomon40b77a62017-12-22 11:25:52 -05002230 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002231
Robert Phillips27927a52018-08-20 13:18:12 -04002232 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002233}
2234
2235void GrRenderTargetContext::drawShape(const GrClip& clip,
2236 GrPaint&& paint,
2237 GrAA aa,
2238 const SkMatrix& viewMatrix,
2239 const GrShape& shape) {
2240 ASSERT_SINGLE_OWNER
2241 RETURN_IF_ABANDONED
2242 SkDEBUGCODE(this->validate();)
2243 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2244
Brian Salomon2fad74a2017-12-20 13:28:55 -05002245 if (shape.isEmpty()) {
2246 if (shape.inverseFilled()) {
2247 this->drawPaint(clip, std::move(paint), viewMatrix);
2248 }
2249 return;
robertphillipsea461502015-05-26 11:38:03 -07002250 }
2251
Robert Phillips72152832017-01-25 17:31:35 -05002252 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002253
Brian Salomon2fad74a2017-12-20 13:28:55 -05002254 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002255 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002256 SkRRect rrect;
2257 // We can ignore the starting point and direction since there is no path effect.
2258 bool inverted;
2259 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2260 if (rrect.isRect()) {
2261 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2262 &shape.style());
2263 return;
2264 } else if (rrect.isOval()) {
2265 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002266 return;
2267 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002268 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2269 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002270 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2271 viewMatrix.rectStaysRect()) {
2272 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2273 // the matrix to all the points individually rather than just to the rect
2274 SkRect rects[2];
2275 if (shape.asNestedRects(rects)) {
2276 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002277 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002278 fContext, std::move(paint), viewMatrix, rects);
2279 if (op) {
2280 this->addDrawOp(clip, std::move(op));
2281 }
2282 // Returning here indicates that there is nothing to draw in this case.
2283 return;
2284 }
robertphillipsea461502015-05-26 11:38:03 -07002285 }
2286 }
robertphillips4bc31812016-03-01 12:22:49 -08002287
Brian Salomon2fad74a2017-12-20 13:28:55 -05002288 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002289}
2290
Chris Daltonbbfd5162017-11-07 13:35:22 -07002291bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002292 const GrUserStencilSettings* ss,
2293 SkRegion::Op op,
2294 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002295 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002296 const SkMatrix& viewMatrix,
2297 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002298 ASSERT_SINGLE_OWNER_PRIV
2299 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002300 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002301 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2302 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002303
2304 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002305 GrPaint paint;
2306 paint.setCoverageSetOpXPFactory(op, invert);
2307 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2308 SkRect::MakeIWH(fRenderTargetContext->width(),
2309 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002310 return true;
2311 }
2312
Robert Phillips72152832017-01-25 17:31:35 -05002313 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002314
2315 // An Assumption here is that path renderer would use some form of tweaking
2316 // the src color (either the input alpha or in the frag shader) to implement
2317 // aa. If we have some future driver-mojo path AA that can do the right
2318 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002319 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002320 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002321
Chris Daltondb91c6e2017-09-08 16:25:08 -06002322 SkIRect clipConservativeBounds;
2323 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2324 &clipConservativeBounds, nullptr);
2325
bsalomon8acedde2016-06-24 10:42:16 -07002326 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002327 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002328 canDrawArgs.fCaps = fRenderTargetContext->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002329 canDrawArgs.fProxy = fRenderTargetContext->asRenderTargetProxy();
robertphillips391395d2016-03-02 09:26:36 -08002330 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002331 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002332 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002333 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002334 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2335 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002336 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002337
2338 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002339 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002340 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002341 if (!pr) {
2342 return false;
2343 }
2344
2345 GrPaint paint;
2346 paint.setCoverageSetOpXPFactory(op, invert);
2347
Brian Salomonf3569f02017-10-24 12:52:33 -04002348 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2349 std::move(paint),
2350 ss,
2351 fRenderTargetContext,
2352 &clip,
2353 &clipConservativeBounds,
2354 &viewMatrix,
2355 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002356 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002357 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002358 pr->drawPath(args);
2359 return true;
2360}
2361
Brian Osman11052242016-10-27 14:47:55 -04002362SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002363 ASSERT_SINGLE_OWNER_PRIV
2364
Robert Phillips6a6de562019-02-15 15:19:15 -05002365 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002366 return SkBudgeted::kNo;
2367 }
2368
Brian Osman11052242016-10-27 14:47:55 -04002369 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002370
Greg Daniel3912a4b2020-01-14 09:56:04 -05002371 return fRenderTargetContext->asSurfaceProxy()->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002372}
2373
Brian Salomon2fad74a2017-12-20 13:28:55 -05002374void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2375 GrPaint&& paint,
2376 GrAA aa,
2377 const SkMatrix& viewMatrix,
2378 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002379 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002380 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002381 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2382
Jim Van Verthf86073a2018-10-02 13:05:38 -04002383 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2384 return;
2385 }
2386
Chris Daltondb91c6e2017-09-08 16:25:08 -06002387 SkIRect clipConservativeBounds;
2388 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2389
Brian Salomon2fad74a2017-12-20 13:28:55 -05002390 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002391 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002392
robertphillips68737822015-10-29 12:12:21 -07002393 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002394 canDrawArgs.fCaps = this->caps();
Greg Daniel46e366a2019-12-16 14:38:36 -05002395 canDrawArgs.fProxy = this->asRenderTargetProxy();
robertphillips68737822015-10-29 12:12:21 -07002396 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002397 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002398 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002399 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002400 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002401
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002402 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002403 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002404 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002405 return;
2406 }
2407
Chris Dalton6ce447a2019-06-23 18:07:38 -06002408 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002409
2410 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2411 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2412 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2413
Brian Salomon2fad74a2017-12-20 13:28:55 -05002414 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002415 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002416 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2417 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002418 return;
2419 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002420 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002421 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002422 }
2423 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002424 if (canDrawArgs.fShape->style().applies()) {
2425 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2426 styleScale);
2427 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002428 return;
2429 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002430 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002431 // This time, allow SW renderer
2432 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2433 } else {
2434 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002435 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002436 }
robertphillipsea461502015-05-26 11:38:03 -07002437
bsalomon8acedde2016-06-24 10:42:16 -07002438 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002439#ifdef SK_DEBUG
2440 SkDebugf("Unable to find path renderer compatible with path.\n");
2441#endif
2442 return;
2443 }
2444
Robert Phillips256c37b2017-03-01 14:32:46 -05002445 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002446 std::move(paint),
2447 &GrUserStencilSettings::kUnused,
2448 this,
2449 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002450 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002451 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002452 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002453 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002454 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002455 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002456}
2457
Brian Salomon467921e2017-03-06 16:17:12 -05002458static void op_bounds(SkRect* bounds, const GrOp* op) {
2459 *bounds = op->bounds();
2460 if (op->hasZeroArea()) {
2461 if (op->hasAABloat()) {
2462 bounds->outset(0.5f, 0.5f);
2463 } else {
2464 // We don't know which way the particular GPU will snap lines or points at integer
2465 // coords. So we ensure that the bounds is large enough for either snap.
2466 SkRect before = *bounds;
2467 bounds->roundOut(bounds);
2468 if (bounds->fLeft == before.fLeft) {
2469 bounds->fLeft -= 1;
2470 }
2471 if (bounds->fTop == before.fTop) {
2472 bounds->fTop -= 1;
2473 }
2474 if (bounds->fRight == before.fRight) {
2475 bounds->fRight += 1;
2476 }
2477 if (bounds->fBottom == before.fBottom) {
2478 bounds->fBottom += 1;
2479 }
2480 }
2481 }
2482}
2483
Chris Dalton08755122019-08-05 16:13:47 -06002484void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002485 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002486 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2487}
2488
Brian Salomon348a0372018-10-31 10:42:18 -04002489void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2490 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002491 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002492 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002493 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002494 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002495 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002496 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002497 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002498 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002499
Brian Salomon467921e2017-03-06 16:17:12 -05002500 // Setup clip
2501 SkRect bounds;
2502 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002503 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002504 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002505 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002506 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002507
Chris Dalton9acf6822019-11-12 11:52:40 -07002508 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002509 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002510 }
2511
Chris Dalton9acf6822019-11-12 11:52:40 -07002512 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002513 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002514 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002515 }
2516
Chris Dalton9acf6822019-11-12 11:52:40 -07002517 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2518 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2519
2520 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2521 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2522 // that uses a stencil test when the stencil buffer is multisampled.)
2523 bool hasMixedSampledCoverage = (
2524 willUseStencil && fNumStencilSamples > this->numSamples());
Greg Daniel46e366a2019-12-16 14:38:36 -05002525 SkASSERT(!hasMixedSampledCoverage ||
2526 this->asRenderTargetProxy()->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002527
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002528 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002529 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002530 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2531
Greg Daniel524e28b2019-11-01 11:48:53 -04002532 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002533 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002534 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002535 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002536 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002537 }
2538 }
2539
2540 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002541 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002542 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002543 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002544 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002545 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002546 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002547}
2548
Greg Daniel524e28b2019-11-01 11:48:53 -04002549bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2550 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002551 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2552 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2553 // start and stop the render pass in order to make the copy.
Greg Daniel46e366a2019-12-16 14:38:36 -05002554 if (this->asRenderTargetProxy()->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002555 return false;
2556 }
2557
Greg Daniel3912a4b2020-01-14 09:56:04 -05002558 if (this->caps()->textureBarrierSupport() &&
2559 !this->asSurfaceProxy()->requiresManualMSAAResolve()) {
Greg Daniel46e366a2019-12-16 14:38:36 -05002560 if (this->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002561 // The render target is a texture, so we can read from it directly in the shader. The XP
2562 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel46e366a2019-12-16 14:38:36 -05002563 dstProxyView->setProxyView(this->readSurfaceView());
Greg Daniel524e28b2019-11-01 11:48:53 -04002564 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002565 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002566 }
2567 }
2568
Greg Daniel3912a4b2020-01-14 09:56:04 -05002569 SkIRect copyRect = SkIRect::MakeSize(this->asSurfaceProxy()->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002570
Eric Karl74480882017-04-03 14:49:05 -07002571 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002572 clip.getConservativeBounds(
Greg Daniel46e366a2019-12-16 14:38:36 -05002573 this->width(), this->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002574 SkRect opBounds = op.bounds();
2575 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2576 // 0.5 pixels.
2577 if (op.hasAABloat() || op.hasZeroArea()) {
2578 opBounds.outset(0.5f, 0.5f);
2579 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2580 // performance we may ignore the clip when the draw is entirely inside the clip is float
2581 // space but will hit pixels just outside the clip when actually rasterizing.
2582 clippedRect.outset(1, 1);
Greg Daniel3912a4b2020-01-14 09:56:04 -05002583 clippedRect.intersect(SkIRect::MakeSize(this->asSurfaceProxy()->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002584 }
2585 SkIRect opIBounds;
2586 opBounds.roundOut(&opIBounds);
2587 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002588#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002589 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002590#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002591 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002592 }
2593
Greg Daniel3155f7f2020-01-16 16:54:26 -05002594 GrColorType colorType = this->colorInfo().colorType();
Brian Salomon467921e2017-03-06 16:17:12 -05002595 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2596 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002597 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Greg Daniel3155f7f2020-01-16 16:54:26 -05002598 this->asRenderTargetProxy(), colorType);
Brian Salomon467921e2017-03-06 16:17:12 -05002599
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002600 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002601 copyRect = clippedRect;
2602 }
Brian Salomon467921e2017-03-06 16:17:12 -05002603
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002604 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002605 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002606 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002607 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002608 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002609 } else {
Eric Karl74480882017-04-03 14:49:05 -07002610 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002611 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002612 }
Brian Salomonc5243782020-04-02 12:50:34 -04002613 auto copy =
2614 GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), this->origin(), GrMipMapped::kNo,
2615 copyRect, fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
2616 SkASSERT(copy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002617
Brian Salomonc5243782020-04-02 12:50:34 -04002618 dstProxyView->setProxyView({std::move(copy), this->origin(), this->readSwizzle()});
Greg Daniel524e28b2019-11-01 11:48:53 -04002619 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002620 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002621}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002622
Greg Daniel573312e2020-02-07 17:22:35 -05002623bool GrRenderTargetContext::blitTexture(GrSurfaceProxyView view, const SkIRect& srcRect,
Brian Salomonfc118442019-11-22 19:09:27 -05002624 const SkIPoint& dstPoint) {
Greg Daniel573312e2020-02-07 17:22:35 -05002625 SkASSERT(view.asTextureProxy());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002626 SkIRect clippedSrcRect;
2627 SkIPoint clippedDstPoint;
Greg Daniel573312e2020-02-07 17:22:35 -05002628 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), view.proxy()->dimensions(),
2629 srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002630 return false;
2631 }
2632
2633 GrPaint paint;
2634 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Greg Danield2ccbb52020-02-05 10:45:39 -05002635
Greg Danield2ccbb52020-02-05 10:45:39 -05002636 auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002637 if (!fp) {
2638 return false;
2639 }
2640 paint.addColorFragmentProcessor(std::move(fp));
2641
2642 this->fillRectToRect(
2643 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2644 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2645 clippedSrcRect.height()),
2646 SkRect::Make(clippedSrcRect));
2647 return true;
2648}