blob: def18a3c199508e719a76c60d7f0eed34046f02a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkDrawable.h"
9#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "include/private/SkShadowFlags.h"
12#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkDrawShadowInfo.h"
16#include "src/core/SkGlyphRunPainter.h"
17#include "src/core/SkLatticeIter.h"
18#include "src/core/SkMatrixPriv.h"
19#include "src/core/SkRRectPriv.h"
20#include "src/core/SkSurfacePriv.h"
21#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrBlurUtils.h"
24#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040027#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrDrawingManager.h"
29#include "src/gpu/GrFixedClip.h"
30#include "src/gpu/GrGpuResourcePriv.h"
31#include "src/gpu/GrMemoryPool.h"
32#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040034#include "src/gpu/GrRenderTarget.h"
Mike Klein52337de2019-07-25 09:00:52 -050035#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrRenderTargetContextPriv.h"
37#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrStencilAttachment.h"
39#include "src/gpu/GrStyle.h"
40#include "src/gpu/GrTracing.h"
41#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040042#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/effects/GrRRectEffect.h"
44#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040045#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040046#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000047#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040048#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/ops/GrAtlasTextOp.h"
50#include "src/gpu/ops/GrClearOp.h"
51#include "src/gpu/ops/GrClearStencilClipOp.h"
52#include "src/gpu/ops/GrDebugMarkerOp.h"
53#include "src/gpu/ops/GrDrawAtlasOp.h"
54#include "src/gpu/ops/GrDrawOp.h"
55#include "src/gpu/ops/GrDrawVerticesOp.h"
56#include "src/gpu/ops/GrDrawableOp.h"
57#include "src/gpu/ops/GrFillRRectOp.h"
58#include "src/gpu/ops/GrFillRectOp.h"
59#include "src/gpu/ops/GrLatticeOp.h"
60#include "src/gpu/ops/GrOp.h"
61#include "src/gpu/ops/GrOvalOpFactory.h"
62#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063#include "src/gpu/ops/GrShadowRRectOp.h"
64#include "src/gpu/ops/GrStencilPathOp.h"
65#include "src/gpu/ops/GrStrokeRectOp.h"
66#include "src/gpu/ops/GrTextureOp.h"
67#include "src/gpu/text/GrTextContext.h"
68#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040069
Herb Derbyc1b482c2018-08-09 15:02:27 -040070class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040071public:
72 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040073 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Robert Phillips7e90be92019-02-15 12:22:59 -050074 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040075 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040076 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040077
Robert Phillips7c525e62018-06-12 10:11:12 -040078 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040079 fRenderTargetContext->addDrawOp(clip, std::move(op));
80 }
81
Robert Phillips46a13382018-08-23 13:53:01 -040082 void drawShape(const GrClip& clip, const SkPaint& paint,
83 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040084 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
85 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040086 }
87
88 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040089 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050090 auto context = fRenderTargetContext->fContext;
Brian Salomonf18b1d82017-10-27 11:30:49 -040091 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
92 if (kARGB_GrMaskFormat == maskFormat) {
93 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
94 } else {
95 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
96 }
97 }
98
Robert Phillips69893702019-02-22 11:16:30 -050099 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400100 return fRenderTargetContext->fContext;
101 }
102
Herb Derby65956872018-08-21 16:55:04 -0400103 SkGlyphRunListPainter* glyphPainter() override {
104 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400105 }
106
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107private:
108 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400109 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400110
Brian Salomonf18b1d82017-10-27 11:30:49 -0400111};
joshualittbc907352016-01-13 06:45:40 -0800112
Robert Phillips72152832017-01-25 17:31:35 -0500113#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800114#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400115 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800116#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400117 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500118#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
119#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
120#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
121#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
122#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700123
Brian Salomone225b562017-06-14 13:00:03 -0400124//////////////////////////////////////////////////////////////////////////////
125
robertphillipsea461502015-05-26 11:38:03 -0700126class AutoCheckFlush {
127public:
halcanary9d524f22016-03-29 09:03:52 -0700128 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700129 SkASSERT(fDrawingManager);
130 }
bsalomonb77a9072016-09-07 10:02:04 -0700131 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700132
133private:
robertphillips77a2e522015-10-17 07:43:27 -0700134 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700135};
136
Greg Danielf41b2bd2019-08-22 16:19:24 -0400137// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
138// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
139// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
140// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500141GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400142 sk_sp<GrRenderTargetProxy> rtp,
Brian Salomond6287472019-06-24 15:50:07 -0400143 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400144 sk_sp<SkColorSpace> colorSpace,
145 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400146 bool managedOpsTask)
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400147 : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace))
Brian Salomonf3569f02017-10-24 12:52:33 -0400148 , fRenderTargetProxy(std::move(rtp))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400149 , fOpsTask(sk_ref_sp(fRenderTargetProxy->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400150 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400151 , fManagedOpsTask(managedOpsTask) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400152 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700153 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700154}
155
robertphillips2e1e51f2015-10-15 08:01:48 -0700156#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400157void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400158 SkASSERT(fRenderTargetProxy);
159 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700160
Greg Danielf41b2bd2019-08-22 16:19:24 -0400161 if (fOpsTask && !fOpsTask->isClosed()) {
162 SkASSERT(fRenderTargetProxy->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700163 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700164}
165#endif
166
Brian Osman11052242016-10-27 14:47:55 -0400167GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800168 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700169}
170
Chris Dalton7d6748e2019-03-13 00:34:52 -0600171inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600172 if (GrAA::kNo == aa) {
173 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
174 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600175 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600176 return GrAAType::kMSAA;
177 }
178 return GrAAType::kNone;
179 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600180 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600181}
182
Robert Phillipsf200a902017-01-30 13:27:37 -0500183GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000184 return fRenderTargetProxy->asTextureProxy();
185}
186
Greg Daniele252f082017-10-23 16:05:23 -0400187const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
188 return fRenderTargetProxy->asTextureProxy();
189}
190
Robert Phillipsf200a902017-01-30 13:27:37 -0500191sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
192 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
193}
194
Greg Daniele252f082017-10-23 16:05:23 -0400195GrMipMapped GrRenderTargetContext::mipMapped() const {
196 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
197 return proxy->mipMapped();
198 }
199 return GrMipMapped::kNo;
200}
201
Greg Danielf41b2bd2019-08-22 16:19:24 -0400202GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800203 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700204 SkDEBUGCODE(this->validate();)
205
Greg Danielf41b2bd2019-08-22 16:19:24 -0400206 if (!fOpsTask || fOpsTask->isClosed()) {
207 fOpsTask = this->drawingManager()->newOpsTask(fRenderTargetProxy, fManagedOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700208 }
209
Greg Danielf41b2bd2019-08-22 16:19:24 -0400210 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700211}
212
Herb Derbycddab252018-07-16 11:19:04 -0400213void GrRenderTargetContext::drawGlyphRunList(
214 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400215 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800216 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700217 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700218 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400219 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700220
Greg Danielbe7fc462019-01-03 16:40:42 -0500221 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
222 // secondary command buffers because it would require stopping and starting a render pass which
223 // we don't have access to.
224 if (this->wrapsVkSecondaryCB()) {
225 return;
226 }
227
Herb Derby26cbe512018-05-24 14:39:01 -0400228 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400229 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400230 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700231}
232
Brian Osman11052242016-10-27 14:47:55 -0400233void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800234 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700235 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700236 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400237 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700238
Robert Phillips72152832017-01-25 17:31:35 -0500239 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400240
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700242}
243
Brian Osman11052242016-10-27 14:47:55 -0400244void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500245 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700246 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800247 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700248 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700249 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400250 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700251
Robert Phillips72152832017-01-25 17:31:35 -0500252 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700253 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
254 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700255}
robertphillips9199a9f2016-07-13 07:48:43 -0700256
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500257void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
258 const SkPMColor4f& color,
259 CanClearFullscreen canClearFullscreen) {
260 ASSERT_SINGLE_OWNER_PRIV
261 RETURN_IF_ABANDONED_PRIV
262 SkDEBUGCODE(fRenderTargetContext->validate();)
263 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
264 fRenderTargetContext->fContext);
265
266 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
267 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
268}
269
270static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
271 paint->setColor4f(color);
272 if (color.isOpaque()) {
273 // Can just rely on the src-over blend mode to do the right thing
274 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
275 } else {
276 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
277 // were src blended
278 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
279 }
280}
281
282void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
283 const SkPMColor4f& color,
284 CanClearFullscreen canClearFullscreen) {
285 bool isFull = false;
286 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400287 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
288 // only clear the entire target if we knew it had not been cleared before. As
289 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500290 isFull = !clip.scissorEnabled() ||
291 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400292 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500293 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
294 }
295
296 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400297 GrOpsTask* opsTask = this->getOpsTask();
298 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500299 !this->caps()->performColorClearsAsDraws()) {
300 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400301 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500302 return;
303 } else {
304 // Will use an op for the clear, reset the load op to discard since the op will
305 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400306 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500307 }
308
309 // Must add an op to the list (either because we couldn't use a load op, or because the
310 // clear load op isn't supported)
311 if (this->caps()->performColorClearsAsDraws()) {
312 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
313 GrPaint paint;
314 clear_to_grpaint(color, &paint);
315 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400316 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
317 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500318 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600319 this->addOp(GrClearOp::Make(
320 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500321 }
322 } else {
323 if (this->caps()->performPartialClearsAsDraws()) {
324 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500325 GrPaint paint;
326 clear_to_grpaint(color, &paint);
327
Michael Ludwig64b28a72019-05-28 12:02:00 -0400328 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400329 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
330 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500331 } else {
332 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
333 this->asSurfaceProxy()));
334 // This version of the clear op factory can return null if the clip doesn't intersect
335 // with the surface proxy's boundary
336 if (!op) {
337 return;
338 }
Chris Dalton08755122019-08-05 16:13:47 -0600339 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500340 }
341 }
342}
343
Brian Osman11052242016-10-27 14:47:55 -0400344void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500345 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400346 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000347 // Start with the render target, since that is the maximum content we could possibly fill.
348 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400349 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400350 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000351 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
352 // inverse view matrix.
353 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
354 } else {
355 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
356 SkMatrix localMatrix;
357 if (!viewMatrix.invert(&localMatrix)) {
358 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400359 }
Michael Ludwig61328202019-06-19 14:48:58 +0000360 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
361 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700362 }
robertphillipsea461502015-05-26 11:38:03 -0700363}
364
Michael Ludwig61328202019-06-19 14:48:58 +0000365enum class GrRenderTargetContext::QuadOptimization {
366 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
367 kDiscarded,
368 // The rect to draw was converted to some other op and appended to the oplist, so no additional
369 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
370 // a constColor is provided.
371 kSubmitted,
372 // The clip was folded into the device quad, with updated edge flags and local coords, and
373 // caller is responsible for adding an appropriate op.
374 kClipApplied,
375 // No change to clip, but quad updated to better fit clip/render target, and caller is
376 // responsible for adding an appropriate op.
377 kCropped
378};
Michael Ludwig61a16512019-01-15 11:15:13 -0500379
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400380static bool make_vertex_finite(float* value) {
381 if (SkScalarIsNaN(*value)) {
382 return false;
383 }
384
385 if (!SkScalarIsFinite(*value)) {
386 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
387 // left when calculating crops.
388 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
389 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
390 }
391
392 return true;
393}
394
Michael Ludwig61328202019-06-19 14:48:58 +0000395GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400396 const GrClip& clip, const SkPMColor4f* constColor,
397 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
398 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000399 // Optimization requirements:
400 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
401 // 2. kClear applies when constColor and final geom is pixel aligned rect;
402 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
403 // 3. kRRect applies when constColor and rrect clip and quad covers clip
404 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
405 // 5. kCropped applies when rect quad (currently)
406 // 6. kNone always applies
407 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000408
Michael Ludwige08b4432019-06-19 18:00:48 -0400409 SkRect rtRect;
410 if (stencilSettings) {
411 // Must use worst case bounds so that stencil buffer updates on approximately sized render
412 // targets don't get corrupted.
413 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
414 fRenderTargetProxy->worstCaseHeight());
415 } else {
416 // Use the logical size of the render target, which allows for "fullscreen" clears even if
417 // the render target has an approximate backing fit
418 rtRect = SkRect::MakeWH(this->width(), this->height());
419 }
420
Michael Ludwig61328202019-06-19 14:48:58 +0000421 SkRect drawBounds = deviceQuad->bounds();
422 if (constColor) {
423 // Don't bother updating local coordinates when the paint will ignore them anyways
424 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400425 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
426 // will be cropped to the finite 'clip' or render target and there is no local space mapping
427 if (!deviceQuad->isFinite()) {
428 for (int i = 0; i < 4; ++i) {
429 if (!make_vertex_finite(deviceQuad->xs() + i) ||
430 !make_vertex_finite(deviceQuad->ys() + i) ||
431 !make_vertex_finite(deviceQuad->ws() + i)) {
432 // Discard if we see a nan
433 return QuadOptimization::kDiscarded;
434 }
435 }
436 SkASSERT(deviceQuad->isFinite());
437 }
438 } else {
439 // CropToRect requires the quads to be finite. If they are not finite and we have local
440 // coordinates, the mapping from local space to device space is poorly defined so drop it
441 if (!deviceQuad->isFinite()) {
442 return QuadOptimization::kDiscarded;
443 }
Brian Salomon7694b902019-06-18 21:00:21 +0000444 }
445
Michael Ludwig61328202019-06-19 14:48:58 +0000446 // If the quad is entirely off screen, it doesn't matter what the clip does
447 if (!rtRect.intersects(drawBounds)) {
448 return QuadOptimization::kDiscarded;
449 }
Brian Salomon7694b902019-06-18 21:00:21 +0000450
Michael Ludwig61328202019-06-19 14:48:58 +0000451 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
452 // the render target).
453 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400454 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
455 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
456 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
457 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000458 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000459 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000460 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
461 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000462 }
Brian Salomon7694b902019-06-18 21:00:21 +0000463 }
464
Michael Ludwig61328202019-06-19 14:48:58 +0000465 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
466 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400467 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000468 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
469 // of the render target and the clip rect)
470 SkRect clipBounds = rtRect;
471 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
472 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000473 }
474
Michael Ludwig61328202019-06-19 14:48:58 +0000475 if (clipRRect.isRect()) {
476 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
477 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
478 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
479 // Clear optimization is possible
480 drawBounds = deviceQuad->bounds();
481 if (drawBounds.contains(rtRect)) {
482 // Fullscreen clear
483 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
484 return QuadOptimization::kSubmitted;
485 } else if (GrClip::IsPixelAligned(drawBounds) &&
486 drawBounds.width() > 256 && drawBounds.height() > 256) {
487 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
488 SkIRect scissorRect;
489 drawBounds.round(&scissorRect);
490 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
491 return QuadOptimization::kSubmitted;
492 }
493 }
494
495 // Update overall AA setting.
496 *edgeFlags = newFlags;
497 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
498 newFlags != GrQuadAAFlags::kNone) {
499 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
500 // properly reflect the smooth edge of the clip.
501 *aa = GrAA::kYes;
502 }
503 // We intentionally do not downgrade AA here because we don't know if we need to
504 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
505 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
506
507 // deviceQuad is exactly the intersection of original quad and clip, so it can be
508 // drawn with no clip (submitted by caller)
509 return QuadOptimization::kClipApplied;
510 } else {
511 // The quads have been updated to better fit the clip bounds, but can't get rid of
512 // the clip entirely
513 return QuadOptimization::kCropped;
514 }
515 } else if (constColor) {
516 // Rounded corners and constant filled color (limit ourselves to solid colors because
517 // there is no way to use custom local coordinates with drawRRect).
518 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
519 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
520 deviceQuad->bounds().contains(clipBounds)) {
521 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
522 // we can draw the rrect directly and ignore the edge flags
523 GrPaint paint;
524 clear_to_grpaint(*constColor, &paint);
525 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
526 clipRRect, GrStyle::SimpleFill());
527 return QuadOptimization::kSubmitted;
528 } else {
529 // The quad has been updated to better fit clip bounds, but can't remove the clip
530 return QuadOptimization::kCropped;
531 }
Brian Salomon7694b902019-06-18 21:00:21 +0000532 }
Brian Salomon7694b902019-06-18 21:00:21 +0000533 }
534
Michael Ludwig61328202019-06-19 14:48:58 +0000535 // Crop the quad to the conservative bounds of the clip.
536 SkIRect clipDevBounds;
537 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
538 SkRect clipBounds = SkRect::Make(clipDevBounds);
539
540 // One final check for discarding, since we may have gone here directly due to a complex clip
541 if (!clipBounds.intersects(drawBounds)) {
542 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000543 }
544
Michael Ludwig61328202019-06-19 14:48:58 +0000545 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
546 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
547 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
548
549 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000550}
551
Michael Ludwig61328202019-06-19 14:48:58 +0000552void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500553 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500554 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000555 GrQuadAAFlags edgeFlags,
556 const GrQuad& deviceQuad,
557 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400558 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000559 ASSERT_SINGLE_OWNER
560 RETURN_IF_ABANDONED
561 SkDEBUGCODE(this->validate();)
562 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500563
Michael Ludwig61328202019-06-19 14:48:58 +0000564 AutoCheckFlush acf(this->drawingManager());
565
566 SkPMColor4f* constColor = nullptr;
567 SkPMColor4f paintColor;
568 if (!ss && !paint.numCoverageFragmentProcessors() &&
569 paint.isConstantBlendedColor(&paintColor)) {
570 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
571 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500572 }
573
Michael Ludwig61328202019-06-19 14:48:58 +0000574 GrQuad croppedDeviceQuad = deviceQuad;
575 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400576 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
577 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000578 if (opt >= QuadOptimization::kClipApplied) {
579 // These optimizations require caller to add an op themselves
580 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
581 : clip;
582 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
583 : this->chooseAAType(aa);
584 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
585 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700586 }
Michael Ludwig61328202019-06-19 14:48:58 +0000587 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800588}
589
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000590void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
591 sk_sp<GrTextureProxy> proxy,
592 sk_sp<GrColorSpaceXform> textureXform,
593 GrSamplerState::Filter filter,
594 const SkPMColor4f& color,
595 SkBlendMode blendMode,
596 GrAA aa,
597 GrQuadAAFlags edgeFlags,
598 const GrQuad& deviceQuad,
599 const GrQuad& localQuad,
600 const SkRect* domain) {
601 ASSERT_SINGLE_OWNER
602 RETURN_IF_ABANDONED
603 SkDEBUGCODE(this->validate();)
604 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
605
606 AutoCheckFlush acf(this->drawingManager());
607
608 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
609 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
610 GrQuad croppedDeviceQuad = deviceQuad;
611 GrQuad croppedLocalQuad = localQuad;
612 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
613 &croppedDeviceQuad, &croppedLocalQuad);
614
615 SkASSERT(opt != QuadOptimization::kSubmitted);
616 if (opt != QuadOptimization::kDiscarded) {
617 // And the texture op if not discarded
618 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
619 : clip;
620 GrAAType aaType = this->chooseAAType(aa);
621 // Use the provided domain, although hypothetically we could detect that the cropped local
622 // quad is sufficiently inside the domain and the constraint could be dropped.
623 this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy),
624 std::move(textureXform), filter, color,
625 blendMode, aaType, edgeFlags,
626 croppedDeviceQuad, croppedLocalQuad, domain));
627 }
628}
629
Brian Osman11052242016-10-27 14:47:55 -0400630void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500631 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500632 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400633 const SkMatrix& viewMatrix,
634 const SkRect& rect,
635 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700636 if (!style) {
637 style = &GrStyle::SimpleFill();
638 }
joshualitt1de610a2016-01-06 08:26:09 -0800639 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700640 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700641 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400642 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700643
bsalomon6663acf2016-05-10 09:14:17 -0700644 // Path effects should've been devolved to a path in SkGpuDevice
645 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700646
Robert Phillips72152832017-01-25 17:31:35 -0500647 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700648
bsalomon6663acf2016-05-10 09:14:17 -0700649 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400650 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000651 // Fills the rect, using rect as its own local coordinates
652 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500653 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700654 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
655 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
656 if ((!rect.width() || !rect.height()) &&
657 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
658 SkScalar r = stroke.getWidth() / 2;
659 // TODO: Move these stroke->fill fallbacks to GrShape?
660 switch (stroke.getJoin()) {
661 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500662 this->drawRect(
663 clip, std::move(paint), aa, viewMatrix,
664 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
665 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700666 return;
667 case SkPaint::kRound_Join:
668 // Raster draws nothing when both dimensions are empty.
669 if (rect.width() || rect.height()){
670 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500671 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
672 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700673 return;
674 }
675 case SkPaint::kBevel_Join:
676 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500677 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700678 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
679 &GrStyle::SimpleFill());
680 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500681 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700682 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
683 &GrStyle::SimpleFill());
684 }
685 return;
686 }
687 }
robertphillips44302392016-07-08 14:43:03 -0700688
Brian Salomonbaaf4392017-06-15 09:59:23 -0400689 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700690
Chris Dalton7d6748e2019-03-13 00:34:52 -0600691 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500692 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
693 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
694 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500695 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400696 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700697 return;
robertphillips4bc31812016-03-01 12:22:49 -0800698 }
robertphillips4bc31812016-03-01 12:22:49 -0800699 }
Mike Klein16885072018-12-11 09:54:31 -0500700 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500701 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700702}
703
Michael Ludwig69858532018-11-28 15:34:34 -0500704void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
705 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
706 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600707 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500708 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
709 quads, cnt));
710}
711
Robert Phillipsec2249f2016-11-09 08:54:35 -0500712int GrRenderTargetContextPriv::maxWindowRectangles() const {
713 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400714 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500715}
716
Greg Danielf41b2bd2019-08-22 16:19:24 -0400717GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600718 ) const {
719#if GR_TEST_UTILS
720 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400721 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600722 }
723#endif
724 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
725 // would normally be overwritten. The one exception is if the render target context is marked as
726 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
727 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
728 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
729 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400730 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600731}
732
Chris Daltoneffee202019-07-01 22:28:03 -0600733void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
734 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600735 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600736 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600737
Chris Daltoneffee202019-07-01 22:28:03 -0600738 int numRequiredSamples = this->numSamples();
739 if (multisampled && 1 == numRequiredSamples) {
740 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
741 // mixed samples.
742 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
743 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400744 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600745 }
746 SkASSERT(numRequiredSamples > 0);
747
748 if (numRequiredSamples > fNumStencilSamples) {
749 fNumStencilSamples = numRequiredSamples;
750 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
751 }
Chris Dalton6b982802019-06-27 13:53:46 -0600752
753 if (needsStencilClear) {
754 if (this->caps()->performStencilClearsAsDraws()) {
755 // There is a driver bug with clearing stencil. We must use an op to manually clear the
756 // stencil buffer before the op that required 'setNeedsStencil'.
757 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
758 } else {
759 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
760 // code note when the instantiated stencil buffer is already clear and skip the clear
761 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
762 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400763 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600764 }
765 }
766}
767
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000768void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700769 ASSERT_SINGLE_OWNER_PRIV
770 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400771 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400772 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
773 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700774
Robert Phillips72152832017-01-25 17:31:35 -0500775 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400776
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500777 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
778}
779
780void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
781 if (this->caps()->performStencilClearsAsDraws()) {
782 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
783 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
784
785 // Configure the paint to have no impact on the color buffer
786 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400787 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400788 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
789 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500790 } else {
791 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
792 fRenderTargetProxy.get()));
793 if (!op) {
794 return;
795 }
Chris Dalton08755122019-08-05 16:13:47 -0600796 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500797 }
robertphillips976f5f02016-06-03 10:59:20 -0700798}
799
Chris Daltonbbfd5162017-11-07 13:35:22 -0700800void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600801 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400802 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400803 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500804 ASSERT_SINGLE_OWNER_PRIV
805 RETURN_IF_ABANDONED_PRIV
806 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400807 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
808 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500809
Brian Salomon467921e2017-03-06 16:17:12 -0500810 // TODO: extract portions of checkDraw that are relevant to path stenciling.
811 SkASSERT(path);
812 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
813
814 // FIXME: Use path bounds instead of this WAR once
815 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
816 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
817
818 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700819 GrAppliedHardClip appliedClip;
820 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
821 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500822 return;
823 }
824
Robert Phillips7c525e62018-06-12 10:11:12 -0400825 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
826 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600827 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500828 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500829 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400830 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400831 if (!op) {
832 return;
833 }
Brian Salomon97180af2017-03-14 13:42:58 -0400834 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600835
Chris Daltoneffee202019-07-01 22:28:03 -0600836 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600837 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700838}
839
Brian Salomond7065e72018-10-12 11:42:02 -0400840void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500841 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400842 GrAA aa, SkCanvas::SrcRectConstraint constraint,
843 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500844 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400845 ASSERT_SINGLE_OWNER
846 RETURN_IF_ABANDONED
847 SkDEBUGCODE(this->validate();)
848 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500849
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500850 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500851 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400852 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
853 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500854 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500855 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500856 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500857 ctm = viewMatrix;
858 if (set[i].fPreViewMatrix) {
859 ctm.preConcat(*set[i].fPreViewMatrix);
860 }
861
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000862 GrQuad quad, srcQuad;
863 if (set[i].fDstClipQuad) {
864 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
865
866 SkPoint srcPts[4];
867 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
868 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500869 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000870 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
871 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500872 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000873
874 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
875 ? &set[i].fSrcRect : nullptr;
876 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
877 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
878 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500879 }
880 } else {
881 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500882 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600883 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400884 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500885 std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500886 this->addDrawOp(clip, std::move(op));
887 }
Brian Salomond7065e72018-10-12 11:42:02 -0400888}
889
Brian Osman11052242016-10-27 14:47:55 -0400890void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500891 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400892 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400893 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400894 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400895 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400896 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500897 ASSERT_SINGLE_OWNER
898 RETURN_IF_ABANDONED
899 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400900 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500901
902 AutoCheckFlush acf(this->drawingManager());
903
904 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600905 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400906 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400907 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400908 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400909 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700910}
911
912///////////////////////////////////////////////////////////////////////////////
913
Brian Osman4d92b892019-03-24 00:53:23 +0000914void GrRenderTargetContext::drawAtlas(const GrClip& clip,
915 GrPaint&& paint,
916 const SkMatrix& viewMatrix,
917 int spriteCount,
918 const SkRSXform xform[],
919 const SkRect texRect[],
920 const SkColor colors[]) {
921 ASSERT_SINGLE_OWNER
922 RETURN_IF_ABANDONED
923 SkDEBUGCODE(this->validate();)
924 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
925
926 AutoCheckFlush acf(this->drawingManager());
927
928 GrAAType aaType = this->chooseAAType(GrAA::kNo);
929 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
930 aaType, spriteCount, xform, texRect, colors);
931 this->addDrawOp(clip, std::move(op));
932}
933
934///////////////////////////////////////////////////////////////////////////////
935
Brian Osman11052242016-10-27 14:47:55 -0400936void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500937 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500938 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400939 const SkMatrix& viewMatrix,
940 const SkRRect& rrect,
941 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800942 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700943 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700944 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400945 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400946
947 const SkStrokeRec& stroke = style.strokeRec();
948 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700949 return;
950 }
951
bsalomon7f0d9f32016-08-15 14:49:10 -0700952 GrNoClip noclip;
953 const GrClip* clip = &origClip;
954#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
955 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500956 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700957 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
958 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -0400959 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
960 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -0700961 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -0400962 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
963 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700964 clip = &noclip;
965 }
966#endif
bsalomon6663acf2016-05-10 09:14:17 -0700967 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700968
Robert Phillips72152832017-01-25 17:31:35 -0500969 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700970
Chris Dalton7d6748e2019-03-13 00:34:52 -0600971 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -0500972
Chris Dalton0dffbab2019-03-27 13:08:50 -0600973 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -0400974 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
975 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
976 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
977 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
978 // to avoid perf regressions on some platforms.
979 assert_alive(paint);
980 op = GrOvalOpFactory::MakeCircularRRectOp(
981 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
982 }
983 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600984 assert_alive(paint);
985 op = GrFillRRectOp::Make(
986 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
987 }
Greg Daniel2655ede2019-04-10 00:49:28 +0000988 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600989 assert_alive(paint);
990 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +0000991 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -0600992 }
993 if (op) {
994 this->addDrawOp(*clip, std::move(op));
995 return;
robertphillipsea461502015-05-26 11:38:03 -0700996 }
robertphillipsb56f9272016-02-25 11:03:52 -0800997
Mike Klein16885072018-12-11 09:54:31 -0500998 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500999 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1000 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001001}
1002
Jim Van Verthc5903412016-11-17 15:27:09 -05001003///////////////////////////////////////////////////////////////////////////////
1004
Jim Van Verth3af1af92017-05-18 15:06:54 -04001005static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1006 SkPoint3 result;
1007 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1008 result.fZ = pt.fZ;
1009 return result;
1010}
1011
1012bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001013 const SkMatrix& viewMatrix,
1014 const SkPath& path,
1015 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001016 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001017 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001018 return true;
1019 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001020 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001021 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001022
1023 // check z plane
1024 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1025 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1026 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1027 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1028 return false;
1029 }
1030
1031 SkRRect rrect;
1032 SkRect rect;
1033 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001034 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001035 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1036 if (!isRRect &&
1037 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1038 rect.width() > SK_ScalarNearlyZero) {
1039 rrect.setOval(rect);
1040 isRRect = true;
1041 }
1042 if (!isRRect && path.isRect(&rect)) {
1043 rrect.setRect(rect);
1044 isRRect = true;
1045 }
1046
1047 if (!isRRect) {
1048 return false;
1049 }
1050
Jim Van Verthc5903412016-11-17 15:27:09 -05001051 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001052 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001053 }
1054
Robert Phillips72152832017-01-25 17:31:35 -05001055 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001056
Jim Van Verth3af1af92017-05-18 15:06:54 -04001057 // transform light
1058 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1059
1060 // 1/scale
1061 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001062 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001063 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1064 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1065
1066 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001067 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1068
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001069 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001070 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1071 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1072 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001073
1074 // Outset the shadow rrect to the border of the penumbra
1075 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1076 SkRRect ambientRRect;
1077 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1078 // If the rrect was an oval then its outset will also be one.
1079 // We set it explicitly to avoid errors.
1080 if (rrect.isOval()) {
1081 ambientRRect = SkRRect::MakeOval(outsetRect);
1082 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001083 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001084 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1085 }
1086
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001087 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088 if (transparent) {
1089 // set a large inset to force a fill
1090 devSpaceInsetWidth = ambientRRect.width();
1091 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001092
Robert Phillips7c525e62018-06-12 10:11:12 -04001093 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1094 ambientColor,
1095 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001096 ambientRRect,
1097 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001098 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001099 if (op) {
1100 this->addDrawOp(clip, std::move(op));
1101 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001102 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001103
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001104 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001105 SkScalar devSpaceSpotBlur;
1106 SkScalar spotScale;
1107 SkVector spotOffset;
1108 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1109 devLightPos.fZ, rec.fLightRadius,
1110 &devSpaceSpotBlur, &spotScale, &spotOffset);
1111 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001112 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1113
Jim Van Verth3af1af92017-05-18 15:06:54 -04001114 // Adjust translate for the effect of the scale.
1115 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1116 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1117 // This offset is in dev space, need to transform it into source space.
1118 SkMatrix ctmInverse;
1119 if (viewMatrix.invert(&ctmInverse)) {
1120 ctmInverse.mapPoints(&spotOffset, 1);
1121 } else {
1122 // Since the matrix is a similarity, this should never happen, but just in case...
1123 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1124 SkASSERT(false);
1125 }
1126
1127 // Compute the transformed shadow rrect
1128 SkRRect spotShadowRRect;
1129 SkMatrix shadowTransform;
1130 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1131 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001132 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133
1134 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001135 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001136 SkScalar insetWidth = blurOutset;
1137 if (transparent) {
1138 // If transparent, just do a fill
1139 insetWidth += spotShadowRRect.width();
1140 } else {
1141 // For shadows, instead of using a stroke we specify an inset from the penumbra
1142 // border. We want to extend this inset area so that it meets up with the caster
1143 // geometry. The inset geometry will by default already be inset by the blur width.
1144 //
1145 // We compare the min and max corners inset by the radius between the original
1146 // rrect and the shadow rrect. The distance between the two plus the difference
1147 // between the scaled radius and the original radius gives the distance from the
1148 // transformed shadow shape to the original shape in that corner. The max
1149 // of these gives the maximum distance we need to cover.
1150 //
1151 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1152 // that to get the full insetWidth.
1153 SkScalar maxOffset;
1154 if (rrect.isRect()) {
1155 // Manhattan distance works better for rects
1156 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1157 rrect.rect().fLeft),
1158 SkTAbs(spotShadowRRect.rect().fTop -
1159 rrect.rect().fTop)),
1160 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1161 rrect.rect().fRight),
1162 SkTAbs(spotShadowRRect.rect().fBottom -
1163 rrect.rect().fBottom)));
1164 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001165 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001166 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1167 rrect.rect().fLeft + dr,
1168 spotShadowRRect.rect().fTop -
1169 rrect.rect().fTop + dr);
1170 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1171 rrect.rect().fRight - dr,
1172 spotShadowRRect.rect().fBottom -
1173 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001174 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1175 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001176 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001177 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001178 }
1179
1180 // Outset the shadow rrect to the border of the penumbra
1181 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1182 if (spotShadowRRect.isOval()) {
1183 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1184 } else {
1185 SkScalar outsetRad = spotRadius + blurOutset;
1186 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1187 }
1188
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001189 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001190
Robert Phillips7c525e62018-06-12 10:11:12 -04001191 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1192 spotColor,
1193 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001194 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001195 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001196 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001197 if (op) {
1198 this->addDrawOp(clip, std::move(op));
1199 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001200 }
1201
1202 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001203}
1204
1205///////////////////////////////////////////////////////////////////////////////
1206
Brian Osman11052242016-10-27 14:47:55 -04001207bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001208 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001209 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001210 const SkMatrix& viewMatrix,
1211 const SkRRect& origOuter,
1212 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001213 SkASSERT(!origInner.isEmpty());
1214 SkASSERT(!origOuter.isEmpty());
1215
Brian Salomon65749212017-12-01 16:01:47 -05001216 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1217
Chris Dalton7d6748e2019-03-13 00:34:52 -06001218 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001219
1220 if (GrAAType::kMSAA == aaType) {
1221 return false;
1222 }
1223
Greg Daniel2655ede2019-04-10 00:49:28 +00001224 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1225 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001226 auto outerR = outer->width() / 2.f;
1227 auto innerR = inner->width() / 2.f;
1228 auto cx = outer->getBounds().fLeft + outerR;
1229 auto cy = outer->getBounds().fTop + outerR;
1230 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1231 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1232 auto avgR = (innerR + outerR) / 2.f;
1233 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1234 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1235 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001236 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001237 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001238 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001239 if (op) {
1240 this->addDrawOp(clip, std::move(op));
1241 return true;
1242 }
Mike Klein16885072018-12-11 09:54:31 -05001243 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001244 }
1245 }
1246
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001247 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001248 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001249 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1250 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001251 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001252 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1253 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001254 }
robertphillips00095892016-02-29 13:50:40 -08001255
robertphillips00095892016-02-29 13:50:40 -08001256 SkMatrix inverseVM;
1257 if (!viewMatrix.isIdentity()) {
1258 if (!origInner.transform(viewMatrix, inner.writable())) {
1259 return false;
1260 }
1261 if (!origOuter.transform(viewMatrix, outer.writable())) {
1262 return false;
1263 }
1264 if (!viewMatrix.invert(&inverseVM)) {
1265 return false;
1266 }
1267 } else {
1268 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001269 }
robertphillips00095892016-02-29 13:50:40 -08001270
Ethan Nicholaseace9352018-10-15 20:09:54 +00001271 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001272 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001273 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001274 if (!innerEffect) {
1275 return false;
1276 }
1277
Ethan Nicholaseace9352018-10-15 20:09:54 +00001278 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001279 if (!outerEffect) {
1280 return false;
1281 }
1282
Brian Salomon82f44312017-01-11 13:42:54 -05001283 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1284 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001285
1286 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001287 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001288 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1289 }
halcanary9d524f22016-03-29 09:03:52 -07001290
Brian Salomon82f44312017-01-11 13:42:54 -05001291 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1292 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001293 return true;
1294}
1295
Brian Osman11052242016-10-27 14:47:55 -04001296void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001297 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001298 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001299 const SkMatrix& viewMatrix,
1300 const SkRRect& outer,
1301 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001302 ASSERT_SINGLE_OWNER
1303 RETURN_IF_ABANDONED
1304 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001305 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001306
1307 SkASSERT(!outer.isEmpty());
1308 SkASSERT(!inner.isEmpty());
1309
Robert Phillips72152832017-01-25 17:31:35 -05001310 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001311
Brian Salomon82f44312017-01-11 13:42:54 -05001312 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001313 return;
1314 }
Mike Klein16885072018-12-11 09:54:31 -05001315 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001316
1317 SkPath path;
1318 path.setIsVolatile(true);
1319 path.addRRect(inner);
1320 path.addRRect(outer);
1321 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001322 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001323}
1324
robertphillipsea461502015-05-26 11:38:03 -07001325///////////////////////////////////////////////////////////////////////////////
1326
Brian Osman11052242016-10-27 14:47:55 -04001327void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001328 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001329 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001330 const SkMatrix& viewMatrix,
1331 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001332 const GrStyle& style,
1333 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001334 ASSERT_SINGLE_OWNER
1335 RETURN_IF_ABANDONED
1336 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001337 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001338
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001339 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001340 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001341 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001342 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001343 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1344 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001345 aa = GrAA::kNo;
1346 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001347 }
msarettcc319b92016-08-25 18:07:18 -07001348 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001349 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001350 SkPath path;
1351 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001352 path.setIsVolatile(true);
1353
Brian Salomon82f44312017-01-11 13:42:54 -05001354 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001355 }
1356
Chris Dalton7d6748e2019-03-13 00:34:52 -06001357 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001358 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1359 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001360 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001361}
1362
Brian Osman11052242016-10-27 14:47:55 -04001363void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001364 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001365 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001366 const SkMatrix& viewMatrix,
1367 const SkRect& oval,
1368 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001369 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001370 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001371 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001372 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001373
Robert Phillips7484d202018-07-03 09:09:08 -04001374 const SkStrokeRec& stroke = style.strokeRec();
1375
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001376 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001377 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1378 return;
1379 }
1380
1381 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001382 return;
robertphillipsea461502015-05-26 11:38:03 -07001383 }
1384
Robert Phillips72152832017-01-25 17:31:35 -05001385 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001386
Chris Dalton7d6748e2019-03-13 00:34:52 -06001387 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001388
1389 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001390 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1391 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001392 // We don't draw true circles as round rects in coverage mode, because it can
1393 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1394 assert_alive(paint);
1395 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1396 this->caps()->shaderCaps());
1397 }
1398 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001399 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1400 // the arc equation. This same special geometry and fragment branch also turn out to be a
1401 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1402 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1403 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001404 assert_alive(paint);
1405 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1406 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001407 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001408 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001409 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001410 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1411 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001412 }
1413 if (op) {
1414 this->addDrawOp(clip, std::move(op));
1415 return;
robertphillipsea461502015-05-26 11:38:03 -07001416 }
robertphillipsb56f9272016-02-25 11:03:52 -08001417
Mike Klein16885072018-12-11 09:54:31 -05001418 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001419 this->drawShapeUsingPathRenderer(
1420 clip, std::move(paint), aa, viewMatrix,
Mike Reed4241f5e2019-09-14 19:13:23 +00001421 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001422}
1423
Brian Osman11052242016-10-27 14:47:55 -04001424void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001425 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001426 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001427 const SkMatrix& viewMatrix,
1428 const SkRect& oval,
1429 SkScalar startAngle,
1430 SkScalar sweepAngle,
1431 bool useCenter,
1432 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001433 ASSERT_SINGLE_OWNER
1434 RETURN_IF_ABANDONED
1435 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001436 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001437
1438 AutoCheckFlush acf(this->drawingManager());
1439
Chris Dalton7d6748e2019-03-13 00:34:52 -06001440 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001441 if (GrAAType::kCoverage == aaType) {
1442 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1443 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1444 std::move(paint),
1445 viewMatrix,
1446 oval,
1447 startAngle,
1448 sweepAngle,
1449 useCenter,
1450 style,
1451 shaderCaps);
1452 if (op) {
1453 this->addDrawOp(clip, std::move(op));
1454 return;
1455 }
1456 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001457 }
Brian Salomone4949402018-04-26 15:22:04 -04001458 this->drawShapeUsingPathRenderer(
1459 clip, std::move(paint), aa, viewMatrix,
1460 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001461}
1462
Brian Osman11052242016-10-27 14:47:55 -04001463void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001464 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001465 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001466 sk_sp<GrTextureProxy> image,
1467 sk_sp<GrColorSpaceXform> csxf,
1468 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001469 std::unique_ptr<SkLatticeIter> iter,
1470 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001471 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001472 RETURN_IF_ABANDONED
1473 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001474 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001475
Robert Phillips72152832017-01-25 17:31:35 -05001476 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001477
Brian Salomon2a943df2018-05-04 13:43:19 -04001478 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001479 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1480 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001481 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001482}
1483
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001484void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1485 const SkRect& bounds) {
1486 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1487 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001488 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001489}
1490
Brian Salomon031b0ba2019-05-23 11:05:26 -04001491void GrRenderTargetContext::asyncRescaleAndReadPixels(
1492 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1493 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1494 auto direct = fContext->priv().asDirectContext();
1495 if (!direct) {
1496 callback(context, nullptr, 0);
1497 return;
1498 }
1499 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1500 callback(context, nullptr, 0);
1501 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001502 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001503 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1504 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001505 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001506 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001507 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001508 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001509 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1510 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001511 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001512 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001513 backendFormatOfFinalContext, dstCT);
1514 // Fail if we can't read from the source surface's color type.
1515 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001516 callback(context, nullptr, 0);
1517 return;
1518 }
Brian Salomon624f9062019-07-02 14:23:00 -04001519 // Fail if read color type does not have all of dstCT's color channels and those missing color
1520 // channels are in the src.
1521 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1522 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1523 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1524 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001525 callback(context, nullptr, 0);
1526 return;
1527 }
1528
Brian Salomonbf6b9792019-08-21 09:38:10 -04001529 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001530 int x = srcRect.fLeft;
1531 int y = srcRect.fTop;
1532 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001533 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1534 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001535 callback(context, nullptr, 0);
1536 return;
1537 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001538 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1539 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001540 x = y = 0;
1541 } else {
1542 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001543 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1544 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001545 info.colorSpace(), info.alphaType());
1546 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1547 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001548 // We flip or color convert by drawing and we don't currently support drawing to
1549 // kPremul.
1550 if (info.alphaType() == kUnpremul_SkAlphaType) {
1551 callback(context, nullptr, 0);
1552 return;
1553 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001554 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001555 SkRect srcRectToDraw = SkRect::Make(srcRect);
1556 // If the src is not texturable first try to make a copy to a texture.
1557 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001558 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1559 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1560 SkBudgeted::kNo);
1561 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001562 callback(context, nullptr, 0);
1563 return;
1564 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001565 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1566 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001567 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001568 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1569 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1570 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001571 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001572 callback(context, nullptr, 0);
1573 return;
1574 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001575 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1576 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1577 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1578 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1579 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001580 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001581 }
1582 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001583 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001584 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1585 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001586}
1587
Brian Salomon024bd002019-06-11 11:38:16 -04001588void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1589 ReadPixelsCallback callback,
1590 ReadPixelsContext context) {
1591 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1592 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1593
1594 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1595
1596 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001597 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001598 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1599 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001600 this->colorSpaceInfo().refColorSpace());
1601 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001602 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001603 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001604 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001605 callback(context, pm.addr(), pm.rowBytes());
1606 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001607 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001608
Brian Salomonab32f652019-05-10 14:24:50 -04001609 struct FinishContext {
1610 ReadPixelsCallback* fClientCallback;
1611 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001612 int fW, fH;
1613 SkColorType fColorType;
1614 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001615 };
1616 // Assumption is that the caller would like to flush. We could take a parameter or require an
1617 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1618 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001619 auto* finishContext = new FinishContext{callback, context, rect.width(),
1620 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001621 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001622 const auto* context = reinterpret_cast<const FinishContext*>(c);
1623 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001624 if (!data) {
1625 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1626 delete context;
1627 return;
1628 }
Brian Salomon4d036892019-07-02 15:10:58 -04001629 std::unique_ptr<char[]> tmp;
1630 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001631 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001632 tmp.reset(new char[rowBytes * context->fH]);
1633 context->fTransferResult.fPixelConverter(tmp.get(), data);
1634 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001635 }
Brian Salomon4d036892019-07-02 15:10:58 -04001636 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001637 delete context;
1638 };
1639 GrFlushInfo flushInfo;
1640 flushInfo.fFinishedContext = finishContext;
1641 flushInfo.fFinishedProc = finishCallback;
1642 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1643}
1644
1645void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1646 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1647 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1648 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1649 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1650 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1651 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1652 auto direct = fContext->priv().asDirectContext();
1653 if (!direct) {
1654 callback(context, nullptr, nullptr);
1655 return;
1656 }
1657 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1658 callback(context, nullptr, nullptr);
1659 return;
1660 }
1661 if (dstW & 0x1) {
1662 return;
1663 }
1664 int x = srcRect.fLeft;
1665 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001666 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001667 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1668 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001669 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001670 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1671 dstColorSpace);
1672 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001673 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1674 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001675 callback(context, nullptr, nullptr);
1676 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001677 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001678 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1679 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001680 x = y = 0;
1681 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001682 // We assume the caller wants kPremul. There is no way to indicate a preference.
1683 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1684 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1685 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001686 if (xform) {
1687 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1688 // TODO: Do something if the input is not a texture already.
1689 if (!texProxy) {
1690 callback(context, nullptr, nullptr);
1691 return;
1692 }
Brian Salomon024bd002019-06-11 11:38:16 -04001693 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001694 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001695 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1696 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001697 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001698 callback(context, nullptr, nullptr);
1699 return;
1700 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001701 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1702 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1703 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1704 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1705 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001706 x = y = 0;
1707 }
1708 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001709 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001710 // TODO: Do something if the input is not a texture already.
1711 if (!srcProxy) {
1712 callback(context, nullptr, nullptr);
1713 return;
1714 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001715 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1716 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1717 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1718 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1719 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1720 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1721 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1722 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1723 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001724 if (!yRTC || !uRTC || !vRTC) {
1725 callback(context, nullptr, nullptr);
1726 return;
1727 }
1728
1729 static constexpr float kRec601M[] {
1730 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1731 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1732 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1733 };
1734 static constexpr float kRec709M[] {
1735 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1736 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1737 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1738 };
1739 static constexpr float kJpegM[] {
1740 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1741 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1742 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1743 };
1744 static constexpr float kIM[] {
1745 1.f, 0.f, 0.f, 0.f,
1746 0.f, 1.f, 0.f, 0.f,
1747 0.f, 0.f, 1.f, 0.f,
1748 };
1749 const float* baseM = kIM;
1750 switch (yuvColorSpace) {
1751 case kRec601_SkYUVColorSpace:
1752 baseM = kRec601M;
1753 break;
1754 case kRec709_SkYUVColorSpace:
1755 baseM = kRec709M;
1756 break;
1757 case kJPEG_SkYUVColorSpace:
1758 baseM = kJpegM;
1759 break;
1760 case kIdentity_SkYUVColorSpace:
1761 baseM = kIM;
1762 break;
1763 }
1764 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1765
1766 auto texMatrix = SkMatrix::MakeTrans(x, y);
1767
Michael Ludwig61328202019-06-19 14:48:58 +00001768 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1769 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1770
Brian Salomon024bd002019-06-11 11:38:16 -04001771 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1772 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001773 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001774 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1775 GrPaint yPaint;
1776 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1777 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1778 yPaint.addColorFragmentProcessor(std::move(yFP));
1779 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001780 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1781 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001782 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1783 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1784 if (!yTransfer.fTransferBuffer) {
1785 callback(context, nullptr, nullptr);
1786 return;
1787 }
1788
1789 texMatrix.preScale(2.f, 2.f);
1790 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1791 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001792 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001793 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1794 GrPaint uPaint;
1795 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1796 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1797 uPaint.addColorFragmentProcessor(std::move(uFP));
1798 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001799 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1800 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001801 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1802 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1803 if (!uTransfer.fTransferBuffer) {
1804 callback(context, nullptr, nullptr);
1805 return;
1806 }
1807
1808 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1809 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001810 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001811 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1812 GrPaint vPaint;
1813 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1814 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1815 vPaint.addColorFragmentProcessor(std::move(vFP));
1816 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001817 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1818 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001819 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1820 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1821 if (!vTransfer.fTransferBuffer) {
1822 callback(context, nullptr, nullptr);
1823 return;
1824 }
1825
1826 struct FinishContext {
1827 ReadPixelsCallbackYUV420* fClientCallback;
1828 ReadPixelsContext fClientContext;
1829 int fW, fH;
1830 PixelTransferResult fYTransfer;
1831 PixelTransferResult fUTransfer;
1832 PixelTransferResult fVTransfer;
1833 };
1834 // Assumption is that the caller would like to flush. We could take a parameter or require an
1835 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1836 // callback to GrGpu until after the next flush that flushes our op list, though.
1837 auto* finishContext = new FinishContext{callback,
1838 context,
1839 dstW,
1840 dstH,
1841 std::move(yTransfer),
1842 std::move(uTransfer),
1843 std::move(vTransfer)};
1844 auto finishCallback = [](GrGpuFinishedContext c) {
1845 const auto* context = reinterpret_cast<const FinishContext*>(c);
1846 const void* y = context->fYTransfer.fTransferBuffer->map();
1847 const void* u = context->fUTransfer.fTransferBuffer->map();
1848 const void* v = context->fVTransfer.fTransferBuffer->map();
1849 if (!y || !u || !v) {
1850 if (y) {
1851 context->fYTransfer.fTransferBuffer->unmap();
1852 }
1853 if (u) {
1854 context->fUTransfer.fTransferBuffer->unmap();
1855 }
1856 if (v) {
1857 context->fVTransfer.fTransferBuffer->unmap();
1858 }
1859 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1860 delete context;
1861 return;
1862 }
1863 size_t w = SkToSizeT(context->fW);
1864 size_t h = SkToSizeT(context->fH);
1865 std::unique_ptr<uint8_t[]> yTemp;
1866 if (context->fYTransfer.fPixelConverter) {
1867 yTemp.reset(new uint8_t[w * h]);
1868 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1869 y = yTemp.get();
1870 }
1871 std::unique_ptr<uint8_t[]> uTemp;
1872 if (context->fUTransfer.fPixelConverter) {
1873 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1874 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1875 u = uTemp.get();
1876 }
1877 std::unique_ptr<uint8_t[]> vTemp;
1878 if (context->fVTransfer.fPixelConverter) {
1879 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1880 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1881 v = vTemp.get();
1882 }
1883 const void* data[] = {y, u, v};
1884 size_t rowBytes[] = {w, w / 2, w / 2};
1885 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1886 context->fYTransfer.fTransferBuffer->unmap();
1887 context->fUTransfer.fTransferBuffer->unmap();
1888 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001889 delete context;
1890 };
1891 GrFlushInfo flushInfo;
1892 flushInfo.fFinishedContext = finishContext;
1893 flushInfo.fFinishedProc = finishCallback;
1894 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001895}
1896
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001897GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1898 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001899 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001900 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001901 return GrSemaphoresSubmitted::kNo;
1902 }
robertphillips8c523e02016-07-26 07:41:00 -07001903 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001904 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001905
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001906 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001907}
1908
Greg Danielc64ee462017-06-15 16:59:49 -04001909bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001910 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001911 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001912 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001913 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001914 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001915
1916 AutoCheckFlush acf(this->drawingManager());
1917
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001918 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001919 return false;
1920 }
1921
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001922 auto direct = fContext->priv().asDirectContext();
1923 if (!direct) {
1924 return false;
1925 }
1926
1927 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001928
Greg Danielc30f1a92019-09-06 15:28:58 -04001929 std::unique_ptr<sk_sp<GrSemaphore>[]> grSemaphores(new sk_sp<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001930 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001931 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001932 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1933 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001934 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001935 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1936 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001937 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001938}
joshualitt33a5fce2015-11-18 13:28:51 -08001939
Robert Phillips65a88fa2017-08-08 08:36:22 -04001940void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001941 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06001942 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001943}
1944
Brian Osman11052242016-10-27 14:47:55 -04001945void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001946 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001947 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001948 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001949 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001950 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001951 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001952 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001953 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001954 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1955
Brian Salomon40b77a62017-12-22 11:25:52 -05001956 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001957
Robert Phillips27927a52018-08-20 13:18:12 -04001958 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001959}
1960
1961void GrRenderTargetContext::drawShape(const GrClip& clip,
1962 GrPaint&& paint,
1963 GrAA aa,
1964 const SkMatrix& viewMatrix,
1965 const GrShape& shape) {
1966 ASSERT_SINGLE_OWNER
1967 RETURN_IF_ABANDONED
1968 SkDEBUGCODE(this->validate();)
1969 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1970
Brian Salomon2fad74a2017-12-20 13:28:55 -05001971 if (shape.isEmpty()) {
1972 if (shape.inverseFilled()) {
1973 this->drawPaint(clip, std::move(paint), viewMatrix);
1974 }
1975 return;
robertphillipsea461502015-05-26 11:38:03 -07001976 }
1977
Robert Phillips72152832017-01-25 17:31:35 -05001978 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001979
Brian Salomon2fad74a2017-12-20 13:28:55 -05001980 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06001981 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001982 SkRRect rrect;
1983 // We can ignore the starting point and direction since there is no path effect.
1984 bool inverted;
1985 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1986 if (rrect.isRect()) {
1987 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1988 &shape.style());
1989 return;
1990 } else if (rrect.isOval()) {
1991 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001992 return;
1993 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001994 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1995 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001996 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1997 viewMatrix.rectStaysRect()) {
1998 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1999 // the matrix to all the points individually rather than just to the rect
2000 SkRect rects[2];
2001 if (shape.asNestedRects(rects)) {
2002 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002003 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002004 fContext, std::move(paint), viewMatrix, rects);
2005 if (op) {
2006 this->addDrawOp(clip, std::move(op));
2007 }
2008 // Returning here indicates that there is nothing to draw in this case.
2009 return;
2010 }
robertphillipsea461502015-05-26 11:38:03 -07002011 }
2012 }
robertphillips4bc31812016-03-01 12:22:49 -08002013
Brian Salomon2fad74a2017-12-20 13:28:55 -05002014 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002015}
2016
Chris Daltonbbfd5162017-11-07 13:35:22 -07002017bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002018 const GrUserStencilSettings* ss,
2019 SkRegion::Op op,
2020 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002021 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002022 const SkMatrix& viewMatrix,
2023 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002024 ASSERT_SINGLE_OWNER_PRIV
2025 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002026 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002027 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2028 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002029
2030 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002031 GrPaint paint;
2032 paint.setCoverageSetOpXPFactory(op, invert);
2033 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2034 SkRect::MakeIWH(fRenderTargetContext->width(),
2035 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002036 return true;
2037 }
2038
Robert Phillips72152832017-01-25 17:31:35 -05002039 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002040
2041 // An Assumption here is that path renderer would use some form of tweaking
2042 // the src color (either the input alpha or in the frag shader) to implement
2043 // aa. If we have some future driver-mojo path AA that can do the right
2044 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002045 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002046 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002047
Chris Daltondb91c6e2017-09-08 16:25:08 -06002048 SkIRect clipConservativeBounds;
2049 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2050 &clipConservativeBounds, nullptr);
2051
bsalomon8acedde2016-06-24 10:42:16 -07002052 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002053 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002054 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002055 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002056 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002057 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002058 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002059 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002060 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2061 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002062 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002063
2064 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002065 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002066 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002067 if (!pr) {
2068 return false;
2069 }
2070
2071 GrPaint paint;
2072 paint.setCoverageSetOpXPFactory(op, invert);
2073
Brian Salomonf3569f02017-10-24 12:52:33 -04002074 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2075 std::move(paint),
2076 ss,
2077 fRenderTargetContext,
2078 &clip,
2079 &clipConservativeBounds,
2080 &viewMatrix,
2081 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002082 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002083 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002084 pr->drawPath(args);
2085 return true;
2086}
2087
Brian Osman11052242016-10-27 14:47:55 -04002088SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002089 ASSERT_SINGLE_OWNER_PRIV
2090
Robert Phillips6a6de562019-02-15 15:19:15 -05002091 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002092 return SkBudgeted::kNo;
2093 }
2094
Brian Osman11052242016-10-27 14:47:55 -04002095 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002096
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002097 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002098}
2099
Brian Salomon2fad74a2017-12-20 13:28:55 -05002100void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2101 GrPaint&& paint,
2102 GrAA aa,
2103 const SkMatrix& viewMatrix,
2104 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002105 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002106 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002107 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2108
Jim Van Verthf86073a2018-10-02 13:05:38 -04002109 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2110 return;
2111 }
2112
Chris Daltondb91c6e2017-09-08 16:25:08 -06002113 SkIRect clipConservativeBounds;
2114 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2115
Brian Salomon2fad74a2017-12-20 13:28:55 -05002116 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002117 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002118
robertphillips68737822015-10-29 12:12:21 -07002119 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002120 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002121 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002122 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002123 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002124 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002125 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002126 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002127
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002128 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002129 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002130 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002131 return;
2132 }
2133
Chris Dalton6ce447a2019-06-23 18:07:38 -06002134 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002135
2136 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2137 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2138 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2139
Brian Salomon2fad74a2017-12-20 13:28:55 -05002140 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002141 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002142 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2143 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002144 return;
2145 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002146 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002147 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002148 }
2149 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002150 if (canDrawArgs.fShape->style().applies()) {
2151 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2152 styleScale);
2153 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002154 return;
2155 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002156 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002157 // This time, allow SW renderer
2158 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2159 } else {
2160 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002161 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002162 }
robertphillipsea461502015-05-26 11:38:03 -07002163
bsalomon8acedde2016-06-24 10:42:16 -07002164 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002165#ifdef SK_DEBUG
2166 SkDebugf("Unable to find path renderer compatible with path.\n");
2167#endif
2168 return;
2169 }
2170
Robert Phillips256c37b2017-03-01 14:32:46 -05002171 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002172 std::move(paint),
2173 &GrUserStencilSettings::kUnused,
2174 this,
2175 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002176 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002177 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002178 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002179 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002180 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002181 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002182}
2183
Brian Salomon467921e2017-03-06 16:17:12 -05002184static void op_bounds(SkRect* bounds, const GrOp* op) {
2185 *bounds = op->bounds();
2186 if (op->hasZeroArea()) {
2187 if (op->hasAABloat()) {
2188 bounds->outset(0.5f, 0.5f);
2189 } else {
2190 // We don't know which way the particular GPU will snap lines or points at integer
2191 // coords. So we ensure that the bounds is large enough for either snap.
2192 SkRect before = *bounds;
2193 bounds->roundOut(bounds);
2194 if (bounds->fLeft == before.fLeft) {
2195 bounds->fLeft -= 1;
2196 }
2197 if (bounds->fTop == before.fTop) {
2198 bounds->fTop -= 1;
2199 }
2200 if (bounds->fRight == before.fRight) {
2201 bounds->fRight += 1;
2202 }
2203 if (bounds->fBottom == before.fBottom) {
2204 bounds->fBottom += 1;
2205 }
2206 }
2207 }
2208}
2209
Chris Dalton08755122019-08-05 16:13:47 -06002210void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002211 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002212 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2213}
2214
Brian Salomon348a0372018-10-31 10:42:18 -04002215void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2216 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002217 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002218 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002219 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002220 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002221 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002222 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002223 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002224 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002225
Brian Salomon467921e2017-03-06 16:17:12 -05002226 // Setup clip
2227 SkRect bounds;
2228 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002229 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002230 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002231 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2232 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2233
2234 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002235 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002236 }
2237
2238 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002239 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002240 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002241 }
2242
Chris Daltoneffee202019-07-01 22:28:03 -06002243 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002244
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002245 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002246 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2247 // here as well.
2248 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2249#ifdef SK_DEBUG
2250 if (hasMixedSampledCoverage) {
2251 SkASSERT(usesStencil);
2252 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2253 }
2254#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002255 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002256 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2257
2258 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002259 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002260 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002261 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002262 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002263 }
2264 }
2265
2266 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002267 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002268 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002269 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002270 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002271 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2272 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002273}
2274
Chris Dalton2243c7b2019-08-21 14:46:35 -06002275bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2276 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002277 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2278 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2279 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002280 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002281 return false;
2282 }
2283
Chris Dalton2243c7b2019-08-21 14:46:35 -06002284 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2285 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002286 // The render target is a texture, so we can read from it directly in the shader. The XP
2287 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002288 dstProxy->setProxy(sk_ref_sp(texProxy));
2289 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002290 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002291 }
2292 }
2293
Chris Dalton2243c7b2019-08-21 14:46:35 -06002294 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002295
Eric Karl74480882017-04-03 14:49:05 -07002296 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002297 clip.getConservativeBounds(
2298 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002299 SkRect opBounds = op.bounds();
2300 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2301 // 0.5 pixels.
2302 if (op.hasAABloat() || op.hasZeroArea()) {
2303 opBounds.outset(0.5f, 0.5f);
2304 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2305 // performance we may ignore the clip when the draw is entirely inside the clip is float
2306 // space but will hit pixels just outside the clip when actually rasterizing.
2307 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002308 clippedRect.intersect(SkIRect::MakeWH(
2309 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002310 }
2311 SkIRect opIBounds;
2312 opBounds.roundOut(&opIBounds);
2313 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002314#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002315 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002316#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002317 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002318 }
2319
2320 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2321 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002322 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002323 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002324
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002325 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002326 copyRect = clippedRect;
2327 }
Brian Salomon467921e2017-03-06 16:17:12 -05002328
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002329 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002330 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002331 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002332 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002333 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002334 } else {
Eric Karl74480882017-04-03 14:49:05 -07002335 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002336 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002337 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002338 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2339 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2340 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002341 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002342
Greg Daniel46cfbc62019-06-07 11:43:30 -04002343 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002344 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002345 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002346}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002347
2348bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2349 const SkIPoint& dstPoint) {
2350 SkIRect clippedSrcRect;
2351 SkIPoint clippedDstPoint;
2352 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2353 &clippedSrcRect, &clippedDstPoint)) {
2354 return false;
2355 }
2356
2357 GrPaint paint;
2358 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2359 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2360 SkMatrix::I());
2361 if (!fp) {
2362 return false;
2363 }
2364 paint.addColorFragmentProcessor(std::move(fp));
2365
2366 this->fillRectToRect(
2367 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2368 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2369 clippedSrcRect.height()),
2370 SkRect::Make(clippedSrcRect));
2371 return true;
2372}
2373