blob: bb4c9803ee5d787b70f3de114b7c147138b82f42 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
11#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
13#include "include/private/SkShadowFlags.h"
14#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040015#include "src/core/SkAutoPixmapStorage.h"
16#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkDrawShadowInfo.h"
18#include "src/core/SkGlyphRunPainter.h"
19#include "src/core/SkLatticeIter.h"
20#include "src/core/SkMatrixPriv.h"
21#include "src/core/SkRRectPriv.h"
22#include "src/core/SkSurfacePriv.h"
23#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrBlurUtils.h"
26#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040029#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrDrawingManager.h"
31#include "src/gpu/GrFixedClip.h"
32#include "src/gpu/GrGpuResourcePriv.h"
33#include "src/gpu/GrMemoryPool.h"
34#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040036#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrRenderTargetContextPriv.h"
38#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrStencilAttachment.h"
40#include "src/gpu/GrStyle.h"
41#include "src/gpu/GrTracing.h"
42#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040043#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/effects/GrRRectEffect.h"
45#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040046#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040047#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000048#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040049#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/ops/GrAtlasTextOp.h"
51#include "src/gpu/ops/GrClearOp.h"
52#include "src/gpu/ops/GrClearStencilClipOp.h"
53#include "src/gpu/ops/GrDebugMarkerOp.h"
54#include "src/gpu/ops/GrDrawAtlasOp.h"
55#include "src/gpu/ops/GrDrawOp.h"
56#include "src/gpu/ops/GrDrawVerticesOp.h"
57#include "src/gpu/ops/GrDrawableOp.h"
58#include "src/gpu/ops/GrFillRRectOp.h"
59#include "src/gpu/ops/GrFillRectOp.h"
60#include "src/gpu/ops/GrLatticeOp.h"
61#include "src/gpu/ops/GrOp.h"
62#include "src/gpu/ops/GrOvalOpFactory.h"
63#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050064#include "src/gpu/ops/GrShadowRRectOp.h"
65#include "src/gpu/ops/GrStencilPathOp.h"
66#include "src/gpu/ops/GrStrokeRectOp.h"
67#include "src/gpu/ops/GrTextureOp.h"
68#include "src/gpu/text/GrTextContext.h"
69#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040070
Herb Derbyc1b482c2018-08-09 15:02:27 -040071class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040072public:
73 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040074 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Robert Phillips7e90be92019-02-15 12:22:59 -050075 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040076 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040077 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040078
Robert Phillips7c525e62018-06-12 10:11:12 -040079 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040080 fRenderTargetContext->addDrawOp(clip, std::move(op));
81 }
82
Robert Phillips46a13382018-08-23 13:53:01 -040083 void drawShape(const GrClip& clip, const SkPaint& paint,
84 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040085 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
86 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040087 }
88
89 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040090 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050091 auto context = fRenderTargetContext->fContext;
Brian Salomonf18b1d82017-10-27 11:30:49 -040092 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
93 if (kARGB_GrMaskFormat == maskFormat) {
94 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
95 } else {
96 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
97 }
98 }
99
Robert Phillips69893702019-02-22 11:16:30 -0500100 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400101 return fRenderTargetContext->fContext;
102 }
103
Herb Derby65956872018-08-21 16:55:04 -0400104 SkGlyphRunListPainter* glyphPainter() override {
105 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400106 }
107
Brian Salomonf18b1d82017-10-27 11:30:49 -0400108private:
109 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400110 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400111
Brian Salomonf18b1d82017-10-27 11:30:49 -0400112};
joshualittbc907352016-01-13 06:45:40 -0800113
Robert Phillips72152832017-01-25 17:31:35 -0500114#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800115#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400116 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800117#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400118 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500119#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
120#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
121#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
122#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
123#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700124
Brian Salomone225b562017-06-14 13:00:03 -0400125//////////////////////////////////////////////////////////////////////////////
126
robertphillipsea461502015-05-26 11:38:03 -0700127class AutoCheckFlush {
128public:
halcanary9d524f22016-03-29 09:03:52 -0700129 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700130 SkASSERT(fDrawingManager);
131 }
bsalomonb77a9072016-09-07 10:02:04 -0700132 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700133
134private:
robertphillips77a2e522015-10-17 07:43:27 -0700135 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700136};
137
Greg Danielf41b2bd2019-08-22 16:19:24 -0400138// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
139// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
140// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
141// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500142GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400143 sk_sp<GrRenderTargetProxy> rtp,
Brian Salomond6287472019-06-24 15:50:07 -0400144 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400145 sk_sp<SkColorSpace> colorSpace,
146 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400147 bool managedOpsTask)
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400148 : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace))
Brian Salomonf3569f02017-10-24 12:52:33 -0400149 , fRenderTargetProxy(std::move(rtp))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400150 , fOpsTask(sk_ref_sp(fRenderTargetProxy->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400151 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400152 , fManagedOpsTask(managedOpsTask) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400153 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700154 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700155}
156
robertphillips2e1e51f2015-10-15 08:01:48 -0700157#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400158void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400159 SkASSERT(fRenderTargetProxy);
160 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700161
Greg Danielf41b2bd2019-08-22 16:19:24 -0400162 if (fOpsTask && !fOpsTask->isClosed()) {
163 SkASSERT(fRenderTargetProxy->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700164 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700165}
166#endif
167
Brian Osman11052242016-10-27 14:47:55 -0400168GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800169 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700170}
171
Chris Dalton7d6748e2019-03-13 00:34:52 -0600172inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600173 if (GrAA::kNo == aa) {
174 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
175 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600176 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600177 return GrAAType::kMSAA;
178 }
179 return GrAAType::kNone;
180 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600181 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600182}
183
Robert Phillipsf200a902017-01-30 13:27:37 -0500184GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000185 return fRenderTargetProxy->asTextureProxy();
186}
187
Greg Daniele252f082017-10-23 16:05:23 -0400188const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
189 return fRenderTargetProxy->asTextureProxy();
190}
191
Robert Phillipsf200a902017-01-30 13:27:37 -0500192sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
193 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
194}
195
Greg Daniele252f082017-10-23 16:05:23 -0400196GrMipMapped GrRenderTargetContext::mipMapped() const {
197 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
198 return proxy->mipMapped();
199 }
200 return GrMipMapped::kNo;
201}
202
Greg Danielf41b2bd2019-08-22 16:19:24 -0400203GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800204 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700205 SkDEBUGCODE(this->validate();)
206
Greg Danielf41b2bd2019-08-22 16:19:24 -0400207 if (!fOpsTask || fOpsTask->isClosed()) {
208 fOpsTask = this->drawingManager()->newOpsTask(fRenderTargetProxy, fManagedOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700209 }
210
Greg Danielf41b2bd2019-08-22 16:19:24 -0400211 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700212}
213
Herb Derbycddab252018-07-16 11:19:04 -0400214void GrRenderTargetContext::drawGlyphRunList(
215 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400216 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800217 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700218 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700219 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400220 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700221
Greg Danielbe7fc462019-01-03 16:40:42 -0500222 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
223 // secondary command buffers because it would require stopping and starting a render pass which
224 // we don't have access to.
225 if (this->wrapsVkSecondaryCB()) {
226 return;
227 }
228
Herb Derby26cbe512018-05-24 14:39:01 -0400229 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400230 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400231 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700232}
233
Brian Osman11052242016-10-27 14:47:55 -0400234void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800235 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700236 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700237 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400238 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700239
Robert Phillips72152832017-01-25 17:31:35 -0500240 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400241
Greg Danielf41b2bd2019-08-22 16:19:24 -0400242 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700243}
244
Brian Osman11052242016-10-27 14:47:55 -0400245void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500246 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700247 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800248 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700249 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700250 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400251 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700252
Robert Phillips72152832017-01-25 17:31:35 -0500253 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700254 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
255 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700256}
robertphillips9199a9f2016-07-13 07:48:43 -0700257
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500258void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
259 const SkPMColor4f& color,
260 CanClearFullscreen canClearFullscreen) {
261 ASSERT_SINGLE_OWNER_PRIV
262 RETURN_IF_ABANDONED_PRIV
263 SkDEBUGCODE(fRenderTargetContext->validate();)
264 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
265 fRenderTargetContext->fContext);
266
267 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
268 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
269}
270
271static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
272 paint->setColor4f(color);
273 if (color.isOpaque()) {
274 // Can just rely on the src-over blend mode to do the right thing
275 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
276 } else {
277 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
278 // were src blended
279 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
280 }
281}
282
283void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
284 const SkPMColor4f& color,
285 CanClearFullscreen canClearFullscreen) {
286 bool isFull = false;
287 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400288 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
289 // only clear the entire target if we knew it had not been cleared before. As
290 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500291 isFull = !clip.scissorEnabled() ||
292 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400293 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500294 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
295 }
296
297 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400298 GrOpsTask* opsTask = this->getOpsTask();
299 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500300 !this->caps()->performColorClearsAsDraws()) {
301 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400302 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500303 return;
304 } else {
305 // Will use an op for the clear, reset the load op to discard since the op will
306 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400307 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500308 }
309
310 // Must add an op to the list (either because we couldn't use a load op, or because the
311 // clear load op isn't supported)
312 if (this->caps()->performColorClearsAsDraws()) {
313 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
314 GrPaint paint;
315 clear_to_grpaint(color, &paint);
316 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400317 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
318 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500319 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600320 this->addOp(GrClearOp::Make(
321 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500322 }
323 } else {
324 if (this->caps()->performPartialClearsAsDraws()) {
325 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500326 GrPaint paint;
327 clear_to_grpaint(color, &paint);
328
Michael Ludwig64b28a72019-05-28 12:02:00 -0400329 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400330 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
331 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500332 } else {
333 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
334 this->asSurfaceProxy()));
335 // This version of the clear op factory can return null if the clip doesn't intersect
336 // with the surface proxy's boundary
337 if (!op) {
338 return;
339 }
Chris Dalton08755122019-08-05 16:13:47 -0600340 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500341 }
342 }
343}
344
Brian Osman11052242016-10-27 14:47:55 -0400345void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500346 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400347 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000348 // Start with the render target, since that is the maximum content we could possibly fill.
349 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400350 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400351 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000352 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
353 // inverse view matrix.
354 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
355 } else {
356 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
357 SkMatrix localMatrix;
358 if (!viewMatrix.invert(&localMatrix)) {
359 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400360 }
Michael Ludwig61328202019-06-19 14:48:58 +0000361 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
362 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700363 }
robertphillipsea461502015-05-26 11:38:03 -0700364}
365
Michael Ludwig61328202019-06-19 14:48:58 +0000366enum class GrRenderTargetContext::QuadOptimization {
367 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
368 kDiscarded,
369 // The rect to draw was converted to some other op and appended to the oplist, so no additional
370 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
371 // a constColor is provided.
372 kSubmitted,
373 // The clip was folded into the device quad, with updated edge flags and local coords, and
374 // caller is responsible for adding an appropriate op.
375 kClipApplied,
376 // No change to clip, but quad updated to better fit clip/render target, and caller is
377 // responsible for adding an appropriate op.
378 kCropped
379};
Michael Ludwig61a16512019-01-15 11:15:13 -0500380
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400381static bool make_vertex_finite(float* value) {
382 if (SkScalarIsNaN(*value)) {
383 return false;
384 }
385
386 if (!SkScalarIsFinite(*value)) {
387 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
388 // left when calculating crops.
389 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
390 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
391 }
392
393 return true;
394}
395
Michael Ludwig61328202019-06-19 14:48:58 +0000396GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400397 const GrClip& clip, const SkPMColor4f* constColor,
398 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
399 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000400 // Optimization requirements:
401 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
402 // 2. kClear applies when constColor and final geom is pixel aligned rect;
403 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
404 // 3. kRRect applies when constColor and rrect clip and quad covers clip
405 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
406 // 5. kCropped applies when rect quad (currently)
407 // 6. kNone always applies
408 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000409
Michael Ludwige08b4432019-06-19 18:00:48 -0400410 SkRect rtRect;
411 if (stencilSettings) {
412 // Must use worst case bounds so that stencil buffer updates on approximately sized render
413 // targets don't get corrupted.
414 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
415 fRenderTargetProxy->worstCaseHeight());
416 } else {
417 // Use the logical size of the render target, which allows for "fullscreen" clears even if
418 // the render target has an approximate backing fit
419 rtRect = SkRect::MakeWH(this->width(), this->height());
420 }
421
Michael Ludwig61328202019-06-19 14:48:58 +0000422 SkRect drawBounds = deviceQuad->bounds();
423 if (constColor) {
424 // Don't bother updating local coordinates when the paint will ignore them anyways
425 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400426 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
427 // will be cropped to the finite 'clip' or render target and there is no local space mapping
428 if (!deviceQuad->isFinite()) {
429 for (int i = 0; i < 4; ++i) {
430 if (!make_vertex_finite(deviceQuad->xs() + i) ||
431 !make_vertex_finite(deviceQuad->ys() + i) ||
432 !make_vertex_finite(deviceQuad->ws() + i)) {
433 // Discard if we see a nan
434 return QuadOptimization::kDiscarded;
435 }
436 }
437 SkASSERT(deviceQuad->isFinite());
438 }
439 } else {
440 // CropToRect requires the quads to be finite. If they are not finite and we have local
441 // coordinates, the mapping from local space to device space is poorly defined so drop it
442 if (!deviceQuad->isFinite()) {
443 return QuadOptimization::kDiscarded;
444 }
Brian Salomon7694b902019-06-18 21:00:21 +0000445 }
446
Michael Ludwig61328202019-06-19 14:48:58 +0000447 // If the quad is entirely off screen, it doesn't matter what the clip does
448 if (!rtRect.intersects(drawBounds)) {
449 return QuadOptimization::kDiscarded;
450 }
Brian Salomon7694b902019-06-18 21:00:21 +0000451
Michael Ludwig61328202019-06-19 14:48:58 +0000452 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
453 // the render target).
454 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400455 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
456 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
457 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
458 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000459 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000460 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000461 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
462 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000463 }
Brian Salomon7694b902019-06-18 21:00:21 +0000464 }
465
Michael Ludwig61328202019-06-19 14:48:58 +0000466 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
467 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400468 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000469 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
470 // of the render target and the clip rect)
471 SkRect clipBounds = rtRect;
472 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
473 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000474 }
475
Michael Ludwig61328202019-06-19 14:48:58 +0000476 if (clipRRect.isRect()) {
477 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
478 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
479 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
480 // Clear optimization is possible
481 drawBounds = deviceQuad->bounds();
482 if (drawBounds.contains(rtRect)) {
483 // Fullscreen clear
484 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
485 return QuadOptimization::kSubmitted;
486 } else if (GrClip::IsPixelAligned(drawBounds) &&
487 drawBounds.width() > 256 && drawBounds.height() > 256) {
488 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
489 SkIRect scissorRect;
490 drawBounds.round(&scissorRect);
491 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
492 return QuadOptimization::kSubmitted;
493 }
494 }
495
496 // Update overall AA setting.
497 *edgeFlags = newFlags;
498 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
499 newFlags != GrQuadAAFlags::kNone) {
500 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
501 // properly reflect the smooth edge of the clip.
502 *aa = GrAA::kYes;
503 }
504 // We intentionally do not downgrade AA here because we don't know if we need to
505 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
506 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
507
508 // deviceQuad is exactly the intersection of original quad and clip, so it can be
509 // drawn with no clip (submitted by caller)
510 return QuadOptimization::kClipApplied;
511 } else {
512 // The quads have been updated to better fit the clip bounds, but can't get rid of
513 // the clip entirely
514 return QuadOptimization::kCropped;
515 }
516 } else if (constColor) {
517 // Rounded corners and constant filled color (limit ourselves to solid colors because
518 // there is no way to use custom local coordinates with drawRRect).
519 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
520 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
521 deviceQuad->bounds().contains(clipBounds)) {
522 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
523 // we can draw the rrect directly and ignore the edge flags
524 GrPaint paint;
525 clear_to_grpaint(*constColor, &paint);
526 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
527 clipRRect, GrStyle::SimpleFill());
528 return QuadOptimization::kSubmitted;
529 } else {
530 // The quad has been updated to better fit clip bounds, but can't remove the clip
531 return QuadOptimization::kCropped;
532 }
Brian Salomon7694b902019-06-18 21:00:21 +0000533 }
Brian Salomon7694b902019-06-18 21:00:21 +0000534 }
535
Michael Ludwig61328202019-06-19 14:48:58 +0000536 // Crop the quad to the conservative bounds of the clip.
537 SkIRect clipDevBounds;
538 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
539 SkRect clipBounds = SkRect::Make(clipDevBounds);
540
541 // One final check for discarding, since we may have gone here directly due to a complex clip
542 if (!clipBounds.intersects(drawBounds)) {
543 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000544 }
545
Michael Ludwig61328202019-06-19 14:48:58 +0000546 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
547 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
548 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
549
550 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000551}
552
Michael Ludwig61328202019-06-19 14:48:58 +0000553void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500554 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500555 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000556 GrQuadAAFlags edgeFlags,
557 const GrQuad& deviceQuad,
558 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400559 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000560 ASSERT_SINGLE_OWNER
561 RETURN_IF_ABANDONED
562 SkDEBUGCODE(this->validate();)
563 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500564
Michael Ludwig61328202019-06-19 14:48:58 +0000565 AutoCheckFlush acf(this->drawingManager());
566
567 SkPMColor4f* constColor = nullptr;
568 SkPMColor4f paintColor;
569 if (!ss && !paint.numCoverageFragmentProcessors() &&
570 paint.isConstantBlendedColor(&paintColor)) {
571 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
572 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500573 }
574
Michael Ludwig61328202019-06-19 14:48:58 +0000575 GrQuad croppedDeviceQuad = deviceQuad;
576 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400577 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
578 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000579 if (opt >= QuadOptimization::kClipApplied) {
580 // These optimizations require caller to add an op themselves
581 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
582 : clip;
583 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
584 : this->chooseAAType(aa);
585 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
586 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700587 }
Michael Ludwig61328202019-06-19 14:48:58 +0000588 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800589}
590
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000591void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
592 sk_sp<GrTextureProxy> proxy,
593 sk_sp<GrColorSpaceXform> textureXform,
594 GrSamplerState::Filter filter,
595 const SkPMColor4f& color,
596 SkBlendMode blendMode,
597 GrAA aa,
598 GrQuadAAFlags edgeFlags,
599 const GrQuad& deviceQuad,
600 const GrQuad& localQuad,
601 const SkRect* domain) {
602 ASSERT_SINGLE_OWNER
603 RETURN_IF_ABANDONED
604 SkDEBUGCODE(this->validate();)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400605 SkASSERT(proxy);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000606 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
607
608 AutoCheckFlush acf(this->drawingManager());
609
610 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
611 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
612 GrQuad croppedDeviceQuad = deviceQuad;
613 GrQuad croppedLocalQuad = localQuad;
614 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
615 &croppedDeviceQuad, &croppedLocalQuad);
616
617 SkASSERT(opt != QuadOptimization::kSubmitted);
618 if (opt != QuadOptimization::kDiscarded) {
619 // And the texture op if not discarded
620 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
621 : clip;
622 GrAAType aaType = this->chooseAAType(aa);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400623 auto clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
624 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
625 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000626 // Use the provided domain, although hypothetically we could detect that the cropped local
627 // quad is sufficiently inside the domain and the constraint could be dropped.
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400628 this->addDrawOp(finalClip,
629 GrTextureOp::Make(fContext, std::move(proxy), std::move(textureXform),
630 filter, color, saturate, blendMode, aaType, edgeFlags,
631 croppedDeviceQuad, croppedLocalQuad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000632 }
633}
634
Brian Osman11052242016-10-27 14:47:55 -0400635void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500636 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500637 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400638 const SkMatrix& viewMatrix,
639 const SkRect& rect,
640 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700641 if (!style) {
642 style = &GrStyle::SimpleFill();
643 }
joshualitt1de610a2016-01-06 08:26:09 -0800644 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700645 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700646 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400647 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700648
bsalomon6663acf2016-05-10 09:14:17 -0700649 // Path effects should've been devolved to a path in SkGpuDevice
650 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700651
Robert Phillips72152832017-01-25 17:31:35 -0500652 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700653
bsalomon6663acf2016-05-10 09:14:17 -0700654 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400655 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000656 // Fills the rect, using rect as its own local coordinates
657 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500658 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700659 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
660 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
661 if ((!rect.width() || !rect.height()) &&
662 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
663 SkScalar r = stroke.getWidth() / 2;
664 // TODO: Move these stroke->fill fallbacks to GrShape?
665 switch (stroke.getJoin()) {
666 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500667 this->drawRect(
668 clip, std::move(paint), aa, viewMatrix,
669 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
670 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700671 return;
672 case SkPaint::kRound_Join:
673 // Raster draws nothing when both dimensions are empty.
674 if (rect.width() || rect.height()){
675 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500676 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
677 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700678 return;
679 }
680 case SkPaint::kBevel_Join:
681 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500682 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700683 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
684 &GrStyle::SimpleFill());
685 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500686 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700687 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
688 &GrStyle::SimpleFill());
689 }
690 return;
691 }
692 }
robertphillips44302392016-07-08 14:43:03 -0700693
Brian Salomonbaaf4392017-06-15 09:59:23 -0400694 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700695
Chris Dalton7d6748e2019-03-13 00:34:52 -0600696 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500697 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
698 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
699 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500700 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400701 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700702 return;
robertphillips4bc31812016-03-01 12:22:49 -0800703 }
robertphillips4bc31812016-03-01 12:22:49 -0800704 }
Mike Klein16885072018-12-11 09:54:31 -0500705 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500706 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700707}
708
Michael Ludwig69858532018-11-28 15:34:34 -0500709void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
710 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
711 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600712 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500713 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
714 quads, cnt));
715}
716
Robert Phillipsec2249f2016-11-09 08:54:35 -0500717int GrRenderTargetContextPriv::maxWindowRectangles() const {
718 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400719 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500720}
721
Greg Danielf41b2bd2019-08-22 16:19:24 -0400722GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600723 ) const {
724#if GR_TEST_UTILS
725 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400726 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600727 }
728#endif
729 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
730 // would normally be overwritten. The one exception is if the render target context is marked as
731 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
732 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
733 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
734 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400735 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600736}
737
Chris Daltoneffee202019-07-01 22:28:03 -0600738void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
739 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600740 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600741 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600742
Chris Daltoneffee202019-07-01 22:28:03 -0600743 int numRequiredSamples = this->numSamples();
744 if (multisampled && 1 == numRequiredSamples) {
745 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
746 // mixed samples.
747 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
748 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400749 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600750 }
751 SkASSERT(numRequiredSamples > 0);
752
753 if (numRequiredSamples > fNumStencilSamples) {
754 fNumStencilSamples = numRequiredSamples;
755 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
756 }
Chris Dalton6b982802019-06-27 13:53:46 -0600757
758 if (needsStencilClear) {
759 if (this->caps()->performStencilClearsAsDraws()) {
760 // There is a driver bug with clearing stencil. We must use an op to manually clear the
761 // stencil buffer before the op that required 'setNeedsStencil'.
762 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
763 } else {
764 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
765 // code note when the instantiated stencil buffer is already clear and skip the clear
766 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
767 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400768 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600769 }
770 }
771}
772
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000773void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700774 ASSERT_SINGLE_OWNER_PRIV
775 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400776 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400777 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
778 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700779
Robert Phillips72152832017-01-25 17:31:35 -0500780 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400781
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500782 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
783}
784
785void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
786 if (this->caps()->performStencilClearsAsDraws()) {
787 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
788 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
789
790 // Configure the paint to have no impact on the color buffer
791 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400792 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400793 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
794 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500795 } else {
796 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
797 fRenderTargetProxy.get()));
798 if (!op) {
799 return;
800 }
Chris Dalton08755122019-08-05 16:13:47 -0600801 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500802 }
robertphillips976f5f02016-06-03 10:59:20 -0700803}
804
Chris Daltonbbfd5162017-11-07 13:35:22 -0700805void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600806 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400807 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400808 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500809 ASSERT_SINGLE_OWNER_PRIV
810 RETURN_IF_ABANDONED_PRIV
811 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400812 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
813 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500814
Brian Salomon467921e2017-03-06 16:17:12 -0500815 // TODO: extract portions of checkDraw that are relevant to path stenciling.
816 SkASSERT(path);
817 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
818
819 // FIXME: Use path bounds instead of this WAR once
820 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
821 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
822
823 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700824 GrAppliedHardClip appliedClip;
825 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
826 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500827 return;
828 }
829
Robert Phillips7c525e62018-06-12 10:11:12 -0400830 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
831 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600832 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500833 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500834 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400835 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400836 if (!op) {
837 return;
838 }
Brian Salomon97180af2017-03-14 13:42:58 -0400839 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600840
Chris Daltoneffee202019-07-01 22:28:03 -0600841 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600842 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700843}
844
Brian Salomond7065e72018-10-12 11:42:02 -0400845void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500846 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400847 GrAA aa, SkCanvas::SrcRectConstraint constraint,
848 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500849 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400850 ASSERT_SINGLE_OWNER
851 RETURN_IF_ABANDONED
852 SkDEBUGCODE(this->validate();)
853 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500854
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500855 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500856 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400857 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
858 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500859 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500860 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500861 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500862 ctm = viewMatrix;
863 if (set[i].fPreViewMatrix) {
864 ctm.preConcat(*set[i].fPreViewMatrix);
865 }
866
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000867 GrQuad quad, srcQuad;
868 if (set[i].fDstClipQuad) {
869 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
870
871 SkPoint srcPts[4];
872 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
873 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500874 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000875 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
876 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500877 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000878
879 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
880 ? &set[i].fSrcRect : nullptr;
881 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
882 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
883 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500884 }
885 } else {
886 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500887 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600888 GrAAType aaType = this->chooseAAType(aa);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400889 auto clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
890 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
891 : GrTextureOp::Saturate::kNo;
892 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, saturate, aaType, constraint,
893 viewMatrix, std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500894 this->addDrawOp(clip, std::move(op));
895 }
Brian Salomond7065e72018-10-12 11:42:02 -0400896}
897
Brian Osman11052242016-10-27 14:47:55 -0400898void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500899 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400900 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400901 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400902 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400903 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400904 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500905 ASSERT_SINGLE_OWNER
906 RETURN_IF_ABANDONED
907 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400908 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500909
910 AutoCheckFlush acf(this->drawingManager());
911
912 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600913 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400914 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400915 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400916 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400917 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700918}
919
920///////////////////////////////////////////////////////////////////////////////
921
Brian Osman4d92b892019-03-24 00:53:23 +0000922void GrRenderTargetContext::drawAtlas(const GrClip& clip,
923 GrPaint&& paint,
924 const SkMatrix& viewMatrix,
925 int spriteCount,
926 const SkRSXform xform[],
927 const SkRect texRect[],
928 const SkColor colors[]) {
929 ASSERT_SINGLE_OWNER
930 RETURN_IF_ABANDONED
931 SkDEBUGCODE(this->validate();)
932 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
933
934 AutoCheckFlush acf(this->drawingManager());
935
936 GrAAType aaType = this->chooseAAType(GrAA::kNo);
937 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
938 aaType, spriteCount, xform, texRect, colors);
939 this->addDrawOp(clip, std::move(op));
940}
941
942///////////////////////////////////////////////////////////////////////////////
943
Brian Osman11052242016-10-27 14:47:55 -0400944void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500945 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500946 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400947 const SkMatrix& viewMatrix,
948 const SkRRect& rrect,
949 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800950 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700951 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700952 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400953 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400954
955 const SkStrokeRec& stroke = style.strokeRec();
956 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700957 return;
958 }
959
bsalomon7f0d9f32016-08-15 14:49:10 -0700960 GrNoClip noclip;
961 const GrClip* clip = &origClip;
962#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
963 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500964 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700965 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
966 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -0400967 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
968 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -0700969 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -0400970 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
971 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700972 clip = &noclip;
973 }
974#endif
bsalomon6663acf2016-05-10 09:14:17 -0700975 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700976
Robert Phillips72152832017-01-25 17:31:35 -0500977 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700978
Chris Dalton7d6748e2019-03-13 00:34:52 -0600979 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -0500980
Chris Dalton0dffbab2019-03-27 13:08:50 -0600981 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -0400982 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
983 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
984 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
985 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
986 // to avoid perf regressions on some platforms.
987 assert_alive(paint);
988 op = GrOvalOpFactory::MakeCircularRRectOp(
989 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
990 }
991 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600992 assert_alive(paint);
993 op = GrFillRRectOp::Make(
994 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
995 }
Greg Daniel2655ede2019-04-10 00:49:28 +0000996 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600997 assert_alive(paint);
998 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +0000999 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001000 }
1001 if (op) {
1002 this->addDrawOp(*clip, std::move(op));
1003 return;
robertphillipsea461502015-05-26 11:38:03 -07001004 }
robertphillipsb56f9272016-02-25 11:03:52 -08001005
Mike Klein16885072018-12-11 09:54:31 -05001006 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001007 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1008 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001009}
1010
Jim Van Verthc5903412016-11-17 15:27:09 -05001011///////////////////////////////////////////////////////////////////////////////
1012
Jim Van Verth3af1af92017-05-18 15:06:54 -04001013static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1014 SkPoint3 result;
1015 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1016 result.fZ = pt.fZ;
1017 return result;
1018}
1019
1020bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001021 const SkMatrix& viewMatrix,
1022 const SkPath& path,
1023 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001024 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001025 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001026 return true;
1027 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001028 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001029 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001030
1031 // check z plane
1032 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1033 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1034 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1035 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1036 return false;
1037 }
1038
1039 SkRRect rrect;
1040 SkRect rect;
1041 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001042 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001043 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1044 if (!isRRect &&
1045 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1046 rect.width() > SK_ScalarNearlyZero) {
1047 rrect.setOval(rect);
1048 isRRect = true;
1049 }
1050 if (!isRRect && path.isRect(&rect)) {
1051 rrect.setRect(rect);
1052 isRRect = true;
1053 }
1054
1055 if (!isRRect) {
1056 return false;
1057 }
1058
Jim Van Verthc5903412016-11-17 15:27:09 -05001059 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001060 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001061 }
1062
Robert Phillips72152832017-01-25 17:31:35 -05001063 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001064
Jim Van Verth3af1af92017-05-18 15:06:54 -04001065 // transform light
1066 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1067
1068 // 1/scale
1069 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001070 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001071 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1072 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1073
1074 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001075 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1076
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001077 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001078 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1079 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1080 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001081
1082 // Outset the shadow rrect to the border of the penumbra
1083 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1084 SkRRect ambientRRect;
1085 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1086 // If the rrect was an oval then its outset will also be one.
1087 // We set it explicitly to avoid errors.
1088 if (rrect.isOval()) {
1089 ambientRRect = SkRRect::MakeOval(outsetRect);
1090 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001091 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001092 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1093 }
1094
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001095 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001096 if (transparent) {
1097 // set a large inset to force a fill
1098 devSpaceInsetWidth = ambientRRect.width();
1099 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001100
Robert Phillips7c525e62018-06-12 10:11:12 -04001101 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1102 ambientColor,
1103 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001104 ambientRRect,
1105 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001106 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001107 if (op) {
1108 this->addDrawOp(clip, std::move(op));
1109 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001110 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001111
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001112 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001113 SkScalar devSpaceSpotBlur;
1114 SkScalar spotScale;
1115 SkVector spotOffset;
1116 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1117 devLightPos.fZ, rec.fLightRadius,
1118 &devSpaceSpotBlur, &spotScale, &spotOffset);
1119 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001120 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1121
Jim Van Verth3af1af92017-05-18 15:06:54 -04001122 // Adjust translate for the effect of the scale.
1123 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1124 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1125 // This offset is in dev space, need to transform it into source space.
1126 SkMatrix ctmInverse;
1127 if (viewMatrix.invert(&ctmInverse)) {
1128 ctmInverse.mapPoints(&spotOffset, 1);
1129 } else {
1130 // Since the matrix is a similarity, this should never happen, but just in case...
1131 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1132 SkASSERT(false);
1133 }
1134
1135 // Compute the transformed shadow rrect
1136 SkRRect spotShadowRRect;
1137 SkMatrix shadowTransform;
1138 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1139 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001140 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001141
1142 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001143 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001144 SkScalar insetWidth = blurOutset;
1145 if (transparent) {
1146 // If transparent, just do a fill
1147 insetWidth += spotShadowRRect.width();
1148 } else {
1149 // For shadows, instead of using a stroke we specify an inset from the penumbra
1150 // border. We want to extend this inset area so that it meets up with the caster
1151 // geometry. The inset geometry will by default already be inset by the blur width.
1152 //
1153 // We compare the min and max corners inset by the radius between the original
1154 // rrect and the shadow rrect. The distance between the two plus the difference
1155 // between the scaled radius and the original radius gives the distance from the
1156 // transformed shadow shape to the original shape in that corner. The max
1157 // of these gives the maximum distance we need to cover.
1158 //
1159 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1160 // that to get the full insetWidth.
1161 SkScalar maxOffset;
1162 if (rrect.isRect()) {
1163 // Manhattan distance works better for rects
1164 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1165 rrect.rect().fLeft),
1166 SkTAbs(spotShadowRRect.rect().fTop -
1167 rrect.rect().fTop)),
1168 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1169 rrect.rect().fRight),
1170 SkTAbs(spotShadowRRect.rect().fBottom -
1171 rrect.rect().fBottom)));
1172 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001173 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001174 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1175 rrect.rect().fLeft + dr,
1176 spotShadowRRect.rect().fTop -
1177 rrect.rect().fTop + dr);
1178 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1179 rrect.rect().fRight - dr,
1180 spotShadowRRect.rect().fBottom -
1181 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001182 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1183 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001184 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001185 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001186 }
1187
1188 // Outset the shadow rrect to the border of the penumbra
1189 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1190 if (spotShadowRRect.isOval()) {
1191 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1192 } else {
1193 SkScalar outsetRad = spotRadius + blurOutset;
1194 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1195 }
1196
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001197 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001198
Robert Phillips7c525e62018-06-12 10:11:12 -04001199 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1200 spotColor,
1201 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001202 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001203 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001204 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001205 if (op) {
1206 this->addDrawOp(clip, std::move(op));
1207 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001208 }
1209
1210 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001211}
1212
1213///////////////////////////////////////////////////////////////////////////////
1214
Brian Osman11052242016-10-27 14:47:55 -04001215bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001216 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001217 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001218 const SkMatrix& viewMatrix,
1219 const SkRRect& origOuter,
1220 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001221 SkASSERT(!origInner.isEmpty());
1222 SkASSERT(!origOuter.isEmpty());
1223
Brian Salomon65749212017-12-01 16:01:47 -05001224 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1225
Chris Dalton7d6748e2019-03-13 00:34:52 -06001226 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001227
1228 if (GrAAType::kMSAA == aaType) {
1229 return false;
1230 }
1231
Greg Daniel2655ede2019-04-10 00:49:28 +00001232 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1233 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001234 auto outerR = outer->width() / 2.f;
1235 auto innerR = inner->width() / 2.f;
1236 auto cx = outer->getBounds().fLeft + outerR;
1237 auto cy = outer->getBounds().fTop + outerR;
1238 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1239 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1240 auto avgR = (innerR + outerR) / 2.f;
1241 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1242 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1243 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001244 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001245 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001246 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001247 if (op) {
1248 this->addDrawOp(clip, std::move(op));
1249 return true;
1250 }
Mike Klein16885072018-12-11 09:54:31 -05001251 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001252 }
1253 }
1254
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001255 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001256 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001257 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1258 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001259 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001260 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1261 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001262 }
robertphillips00095892016-02-29 13:50:40 -08001263
robertphillips00095892016-02-29 13:50:40 -08001264 SkMatrix inverseVM;
1265 if (!viewMatrix.isIdentity()) {
1266 if (!origInner.transform(viewMatrix, inner.writable())) {
1267 return false;
1268 }
1269 if (!origOuter.transform(viewMatrix, outer.writable())) {
1270 return false;
1271 }
1272 if (!viewMatrix.invert(&inverseVM)) {
1273 return false;
1274 }
1275 } else {
1276 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001277 }
robertphillips00095892016-02-29 13:50:40 -08001278
Ethan Nicholaseace9352018-10-15 20:09:54 +00001279 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001280 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001281 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001282 if (!innerEffect) {
1283 return false;
1284 }
1285
Ethan Nicholaseace9352018-10-15 20:09:54 +00001286 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001287 if (!outerEffect) {
1288 return false;
1289 }
1290
Brian Salomon82f44312017-01-11 13:42:54 -05001291 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1292 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001293
1294 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001295 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001296 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1297 }
halcanary9d524f22016-03-29 09:03:52 -07001298
Brian Salomon82f44312017-01-11 13:42:54 -05001299 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1300 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001301 return true;
1302}
1303
Brian Osman11052242016-10-27 14:47:55 -04001304void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001305 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001306 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001307 const SkMatrix& viewMatrix,
1308 const SkRRect& outer,
1309 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001310 ASSERT_SINGLE_OWNER
1311 RETURN_IF_ABANDONED
1312 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001313 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001314
1315 SkASSERT(!outer.isEmpty());
1316 SkASSERT(!inner.isEmpty());
1317
Robert Phillips72152832017-01-25 17:31:35 -05001318 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001319
Brian Salomon82f44312017-01-11 13:42:54 -05001320 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001321 return;
1322 }
Mike Klein16885072018-12-11 09:54:31 -05001323 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001324
1325 SkPath path;
1326 path.setIsVolatile(true);
1327 path.addRRect(inner);
1328 path.addRRect(outer);
1329 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001330 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001331}
1332
robertphillipsea461502015-05-26 11:38:03 -07001333///////////////////////////////////////////////////////////////////////////////
1334
Brian Osman11052242016-10-27 14:47:55 -04001335void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001336 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001337 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001338 const SkMatrix& viewMatrix,
1339 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001340 const GrStyle& style,
1341 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001342 ASSERT_SINGLE_OWNER
1343 RETURN_IF_ABANDONED
1344 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001345 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001346
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001347 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001348 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001349 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001350 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001351 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1352 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001353 aa = GrAA::kNo;
1354 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001355 }
msarettcc319b92016-08-25 18:07:18 -07001356 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001357 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001358 SkPath path;
1359 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001360 path.setIsVolatile(true);
1361
Brian Salomon82f44312017-01-11 13:42:54 -05001362 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001363 }
1364
Chris Dalton7d6748e2019-03-13 00:34:52 -06001365 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001366 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1367 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001368 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001369}
1370
Brian Osman11052242016-10-27 14:47:55 -04001371void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001372 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001373 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001374 const SkMatrix& viewMatrix,
1375 const SkRect& oval,
1376 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001377 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001378 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001379 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001380 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001381
Robert Phillips7484d202018-07-03 09:09:08 -04001382 const SkStrokeRec& stroke = style.strokeRec();
1383
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001384 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001385 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1386 return;
1387 }
1388
1389 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001390 return;
robertphillipsea461502015-05-26 11:38:03 -07001391 }
1392
Robert Phillips72152832017-01-25 17:31:35 -05001393 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001394
Chris Dalton7d6748e2019-03-13 00:34:52 -06001395 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001396
1397 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001398 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1399 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001400 // We don't draw true circles as round rects in coverage mode, because it can
1401 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1402 assert_alive(paint);
1403 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1404 this->caps()->shaderCaps());
1405 }
1406 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001407 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1408 // the arc equation. This same special geometry and fragment branch also turn out to be a
1409 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1410 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1411 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001412 assert_alive(paint);
1413 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1414 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001415 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001416 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001417 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001418 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1419 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001420 }
1421 if (op) {
1422 this->addDrawOp(clip, std::move(op));
1423 return;
robertphillipsea461502015-05-26 11:38:03 -07001424 }
robertphillipsb56f9272016-02-25 11:03:52 -08001425
Mike Klein16885072018-12-11 09:54:31 -05001426 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001427 this->drawShapeUsingPathRenderer(
1428 clip, std::move(paint), aa, viewMatrix,
Mike Reed4241f5e2019-09-14 19:13:23 +00001429 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001430}
1431
Brian Osman11052242016-10-27 14:47:55 -04001432void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001433 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001434 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001435 const SkMatrix& viewMatrix,
1436 const SkRect& oval,
1437 SkScalar startAngle,
1438 SkScalar sweepAngle,
1439 bool useCenter,
1440 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001441 ASSERT_SINGLE_OWNER
1442 RETURN_IF_ABANDONED
1443 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001444 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001445
1446 AutoCheckFlush acf(this->drawingManager());
1447
Chris Dalton7d6748e2019-03-13 00:34:52 -06001448 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001449 if (GrAAType::kCoverage == aaType) {
1450 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1451 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1452 std::move(paint),
1453 viewMatrix,
1454 oval,
1455 startAngle,
1456 sweepAngle,
1457 useCenter,
1458 style,
1459 shaderCaps);
1460 if (op) {
1461 this->addDrawOp(clip, std::move(op));
1462 return;
1463 }
1464 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001465 }
Brian Salomone4949402018-04-26 15:22:04 -04001466 this->drawShapeUsingPathRenderer(
1467 clip, std::move(paint), aa, viewMatrix,
1468 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001469}
1470
Brian Osman11052242016-10-27 14:47:55 -04001471void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001472 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001473 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001474 sk_sp<GrTextureProxy> image,
1475 sk_sp<GrColorSpaceXform> csxf,
1476 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001477 std::unique_ptr<SkLatticeIter> iter,
1478 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001479 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001480 RETURN_IF_ABANDONED
1481 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001482 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001483
Robert Phillips72152832017-01-25 17:31:35 -05001484 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001485
Brian Salomon2a943df2018-05-04 13:43:19 -04001486 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001487 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1488 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001489 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001490}
1491
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001492void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1493 const SkRect& bounds) {
1494 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1495 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001496 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001497}
1498
Brian Salomon031b0ba2019-05-23 11:05:26 -04001499void GrRenderTargetContext::asyncRescaleAndReadPixels(
1500 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1501 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1502 auto direct = fContext->priv().asDirectContext();
1503 if (!direct) {
1504 callback(context, nullptr, 0);
1505 return;
1506 }
1507 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1508 callback(context, nullptr, 0);
1509 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001510 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001511 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1512 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001513 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001514 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001515 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001516 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001517 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1518 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001519 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001520 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001521 backendFormatOfFinalContext, dstCT);
1522 // Fail if we can't read from the source surface's color type.
1523 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001524 callback(context, nullptr, 0);
1525 return;
1526 }
Brian Salomon624f9062019-07-02 14:23:00 -04001527 // Fail if read color type does not have all of dstCT's color channels and those missing color
1528 // channels are in the src.
1529 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1530 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1531 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1532 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001533 callback(context, nullptr, 0);
1534 return;
1535 }
1536
Brian Salomonbf6b9792019-08-21 09:38:10 -04001537 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001538 int x = srcRect.fLeft;
1539 int y = srcRect.fTop;
1540 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001541 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1542 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001543 callback(context, nullptr, 0);
1544 return;
1545 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001546 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1547 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001548 x = y = 0;
1549 } else {
1550 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001551 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1552 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001553 info.colorSpace(), info.alphaType());
1554 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1555 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001556 // We flip or color convert by drawing and we don't currently support drawing to
1557 // kPremul.
1558 if (info.alphaType() == kUnpremul_SkAlphaType) {
1559 callback(context, nullptr, 0);
1560 return;
1561 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001562 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001563 SkRect srcRectToDraw = SkRect::Make(srcRect);
1564 // If the src is not texturable first try to make a copy to a texture.
1565 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001566 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1567 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1568 SkBudgeted::kNo);
1569 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001570 callback(context, nullptr, 0);
1571 return;
1572 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001573 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1574 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001575 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001576 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1577 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1578 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001579 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001580 callback(context, nullptr, 0);
1581 return;
1582 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001583 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1584 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1585 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1586 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1587 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001588 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001589 }
1590 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001591 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001592 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1593 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001594}
1595
Brian Salomon024bd002019-06-11 11:38:16 -04001596void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1597 ReadPixelsCallback callback,
1598 ReadPixelsContext context) {
1599 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1600 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1601
1602 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1603
1604 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001605 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001606 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1607 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001608 this->colorSpaceInfo().refColorSpace());
1609 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001610 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001611 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001612 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001613 callback(context, pm.addr(), pm.rowBytes());
1614 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001615 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001616
Brian Salomonab32f652019-05-10 14:24:50 -04001617 struct FinishContext {
1618 ReadPixelsCallback* fClientCallback;
1619 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001620 int fW, fH;
1621 SkColorType fColorType;
1622 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001623 };
1624 // Assumption is that the caller would like to flush. We could take a parameter or require an
1625 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1626 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001627 auto* finishContext = new FinishContext{callback, context, rect.width(),
1628 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001629 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001630 const auto* context = reinterpret_cast<const FinishContext*>(c);
1631 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001632 if (!data) {
1633 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1634 delete context;
1635 return;
1636 }
Brian Salomon4d036892019-07-02 15:10:58 -04001637 std::unique_ptr<char[]> tmp;
1638 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001639 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001640 tmp.reset(new char[rowBytes * context->fH]);
1641 context->fTransferResult.fPixelConverter(tmp.get(), data);
1642 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001643 }
Brian Salomon4d036892019-07-02 15:10:58 -04001644 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001645 delete context;
1646 };
1647 GrFlushInfo flushInfo;
1648 flushInfo.fFinishedContext = finishContext;
1649 flushInfo.fFinishedProc = finishCallback;
1650 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1651}
1652
1653void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1654 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1655 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1656 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1657 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1658 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1659 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1660 auto direct = fContext->priv().asDirectContext();
1661 if (!direct) {
1662 callback(context, nullptr, nullptr);
1663 return;
1664 }
1665 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1666 callback(context, nullptr, nullptr);
1667 return;
1668 }
1669 if (dstW & 0x1) {
1670 return;
1671 }
1672 int x = srcRect.fLeft;
1673 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001674 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001675 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1676 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001677 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001678 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1679 dstColorSpace);
1680 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001681 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1682 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001683 callback(context, nullptr, nullptr);
1684 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001685 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001686 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1687 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001688 x = y = 0;
1689 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001690 // We assume the caller wants kPremul. There is no way to indicate a preference.
1691 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1692 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1693 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001694 if (xform) {
1695 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1696 // TODO: Do something if the input is not a texture already.
1697 if (!texProxy) {
1698 callback(context, nullptr, nullptr);
1699 return;
1700 }
Brian Salomon024bd002019-06-11 11:38:16 -04001701 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001702 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001703 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1704 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001705 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001706 callback(context, nullptr, nullptr);
1707 return;
1708 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001709 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1710 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1711 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1712 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1713 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001714 x = y = 0;
1715 }
1716 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001717 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001718 // TODO: Do something if the input is not a texture already.
1719 if (!srcProxy) {
1720 callback(context, nullptr, nullptr);
1721 return;
1722 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001723 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1724 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1725 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1726 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1727 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1728 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1729 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1730 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1731 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001732 if (!yRTC || !uRTC || !vRTC) {
1733 callback(context, nullptr, nullptr);
1734 return;
1735 }
1736
1737 static constexpr float kRec601M[] {
1738 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1739 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1740 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1741 };
1742 static constexpr float kRec709M[] {
1743 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1744 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1745 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1746 };
1747 static constexpr float kJpegM[] {
1748 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1749 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1750 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1751 };
1752 static constexpr float kIM[] {
1753 1.f, 0.f, 0.f, 0.f,
1754 0.f, 1.f, 0.f, 0.f,
1755 0.f, 0.f, 1.f, 0.f,
1756 };
1757 const float* baseM = kIM;
1758 switch (yuvColorSpace) {
1759 case kRec601_SkYUVColorSpace:
1760 baseM = kRec601M;
1761 break;
1762 case kRec709_SkYUVColorSpace:
1763 baseM = kRec709M;
1764 break;
1765 case kJPEG_SkYUVColorSpace:
1766 baseM = kJpegM;
1767 break;
1768 case kIdentity_SkYUVColorSpace:
1769 baseM = kIM;
1770 break;
1771 }
1772 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1773
1774 auto texMatrix = SkMatrix::MakeTrans(x, y);
1775
Michael Ludwig61328202019-06-19 14:48:58 +00001776 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1777 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1778
Brian Salomon024bd002019-06-11 11:38:16 -04001779 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1780 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001781 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001782 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1783 GrPaint yPaint;
1784 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1785 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1786 yPaint.addColorFragmentProcessor(std::move(yFP));
1787 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001788 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1789 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001790 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1791 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1792 if (!yTransfer.fTransferBuffer) {
1793 callback(context, nullptr, nullptr);
1794 return;
1795 }
1796
1797 texMatrix.preScale(2.f, 2.f);
1798 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1799 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001800 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001801 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1802 GrPaint uPaint;
1803 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1804 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1805 uPaint.addColorFragmentProcessor(std::move(uFP));
1806 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001807 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1808 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001809 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1810 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1811 if (!uTransfer.fTransferBuffer) {
1812 callback(context, nullptr, nullptr);
1813 return;
1814 }
1815
1816 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1817 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001818 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001819 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1820 GrPaint vPaint;
1821 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1822 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1823 vPaint.addColorFragmentProcessor(std::move(vFP));
1824 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001825 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1826 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001827 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1828 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1829 if (!vTransfer.fTransferBuffer) {
1830 callback(context, nullptr, nullptr);
1831 return;
1832 }
1833
1834 struct FinishContext {
1835 ReadPixelsCallbackYUV420* fClientCallback;
1836 ReadPixelsContext fClientContext;
1837 int fW, fH;
1838 PixelTransferResult fYTransfer;
1839 PixelTransferResult fUTransfer;
1840 PixelTransferResult fVTransfer;
1841 };
1842 // Assumption is that the caller would like to flush. We could take a parameter or require an
1843 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1844 // callback to GrGpu until after the next flush that flushes our op list, though.
1845 auto* finishContext = new FinishContext{callback,
1846 context,
1847 dstW,
1848 dstH,
1849 std::move(yTransfer),
1850 std::move(uTransfer),
1851 std::move(vTransfer)};
1852 auto finishCallback = [](GrGpuFinishedContext c) {
1853 const auto* context = reinterpret_cast<const FinishContext*>(c);
1854 const void* y = context->fYTransfer.fTransferBuffer->map();
1855 const void* u = context->fUTransfer.fTransferBuffer->map();
1856 const void* v = context->fVTransfer.fTransferBuffer->map();
1857 if (!y || !u || !v) {
1858 if (y) {
1859 context->fYTransfer.fTransferBuffer->unmap();
1860 }
1861 if (u) {
1862 context->fUTransfer.fTransferBuffer->unmap();
1863 }
1864 if (v) {
1865 context->fVTransfer.fTransferBuffer->unmap();
1866 }
1867 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1868 delete context;
1869 return;
1870 }
1871 size_t w = SkToSizeT(context->fW);
1872 size_t h = SkToSizeT(context->fH);
1873 std::unique_ptr<uint8_t[]> yTemp;
1874 if (context->fYTransfer.fPixelConverter) {
1875 yTemp.reset(new uint8_t[w * h]);
1876 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1877 y = yTemp.get();
1878 }
1879 std::unique_ptr<uint8_t[]> uTemp;
1880 if (context->fUTransfer.fPixelConverter) {
1881 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1882 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1883 u = uTemp.get();
1884 }
1885 std::unique_ptr<uint8_t[]> vTemp;
1886 if (context->fVTransfer.fPixelConverter) {
1887 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1888 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1889 v = vTemp.get();
1890 }
1891 const void* data[] = {y, u, v};
1892 size_t rowBytes[] = {w, w / 2, w / 2};
1893 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1894 context->fYTransfer.fTransferBuffer->unmap();
1895 context->fUTransfer.fTransferBuffer->unmap();
1896 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001897 delete context;
1898 };
1899 GrFlushInfo flushInfo;
1900 flushInfo.fFinishedContext = finishContext;
1901 flushInfo.fFinishedProc = finishCallback;
1902 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001903}
1904
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001905GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1906 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001907 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001908 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001909 return GrSemaphoresSubmitted::kNo;
1910 }
robertphillips8c523e02016-07-26 07:41:00 -07001911 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001912 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001913
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001914 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001915}
1916
Greg Danielc64ee462017-06-15 16:59:49 -04001917bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001918 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001919 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001920 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001921 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001922 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001923
1924 AutoCheckFlush acf(this->drawingManager());
1925
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001926 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001927 return false;
1928 }
1929
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001930 auto direct = fContext->priv().asDirectContext();
1931 if (!direct) {
1932 return false;
1933 }
1934
1935 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001936
Greg Danielc30f1a92019-09-06 15:28:58 -04001937 std::unique_ptr<sk_sp<GrSemaphore>[]> grSemaphores(new sk_sp<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001938 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001939 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001940 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1941 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001942 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001943 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1944 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001945 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001946}
joshualitt33a5fce2015-11-18 13:28:51 -08001947
Robert Phillips65a88fa2017-08-08 08:36:22 -04001948void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001949 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06001950 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001951}
1952
Brian Osman11052242016-10-27 14:47:55 -04001953void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001954 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001955 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001956 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001957 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001958 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001959 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001960 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001961 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001962 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1963
Brian Salomon40b77a62017-12-22 11:25:52 -05001964 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001965
Robert Phillips27927a52018-08-20 13:18:12 -04001966 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001967}
1968
1969void GrRenderTargetContext::drawShape(const GrClip& clip,
1970 GrPaint&& paint,
1971 GrAA aa,
1972 const SkMatrix& viewMatrix,
1973 const GrShape& shape) {
1974 ASSERT_SINGLE_OWNER
1975 RETURN_IF_ABANDONED
1976 SkDEBUGCODE(this->validate();)
1977 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1978
Brian Salomon2fad74a2017-12-20 13:28:55 -05001979 if (shape.isEmpty()) {
1980 if (shape.inverseFilled()) {
1981 this->drawPaint(clip, std::move(paint), viewMatrix);
1982 }
1983 return;
robertphillipsea461502015-05-26 11:38:03 -07001984 }
1985
Robert Phillips72152832017-01-25 17:31:35 -05001986 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001987
Brian Salomon2fad74a2017-12-20 13:28:55 -05001988 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06001989 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001990 SkRRect rrect;
1991 // We can ignore the starting point and direction since there is no path effect.
1992 bool inverted;
1993 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1994 if (rrect.isRect()) {
1995 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1996 &shape.style());
1997 return;
1998 } else if (rrect.isOval()) {
1999 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002000 return;
2001 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002002 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2003 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002004 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2005 viewMatrix.rectStaysRect()) {
2006 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2007 // the matrix to all the points individually rather than just to the rect
2008 SkRect rects[2];
2009 if (shape.asNestedRects(rects)) {
2010 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002011 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002012 fContext, std::move(paint), viewMatrix, rects);
2013 if (op) {
2014 this->addDrawOp(clip, std::move(op));
2015 }
2016 // Returning here indicates that there is nothing to draw in this case.
2017 return;
2018 }
robertphillipsea461502015-05-26 11:38:03 -07002019 }
2020 }
robertphillips4bc31812016-03-01 12:22:49 -08002021
Brian Salomon2fad74a2017-12-20 13:28:55 -05002022 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002023}
2024
Chris Daltonbbfd5162017-11-07 13:35:22 -07002025bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002026 const GrUserStencilSettings* ss,
2027 SkRegion::Op op,
2028 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002029 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002030 const SkMatrix& viewMatrix,
2031 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002032 ASSERT_SINGLE_OWNER_PRIV
2033 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002034 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002035 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2036 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002037
2038 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002039 GrPaint paint;
2040 paint.setCoverageSetOpXPFactory(op, invert);
2041 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2042 SkRect::MakeIWH(fRenderTargetContext->width(),
2043 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002044 return true;
2045 }
2046
Robert Phillips72152832017-01-25 17:31:35 -05002047 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002048
2049 // An Assumption here is that path renderer would use some form of tweaking
2050 // the src color (either the input alpha or in the frag shader) to implement
2051 // aa. If we have some future driver-mojo path AA that can do the right
2052 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002053 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002054 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002055
Chris Daltondb91c6e2017-09-08 16:25:08 -06002056 SkIRect clipConservativeBounds;
2057 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2058 &clipConservativeBounds, nullptr);
2059
bsalomon8acedde2016-06-24 10:42:16 -07002060 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002061 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002062 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002063 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002064 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002065 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002066 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002067 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002068 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2069 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002070 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002071
2072 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002073 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002074 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002075 if (!pr) {
2076 return false;
2077 }
2078
2079 GrPaint paint;
2080 paint.setCoverageSetOpXPFactory(op, invert);
2081
Brian Salomonf3569f02017-10-24 12:52:33 -04002082 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2083 std::move(paint),
2084 ss,
2085 fRenderTargetContext,
2086 &clip,
2087 &clipConservativeBounds,
2088 &viewMatrix,
2089 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002090 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002091 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002092 pr->drawPath(args);
2093 return true;
2094}
2095
Brian Osman11052242016-10-27 14:47:55 -04002096SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002097 ASSERT_SINGLE_OWNER_PRIV
2098
Robert Phillips6a6de562019-02-15 15:19:15 -05002099 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002100 return SkBudgeted::kNo;
2101 }
2102
Brian Osman11052242016-10-27 14:47:55 -04002103 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002104
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002105 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002106}
2107
Brian Salomon2fad74a2017-12-20 13:28:55 -05002108void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2109 GrPaint&& paint,
2110 GrAA aa,
2111 const SkMatrix& viewMatrix,
2112 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002113 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002114 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002115 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2116
Jim Van Verthf86073a2018-10-02 13:05:38 -04002117 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2118 return;
2119 }
2120
Chris Daltondb91c6e2017-09-08 16:25:08 -06002121 SkIRect clipConservativeBounds;
2122 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2123
Brian Salomon2fad74a2017-12-20 13:28:55 -05002124 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002125 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002126
robertphillips68737822015-10-29 12:12:21 -07002127 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002128 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002129 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002130 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002131 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002132 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002133 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002134 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002135
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002136 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002137 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002138 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002139 return;
2140 }
2141
Chris Dalton6ce447a2019-06-23 18:07:38 -06002142 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002143
2144 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2145 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2146 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2147
Brian Salomon2fad74a2017-12-20 13:28:55 -05002148 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002149 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002150 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2151 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002152 return;
2153 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002154 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002155 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002156 }
2157 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002158 if (canDrawArgs.fShape->style().applies()) {
2159 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2160 styleScale);
2161 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002162 return;
2163 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002164 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002165 // This time, allow SW renderer
2166 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2167 } else {
2168 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002169 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002170 }
robertphillipsea461502015-05-26 11:38:03 -07002171
bsalomon8acedde2016-06-24 10:42:16 -07002172 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002173#ifdef SK_DEBUG
2174 SkDebugf("Unable to find path renderer compatible with path.\n");
2175#endif
2176 return;
2177 }
2178
Robert Phillips256c37b2017-03-01 14:32:46 -05002179 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002180 std::move(paint),
2181 &GrUserStencilSettings::kUnused,
2182 this,
2183 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002184 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002185 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002186 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002187 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002188 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002189 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002190}
2191
Brian Salomon467921e2017-03-06 16:17:12 -05002192static void op_bounds(SkRect* bounds, const GrOp* op) {
2193 *bounds = op->bounds();
2194 if (op->hasZeroArea()) {
2195 if (op->hasAABloat()) {
2196 bounds->outset(0.5f, 0.5f);
2197 } else {
2198 // We don't know which way the particular GPU will snap lines or points at integer
2199 // coords. So we ensure that the bounds is large enough for either snap.
2200 SkRect before = *bounds;
2201 bounds->roundOut(bounds);
2202 if (bounds->fLeft == before.fLeft) {
2203 bounds->fLeft -= 1;
2204 }
2205 if (bounds->fTop == before.fTop) {
2206 bounds->fTop -= 1;
2207 }
2208 if (bounds->fRight == before.fRight) {
2209 bounds->fRight += 1;
2210 }
2211 if (bounds->fBottom == before.fBottom) {
2212 bounds->fBottom += 1;
2213 }
2214 }
2215 }
2216}
2217
Chris Dalton08755122019-08-05 16:13:47 -06002218void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002219 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002220 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2221}
2222
Brian Salomon348a0372018-10-31 10:42:18 -04002223void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2224 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002225 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002226 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002227 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002228 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002229 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002230 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002231 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002232 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002233
Brian Salomon467921e2017-03-06 16:17:12 -05002234 // Setup clip
2235 SkRect bounds;
2236 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002237 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002238 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002239 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2240 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2241
2242 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002243 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002244 }
2245
2246 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002247 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002248 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002249 }
2250
Chris Daltoneffee202019-07-01 22:28:03 -06002251 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002252
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002253 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002254 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2255 // here as well.
2256 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2257#ifdef SK_DEBUG
2258 if (hasMixedSampledCoverage) {
2259 SkASSERT(usesStencil);
2260 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2261 }
2262#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002263 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002264 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2265
2266 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002267 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002268 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002269 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002270 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002271 }
2272 }
2273
2274 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002275 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002276 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002277 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002278 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002279 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2280 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002281}
2282
Chris Dalton2243c7b2019-08-21 14:46:35 -06002283bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2284 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002285 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2286 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2287 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002288 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002289 return false;
2290 }
2291
Chris Dalton2243c7b2019-08-21 14:46:35 -06002292 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2293 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002294 // The render target is a texture, so we can read from it directly in the shader. The XP
2295 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002296 dstProxy->setProxy(sk_ref_sp(texProxy));
2297 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002298 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002299 }
2300 }
2301
Chris Dalton2243c7b2019-08-21 14:46:35 -06002302 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002303
Eric Karl74480882017-04-03 14:49:05 -07002304 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002305 clip.getConservativeBounds(
2306 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002307 SkRect opBounds = op.bounds();
2308 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2309 // 0.5 pixels.
2310 if (op.hasAABloat() || op.hasZeroArea()) {
2311 opBounds.outset(0.5f, 0.5f);
2312 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2313 // performance we may ignore the clip when the draw is entirely inside the clip is float
2314 // space but will hit pixels just outside the clip when actually rasterizing.
2315 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002316 clippedRect.intersect(SkIRect::MakeWH(
2317 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002318 }
2319 SkIRect opIBounds;
2320 opBounds.roundOut(&opIBounds);
2321 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002322#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002323 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002324#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002325 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002326 }
2327
2328 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2329 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002330 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002331 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002332
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002333 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002334 copyRect = clippedRect;
2335 }
Brian Salomon467921e2017-03-06 16:17:12 -05002336
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002337 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002338 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002339 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002340 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002341 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002342 } else {
Eric Karl74480882017-04-03 14:49:05 -07002343 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002344 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002345 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002346 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2347 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2348 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002349 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002350
Greg Daniel46cfbc62019-06-07 11:43:30 -04002351 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002352 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002353 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002354}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002355
2356bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2357 const SkIPoint& dstPoint) {
2358 SkIRect clippedSrcRect;
2359 SkIPoint clippedDstPoint;
2360 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2361 &clippedSrcRect, &clippedDstPoint)) {
2362 return false;
2363 }
2364
2365 GrPaint paint;
2366 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2367 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2368 SkMatrix::I());
2369 if (!fp) {
2370 return false;
2371 }
2372 paint.addColorFragmentProcessor(std::move(fp));
2373
2374 this->fillRectToRect(
2375 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2376 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2377 clippedSrcRect.height()),
2378 SkRect::Make(clippedSrcRect));
2379 return true;
2380}
2381