blob: 29bceb3b585dcc478076fd6ab1ac9eac65760b74 [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
Chris Dalton69371032019-09-18 15:44:25 +0000345void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect) {
346 ASSERT_SINGLE_OWNER_PRIV
347 RETURN_IF_ABANDONED_PRIV
348 SkDEBUGCODE(fRenderTargetContext->validate();)
349 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
350 fRenderTargetContext->fContext);
351
352 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
353
354 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
355 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
356
357 if (clearRect) {
358 if (clearRect->contains(rtRect)) {
359 clearRect = nullptr; // full screen
360 } else {
361 if (!rtRect.intersect(*clearRect)) {
362 return;
363 }
364 }
365 }
366
367 static const SkPMColor4f kColor = SK_PMColor4fTRANSPARENT;
368
369 // TODO: in a post-MDB world this should be handled at the OpsTask level.
370 // This makes sure to always add an op to the list, instead of marking the clear as a load op.
371 // This code follows very similar logic to internalClear() below, but critical differences are
372 // highlighted in line related to absClear()'s unique behavior.
373 if (clearRect) {
374 if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
375 GrPaint paint;
376 clear_to_grpaint(kColor, &paint);
377
378 // Use the disabled clip; the rect geometry already matches the clear rectangle and
379 // if it were added to a scissor, that would be intersected with the logical surface
380 // bounds and not the worst case dimensions required here.
381 fRenderTargetContext->addDrawOp(
382 GrFixedClip::Disabled(),
383 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
384 SkMatrix::I(), SkRect::Make(rtRect)));
385 } else {
386 // Must use the ClearOp factory that takes a boolean (false) instead of a surface
387 // proxy. The surface proxy variant would intersect the clip rect with its logical
388 // bounds, which is not desired in this special case.
389 fRenderTargetContext->addOp(GrClearOp::Make(
390 fRenderTargetContext->fContext, rtRect, kColor, /* fullscreen */ false));
391 }
392 } else {
393 if (fRenderTargetContext->getOpsTask()->resetForFullscreenClear(
394 fRenderTargetContext->canDiscardPreviousOpsOnFullClear()) &&
395 !fRenderTargetContext->caps()->performColorClearsAsDraws()) {
396 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kClear, kColor);
397 } else {
398 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kDiscard);
399
400 if (fRenderTargetContext->caps()->performColorClearsAsDraws()) {
401 // This draws a quad covering the worst case dimensions instead of just the logical
402 // width and height like in internalClear().
403 GrPaint paint;
404 clear_to_grpaint(kColor, &paint);
405 fRenderTargetContext->addDrawOp(
406 GrFixedClip::Disabled(),
407 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext,
408 std::move(paint), SkMatrix::I(),
409 SkRect::Make(rtRect)));
410 } else {
411 // Nothing special about this path in absClear compared to internalClear()
412 fRenderTargetContext->addOp(GrClearOp::Make(
413 fRenderTargetContext->fContext, SkIRect::MakeEmpty(), kColor,
414 /* fullscreen */ true));
415 }
416 }
417 }
418}
419
Brian Osman11052242016-10-27 14:47:55 -0400420void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500421 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400422 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000423 // Start with the render target, since that is the maximum content we could possibly fill.
424 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400425 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400426 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000427 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
428 // inverse view matrix.
429 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
430 } else {
431 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
432 SkMatrix localMatrix;
433 if (!viewMatrix.invert(&localMatrix)) {
434 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400435 }
Michael Ludwig61328202019-06-19 14:48:58 +0000436 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
437 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700438 }
robertphillipsea461502015-05-26 11:38:03 -0700439}
440
Michael Ludwig61328202019-06-19 14:48:58 +0000441enum class GrRenderTargetContext::QuadOptimization {
442 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
443 kDiscarded,
444 // The rect to draw was converted to some other op and appended to the oplist, so no additional
445 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
446 // a constColor is provided.
447 kSubmitted,
448 // The clip was folded into the device quad, with updated edge flags and local coords, and
449 // caller is responsible for adding an appropriate op.
450 kClipApplied,
451 // No change to clip, but quad updated to better fit clip/render target, and caller is
452 // responsible for adding an appropriate op.
453 kCropped
454};
Michael Ludwig61a16512019-01-15 11:15:13 -0500455
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400456static bool make_vertex_finite(float* value) {
457 if (SkScalarIsNaN(*value)) {
458 return false;
459 }
460
461 if (!SkScalarIsFinite(*value)) {
462 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
463 // left when calculating crops.
464 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
465 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
466 }
467
468 return true;
469}
470
Michael Ludwig61328202019-06-19 14:48:58 +0000471GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400472 const GrClip& clip, const SkPMColor4f* constColor,
473 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
474 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000475 // Optimization requirements:
476 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
477 // 2. kClear applies when constColor and final geom is pixel aligned rect;
478 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
479 // 3. kRRect applies when constColor and rrect clip and quad covers clip
480 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
481 // 5. kCropped applies when rect quad (currently)
482 // 6. kNone always applies
483 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000484
Michael Ludwige08b4432019-06-19 18:00:48 -0400485 SkRect rtRect;
486 if (stencilSettings) {
487 // Must use worst case bounds so that stencil buffer updates on approximately sized render
488 // targets don't get corrupted.
489 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
490 fRenderTargetProxy->worstCaseHeight());
491 } else {
492 // Use the logical size of the render target, which allows for "fullscreen" clears even if
493 // the render target has an approximate backing fit
494 rtRect = SkRect::MakeWH(this->width(), this->height());
495 }
496
Michael Ludwig61328202019-06-19 14:48:58 +0000497 SkRect drawBounds = deviceQuad->bounds();
498 if (constColor) {
499 // Don't bother updating local coordinates when the paint will ignore them anyways
500 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400501 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
502 // will be cropped to the finite 'clip' or render target and there is no local space mapping
503 if (!deviceQuad->isFinite()) {
504 for (int i = 0; i < 4; ++i) {
505 if (!make_vertex_finite(deviceQuad->xs() + i) ||
506 !make_vertex_finite(deviceQuad->ys() + i) ||
507 !make_vertex_finite(deviceQuad->ws() + i)) {
508 // Discard if we see a nan
509 return QuadOptimization::kDiscarded;
510 }
511 }
512 SkASSERT(deviceQuad->isFinite());
513 }
514 } else {
515 // CropToRect requires the quads to be finite. If they are not finite and we have local
516 // coordinates, the mapping from local space to device space is poorly defined so drop it
517 if (!deviceQuad->isFinite()) {
518 return QuadOptimization::kDiscarded;
519 }
Brian Salomon7694b902019-06-18 21:00:21 +0000520 }
521
Michael Ludwig61328202019-06-19 14:48:58 +0000522 // If the quad is entirely off screen, it doesn't matter what the clip does
523 if (!rtRect.intersects(drawBounds)) {
524 return QuadOptimization::kDiscarded;
525 }
Brian Salomon7694b902019-06-18 21:00:21 +0000526
Michael Ludwig61328202019-06-19 14:48:58 +0000527 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
528 // the render target).
529 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400530 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
531 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
532 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
533 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000534 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000535 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000536 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
537 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000538 }
Brian Salomon7694b902019-06-18 21:00:21 +0000539 }
540
Michael Ludwig61328202019-06-19 14:48:58 +0000541 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
542 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400543 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000544 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
545 // of the render target and the clip rect)
546 SkRect clipBounds = rtRect;
547 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
548 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000549 }
550
Michael Ludwig61328202019-06-19 14:48:58 +0000551 if (clipRRect.isRect()) {
552 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
553 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
554 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
555 // Clear optimization is possible
556 drawBounds = deviceQuad->bounds();
557 if (drawBounds.contains(rtRect)) {
558 // Fullscreen clear
559 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
560 return QuadOptimization::kSubmitted;
561 } else if (GrClip::IsPixelAligned(drawBounds) &&
562 drawBounds.width() > 256 && drawBounds.height() > 256) {
563 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
564 SkIRect scissorRect;
565 drawBounds.round(&scissorRect);
566 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
567 return QuadOptimization::kSubmitted;
568 }
569 }
570
571 // Update overall AA setting.
572 *edgeFlags = newFlags;
573 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
574 newFlags != GrQuadAAFlags::kNone) {
575 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
576 // properly reflect the smooth edge of the clip.
577 *aa = GrAA::kYes;
578 }
579 // We intentionally do not downgrade AA here because we don't know if we need to
580 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
581 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
582
583 // deviceQuad is exactly the intersection of original quad and clip, so it can be
584 // drawn with no clip (submitted by caller)
585 return QuadOptimization::kClipApplied;
586 } else {
587 // The quads have been updated to better fit the clip bounds, but can't get rid of
588 // the clip entirely
589 return QuadOptimization::kCropped;
590 }
591 } else if (constColor) {
592 // Rounded corners and constant filled color (limit ourselves to solid colors because
593 // there is no way to use custom local coordinates with drawRRect).
594 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
595 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
596 deviceQuad->bounds().contains(clipBounds)) {
597 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
598 // we can draw the rrect directly and ignore the edge flags
599 GrPaint paint;
600 clear_to_grpaint(*constColor, &paint);
601 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
602 clipRRect, GrStyle::SimpleFill());
603 return QuadOptimization::kSubmitted;
604 } else {
605 // The quad has been updated to better fit clip bounds, but can't remove the clip
606 return QuadOptimization::kCropped;
607 }
Brian Salomon7694b902019-06-18 21:00:21 +0000608 }
Brian Salomon7694b902019-06-18 21:00:21 +0000609 }
610
Michael Ludwig61328202019-06-19 14:48:58 +0000611 // Crop the quad to the conservative bounds of the clip.
612 SkIRect clipDevBounds;
613 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
614 SkRect clipBounds = SkRect::Make(clipDevBounds);
615
616 // One final check for discarding, since we may have gone here directly due to a complex clip
617 if (!clipBounds.intersects(drawBounds)) {
618 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000619 }
620
Michael Ludwig61328202019-06-19 14:48:58 +0000621 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
622 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
623 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
624
625 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000626}
627
Michael Ludwig61328202019-06-19 14:48:58 +0000628void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500629 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500630 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000631 GrQuadAAFlags edgeFlags,
632 const GrQuad& deviceQuad,
633 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400634 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000635 ASSERT_SINGLE_OWNER
636 RETURN_IF_ABANDONED
637 SkDEBUGCODE(this->validate();)
638 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500639
Michael Ludwig61328202019-06-19 14:48:58 +0000640 AutoCheckFlush acf(this->drawingManager());
641
642 SkPMColor4f* constColor = nullptr;
643 SkPMColor4f paintColor;
644 if (!ss && !paint.numCoverageFragmentProcessors() &&
645 paint.isConstantBlendedColor(&paintColor)) {
646 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
647 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500648 }
649
Michael Ludwig61328202019-06-19 14:48:58 +0000650 GrQuad croppedDeviceQuad = deviceQuad;
651 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400652 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
653 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000654 if (opt >= QuadOptimization::kClipApplied) {
655 // These optimizations require caller to add an op themselves
656 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
657 : clip;
658 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
659 : this->chooseAAType(aa);
660 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
661 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700662 }
Michael Ludwig61328202019-06-19 14:48:58 +0000663 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800664}
665
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000666void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
667 sk_sp<GrTextureProxy> proxy,
668 sk_sp<GrColorSpaceXform> textureXform,
669 GrSamplerState::Filter filter,
670 const SkPMColor4f& color,
671 SkBlendMode blendMode,
672 GrAA aa,
673 GrQuadAAFlags edgeFlags,
674 const GrQuad& deviceQuad,
675 const GrQuad& localQuad,
676 const SkRect* domain) {
677 ASSERT_SINGLE_OWNER
678 RETURN_IF_ABANDONED
679 SkDEBUGCODE(this->validate();)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400680 SkASSERT(proxy);
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000681 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
682
683 AutoCheckFlush acf(this->drawingManager());
684
685 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
686 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
687 GrQuad croppedDeviceQuad = deviceQuad;
688 GrQuad croppedLocalQuad = localQuad;
689 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
690 &croppedDeviceQuad, &croppedLocalQuad);
691
692 SkASSERT(opt != QuadOptimization::kSubmitted);
693 if (opt != QuadOptimization::kDiscarded) {
694 // And the texture op if not discarded
695 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
696 : clip;
697 GrAAType aaType = this->chooseAAType(aa);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400698 auto clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
699 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
700 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000701 // Use the provided domain, although hypothetically we could detect that the cropped local
702 // quad is sufficiently inside the domain and the constraint could be dropped.
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400703 this->addDrawOp(finalClip,
704 GrTextureOp::Make(fContext, std::move(proxy), std::move(textureXform),
705 filter, color, saturate, blendMode, aaType, edgeFlags,
706 croppedDeviceQuad, croppedLocalQuad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000707 }
708}
709
Brian Osman11052242016-10-27 14:47:55 -0400710void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500711 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500712 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400713 const SkMatrix& viewMatrix,
714 const SkRect& rect,
715 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700716 if (!style) {
717 style = &GrStyle::SimpleFill();
718 }
joshualitt1de610a2016-01-06 08:26:09 -0800719 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700720 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700721 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400722 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700723
bsalomon6663acf2016-05-10 09:14:17 -0700724 // Path effects should've been devolved to a path in SkGpuDevice
725 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700726
Robert Phillips72152832017-01-25 17:31:35 -0500727 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700728
bsalomon6663acf2016-05-10 09:14:17 -0700729 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400730 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000731 // Fills the rect, using rect as its own local coordinates
732 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500733 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700734 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
735 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
736 if ((!rect.width() || !rect.height()) &&
737 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
738 SkScalar r = stroke.getWidth() / 2;
739 // TODO: Move these stroke->fill fallbacks to GrShape?
740 switch (stroke.getJoin()) {
741 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500742 this->drawRect(
743 clip, std::move(paint), aa, viewMatrix,
744 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
745 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700746 return;
747 case SkPaint::kRound_Join:
748 // Raster draws nothing when both dimensions are empty.
749 if (rect.width() || rect.height()){
750 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500751 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
752 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700753 return;
754 }
755 case SkPaint::kBevel_Join:
756 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500757 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700758 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
759 &GrStyle::SimpleFill());
760 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500761 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700762 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
763 &GrStyle::SimpleFill());
764 }
765 return;
766 }
767 }
robertphillips44302392016-07-08 14:43:03 -0700768
Brian Salomonbaaf4392017-06-15 09:59:23 -0400769 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700770
Chris Dalton7d6748e2019-03-13 00:34:52 -0600771 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500772 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
773 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
774 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500775 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400776 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700777 return;
robertphillips4bc31812016-03-01 12:22:49 -0800778 }
robertphillips4bc31812016-03-01 12:22:49 -0800779 }
Mike Klein16885072018-12-11 09:54:31 -0500780 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500781 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700782}
783
Michael Ludwig69858532018-11-28 15:34:34 -0500784void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
785 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
786 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600787 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500788 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
789 quads, cnt));
790}
791
Robert Phillipsec2249f2016-11-09 08:54:35 -0500792int GrRenderTargetContextPriv::maxWindowRectangles() const {
793 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400794 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500795}
796
Greg Danielf41b2bd2019-08-22 16:19:24 -0400797GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600798 ) const {
799#if GR_TEST_UTILS
800 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400801 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600802 }
803#endif
804 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
805 // would normally be overwritten. The one exception is if the render target context is marked as
806 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
807 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
808 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
809 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400810 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600811}
812
Chris Daltoneffee202019-07-01 22:28:03 -0600813void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
814 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600815 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600816 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600817
Chris Daltoneffee202019-07-01 22:28:03 -0600818 int numRequiredSamples = this->numSamples();
819 if (multisampled && 1 == numRequiredSamples) {
820 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
821 // mixed samples.
822 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
823 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400824 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600825 }
826 SkASSERT(numRequiredSamples > 0);
827
828 if (numRequiredSamples > fNumStencilSamples) {
829 fNumStencilSamples = numRequiredSamples;
830 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
831 }
Chris Dalton6b982802019-06-27 13:53:46 -0600832
833 if (needsStencilClear) {
834 if (this->caps()->performStencilClearsAsDraws()) {
835 // There is a driver bug with clearing stencil. We must use an op to manually clear the
836 // stencil buffer before the op that required 'setNeedsStencil'.
837 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
838 } else {
839 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
840 // code note when the instantiated stencil buffer is already clear and skip the clear
841 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
842 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400843 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600844 }
845 }
846}
847
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000848void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700849 ASSERT_SINGLE_OWNER_PRIV
850 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400851 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400852 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
853 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700854
Robert Phillips72152832017-01-25 17:31:35 -0500855 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400856
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500857 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
858}
859
860void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
861 if (this->caps()->performStencilClearsAsDraws()) {
862 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
863 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
864
865 // Configure the paint to have no impact on the color buffer
866 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400867 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400868 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
869 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500870 } else {
871 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
872 fRenderTargetProxy.get()));
873 if (!op) {
874 return;
875 }
Chris Dalton08755122019-08-05 16:13:47 -0600876 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500877 }
robertphillips976f5f02016-06-03 10:59:20 -0700878}
879
Chris Daltonbbfd5162017-11-07 13:35:22 -0700880void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600881 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400882 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400883 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500884 ASSERT_SINGLE_OWNER_PRIV
885 RETURN_IF_ABANDONED_PRIV
886 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400887 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
888 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500889
Brian Salomon467921e2017-03-06 16:17:12 -0500890 // TODO: extract portions of checkDraw that are relevant to path stenciling.
891 SkASSERT(path);
892 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
893
894 // FIXME: Use path bounds instead of this WAR once
895 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
896 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
897
898 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700899 GrAppliedHardClip appliedClip;
900 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
901 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500902 return;
903 }
904
Robert Phillips7c525e62018-06-12 10:11:12 -0400905 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
906 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600907 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500908 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500909 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400910 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400911 if (!op) {
912 return;
913 }
Brian Salomon97180af2017-03-14 13:42:58 -0400914 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600915
Chris Daltoneffee202019-07-01 22:28:03 -0600916 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600917 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700918}
919
Brian Salomond7065e72018-10-12 11:42:02 -0400920void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500921 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400922 GrAA aa, SkCanvas::SrcRectConstraint constraint,
923 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500924 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400925 ASSERT_SINGLE_OWNER
926 RETURN_IF_ABANDONED
927 SkDEBUGCODE(this->validate();)
928 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500929
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500930 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500931 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400932 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
933 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500934 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500935 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500936 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500937 ctm = viewMatrix;
938 if (set[i].fPreViewMatrix) {
939 ctm.preConcat(*set[i].fPreViewMatrix);
940 }
941
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000942 GrQuad quad, srcQuad;
943 if (set[i].fDstClipQuad) {
944 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
945
946 SkPoint srcPts[4];
947 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
948 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500949 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000950 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
951 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500952 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000953
954 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
955 ? &set[i].fSrcRect : nullptr;
956 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
957 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
958 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500959 }
960 } else {
961 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500962 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600963 GrAAType aaType = this->chooseAAType(aa);
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400964 auto clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
965 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
966 : GrTextureOp::Saturate::kNo;
967 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, saturate, aaType, constraint,
968 viewMatrix, std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500969 this->addDrawOp(clip, std::move(op));
970 }
Brian Salomond7065e72018-10-12 11:42:02 -0400971}
972
Brian Osman11052242016-10-27 14:47:55 -0400973void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500974 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400975 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400976 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400977 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400978 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400979 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500980 ASSERT_SINGLE_OWNER
981 RETURN_IF_ABANDONED
982 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400983 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500984
985 AutoCheckFlush acf(this->drawingManager());
986
987 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600988 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400989 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400990 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400991 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400992 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700993}
994
995///////////////////////////////////////////////////////////////////////////////
996
Brian Osman4d92b892019-03-24 00:53:23 +0000997void GrRenderTargetContext::drawAtlas(const GrClip& clip,
998 GrPaint&& paint,
999 const SkMatrix& viewMatrix,
1000 int spriteCount,
1001 const SkRSXform xform[],
1002 const SkRect texRect[],
1003 const SkColor colors[]) {
1004 ASSERT_SINGLE_OWNER
1005 RETURN_IF_ABANDONED
1006 SkDEBUGCODE(this->validate();)
1007 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1008
1009 AutoCheckFlush acf(this->drawingManager());
1010
1011 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1012 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1013 aaType, spriteCount, xform, texRect, colors);
1014 this->addDrawOp(clip, std::move(op));
1015}
1016
1017///////////////////////////////////////////////////////////////////////////////
1018
Brian Osman11052242016-10-27 14:47:55 -04001019void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001020 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001021 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001022 const SkMatrix& viewMatrix,
1023 const SkRRect& rrect,
1024 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001025 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001026 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001027 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001028 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001029
1030 const SkStrokeRec& stroke = style.strokeRec();
1031 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001032 return;
1033 }
1034
bsalomon7f0d9f32016-08-15 14:49:10 -07001035 GrNoClip noclip;
1036 const GrClip* clip = &origClip;
1037#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1038 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001039 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001040 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1041 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001042 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1043 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001044 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001045 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1046 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001047 clip = &noclip;
1048 }
1049#endif
bsalomon6663acf2016-05-10 09:14:17 -07001050 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001051
Robert Phillips72152832017-01-25 17:31:35 -05001052 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001053
Chris Dalton7d6748e2019-03-13 00:34:52 -06001054 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001055
Chris Dalton0dffbab2019-03-27 13:08:50 -06001056 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001057 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1058 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1059 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1060 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1061 // to avoid perf regressions on some platforms.
1062 assert_alive(paint);
1063 op = GrOvalOpFactory::MakeCircularRRectOp(
1064 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1065 }
1066 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001067 assert_alive(paint);
1068 op = GrFillRRectOp::Make(
1069 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1070 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001071 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001072 assert_alive(paint);
1073 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001074 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001075 }
1076 if (op) {
1077 this->addDrawOp(*clip, std::move(op));
1078 return;
robertphillipsea461502015-05-26 11:38:03 -07001079 }
robertphillipsb56f9272016-02-25 11:03:52 -08001080
Mike Klein16885072018-12-11 09:54:31 -05001081 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001082 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1083 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001084}
1085
Jim Van Verthc5903412016-11-17 15:27:09 -05001086///////////////////////////////////////////////////////////////////////////////
1087
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1089 SkPoint3 result;
1090 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1091 result.fZ = pt.fZ;
1092 return result;
1093}
1094
1095bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001096 const SkMatrix& viewMatrix,
1097 const SkPath& path,
1098 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001099 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001100 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001101 return true;
1102 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001103 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001104 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001105
1106 // check z plane
1107 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1108 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1109 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1110 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1111 return false;
1112 }
1113
1114 SkRRect rrect;
1115 SkRect rect;
1116 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001117 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001118 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1119 if (!isRRect &&
1120 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1121 rect.width() > SK_ScalarNearlyZero) {
1122 rrect.setOval(rect);
1123 isRRect = true;
1124 }
1125 if (!isRRect && path.isRect(&rect)) {
1126 rrect.setRect(rect);
1127 isRRect = true;
1128 }
1129
1130 if (!isRRect) {
1131 return false;
1132 }
1133
Jim Van Verthc5903412016-11-17 15:27:09 -05001134 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001135 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001136 }
1137
Robert Phillips72152832017-01-25 17:31:35 -05001138 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001139
Jim Van Verth3af1af92017-05-18 15:06:54 -04001140 // transform light
1141 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1142
1143 // 1/scale
1144 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001145 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001146 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1147 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1148
1149 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001150 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1151
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001152 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001153 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1154 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1155 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001156
1157 // Outset the shadow rrect to the border of the penumbra
1158 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1159 SkRRect ambientRRect;
1160 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1161 // If the rrect was an oval then its outset will also be one.
1162 // We set it explicitly to avoid errors.
1163 if (rrect.isOval()) {
1164 ambientRRect = SkRRect::MakeOval(outsetRect);
1165 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001166 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001167 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1168 }
1169
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001170 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001171 if (transparent) {
1172 // set a large inset to force a fill
1173 devSpaceInsetWidth = ambientRRect.width();
1174 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001175
Robert Phillips7c525e62018-06-12 10:11:12 -04001176 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1177 ambientColor,
1178 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001179 ambientRRect,
1180 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001181 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001182 if (op) {
1183 this->addDrawOp(clip, std::move(op));
1184 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001185 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001186
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001187 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001188 SkScalar devSpaceSpotBlur;
1189 SkScalar spotScale;
1190 SkVector spotOffset;
1191 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1192 devLightPos.fZ, rec.fLightRadius,
1193 &devSpaceSpotBlur, &spotScale, &spotOffset);
1194 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001195 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1196
Jim Van Verth3af1af92017-05-18 15:06:54 -04001197 // Adjust translate for the effect of the scale.
1198 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1199 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1200 // This offset is in dev space, need to transform it into source space.
1201 SkMatrix ctmInverse;
1202 if (viewMatrix.invert(&ctmInverse)) {
1203 ctmInverse.mapPoints(&spotOffset, 1);
1204 } else {
1205 // Since the matrix is a similarity, this should never happen, but just in case...
1206 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1207 SkASSERT(false);
1208 }
1209
1210 // Compute the transformed shadow rrect
1211 SkRRect spotShadowRRect;
1212 SkMatrix shadowTransform;
1213 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1214 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001215 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001216
1217 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001218 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001219 SkScalar insetWidth = blurOutset;
1220 if (transparent) {
1221 // If transparent, just do a fill
1222 insetWidth += spotShadowRRect.width();
1223 } else {
1224 // For shadows, instead of using a stroke we specify an inset from the penumbra
1225 // border. We want to extend this inset area so that it meets up with the caster
1226 // geometry. The inset geometry will by default already be inset by the blur width.
1227 //
1228 // We compare the min and max corners inset by the radius between the original
1229 // rrect and the shadow rrect. The distance between the two plus the difference
1230 // between the scaled radius and the original radius gives the distance from the
1231 // transformed shadow shape to the original shape in that corner. The max
1232 // of these gives the maximum distance we need to cover.
1233 //
1234 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1235 // that to get the full insetWidth.
1236 SkScalar maxOffset;
1237 if (rrect.isRect()) {
1238 // Manhattan distance works better for rects
1239 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1240 rrect.rect().fLeft),
1241 SkTAbs(spotShadowRRect.rect().fTop -
1242 rrect.rect().fTop)),
1243 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1244 rrect.rect().fRight),
1245 SkTAbs(spotShadowRRect.rect().fBottom -
1246 rrect.rect().fBottom)));
1247 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001248 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001249 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1250 rrect.rect().fLeft + dr,
1251 spotShadowRRect.rect().fTop -
1252 rrect.rect().fTop + dr);
1253 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1254 rrect.rect().fRight - dr,
1255 spotShadowRRect.rect().fBottom -
1256 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001257 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1258 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001259 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001260 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001261 }
1262
1263 // Outset the shadow rrect to the border of the penumbra
1264 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1265 if (spotShadowRRect.isOval()) {
1266 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1267 } else {
1268 SkScalar outsetRad = spotRadius + blurOutset;
1269 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1270 }
1271
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001272 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001273
Robert Phillips7c525e62018-06-12 10:11:12 -04001274 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1275 spotColor,
1276 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001277 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001278 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001279 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001280 if (op) {
1281 this->addDrawOp(clip, std::move(op));
1282 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001283 }
1284
1285 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001286}
1287
1288///////////////////////////////////////////////////////////////////////////////
1289
Brian Osman11052242016-10-27 14:47:55 -04001290bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001291 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001292 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001293 const SkMatrix& viewMatrix,
1294 const SkRRect& origOuter,
1295 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001296 SkASSERT(!origInner.isEmpty());
1297 SkASSERT(!origOuter.isEmpty());
1298
Brian Salomon65749212017-12-01 16:01:47 -05001299 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1300
Chris Dalton7d6748e2019-03-13 00:34:52 -06001301 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001302
1303 if (GrAAType::kMSAA == aaType) {
1304 return false;
1305 }
1306
Greg Daniel2655ede2019-04-10 00:49:28 +00001307 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1308 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001309 auto outerR = outer->width() / 2.f;
1310 auto innerR = inner->width() / 2.f;
1311 auto cx = outer->getBounds().fLeft + outerR;
1312 auto cy = outer->getBounds().fTop + outerR;
1313 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1314 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1315 auto avgR = (innerR + outerR) / 2.f;
1316 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1317 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1318 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001319 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001320 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001321 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001322 if (op) {
1323 this->addDrawOp(clip, std::move(op));
1324 return true;
1325 }
Mike Klein16885072018-12-11 09:54:31 -05001326 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001327 }
1328 }
1329
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001330 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001331 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001332 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1333 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001334 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001335 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1336 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001337 }
robertphillips00095892016-02-29 13:50:40 -08001338
robertphillips00095892016-02-29 13:50:40 -08001339 SkMatrix inverseVM;
1340 if (!viewMatrix.isIdentity()) {
1341 if (!origInner.transform(viewMatrix, inner.writable())) {
1342 return false;
1343 }
1344 if (!origOuter.transform(viewMatrix, outer.writable())) {
1345 return false;
1346 }
1347 if (!viewMatrix.invert(&inverseVM)) {
1348 return false;
1349 }
1350 } else {
1351 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001352 }
robertphillips00095892016-02-29 13:50:40 -08001353
Ethan Nicholaseace9352018-10-15 20:09:54 +00001354 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001355 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001356 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001357 if (!innerEffect) {
1358 return false;
1359 }
1360
Ethan Nicholaseace9352018-10-15 20:09:54 +00001361 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001362 if (!outerEffect) {
1363 return false;
1364 }
1365
Brian Salomon82f44312017-01-11 13:42:54 -05001366 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1367 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001368
1369 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001370 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001371 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1372 }
halcanary9d524f22016-03-29 09:03:52 -07001373
Brian Salomon82f44312017-01-11 13:42:54 -05001374 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1375 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001376 return true;
1377}
1378
Brian Osman11052242016-10-27 14:47:55 -04001379void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001380 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001381 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001382 const SkMatrix& viewMatrix,
1383 const SkRRect& outer,
1384 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001385 ASSERT_SINGLE_OWNER
1386 RETURN_IF_ABANDONED
1387 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001388 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001389
1390 SkASSERT(!outer.isEmpty());
1391 SkASSERT(!inner.isEmpty());
1392
Robert Phillips72152832017-01-25 17:31:35 -05001393 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001394
Brian Salomon82f44312017-01-11 13:42:54 -05001395 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001396 return;
1397 }
Mike Klein16885072018-12-11 09:54:31 -05001398 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001399
1400 SkPath path;
1401 path.setIsVolatile(true);
1402 path.addRRect(inner);
1403 path.addRRect(outer);
1404 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001405 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001406}
1407
robertphillipsea461502015-05-26 11:38:03 -07001408///////////////////////////////////////////////////////////////////////////////
1409
Brian Osman11052242016-10-27 14:47:55 -04001410void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001411 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001412 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001413 const SkMatrix& viewMatrix,
1414 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001415 const GrStyle& style,
1416 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001417 ASSERT_SINGLE_OWNER
1418 RETURN_IF_ABANDONED
1419 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001420 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001421
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001422 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001423 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001424 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001426 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1427 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001428 aa = GrAA::kNo;
1429 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001430 }
msarettcc319b92016-08-25 18:07:18 -07001431 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001432 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001433 SkPath path;
1434 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001435 path.setIsVolatile(true);
1436
Brian Salomon82f44312017-01-11 13:42:54 -05001437 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001438 }
1439
Chris Dalton7d6748e2019-03-13 00:34:52 -06001440 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001441 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1442 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001443 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001444}
1445
Brian Osman11052242016-10-27 14:47:55 -04001446void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001447 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001448 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001449 const SkMatrix& viewMatrix,
1450 const SkRect& oval,
1451 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001452 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001453 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001454 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001455 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001456
Robert Phillips7484d202018-07-03 09:09:08 -04001457 const SkStrokeRec& stroke = style.strokeRec();
1458
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001459 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001460 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1461 return;
1462 }
1463
1464 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001465 return;
robertphillipsea461502015-05-26 11:38:03 -07001466 }
1467
Robert Phillips72152832017-01-25 17:31:35 -05001468 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001469
Chris Dalton7d6748e2019-03-13 00:34:52 -06001470 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001471
1472 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001473 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1474 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001475 // We don't draw true circles as round rects in coverage mode, because it can
1476 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1477 assert_alive(paint);
1478 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1479 this->caps()->shaderCaps());
1480 }
1481 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001482 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1483 // the arc equation. This same special geometry and fragment branch also turn out to be a
1484 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1485 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1486 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001487 assert_alive(paint);
1488 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1489 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001490 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001491 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001492 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001493 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1494 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001495 }
1496 if (op) {
1497 this->addDrawOp(clip, std::move(op));
1498 return;
robertphillipsea461502015-05-26 11:38:03 -07001499 }
robertphillipsb56f9272016-02-25 11:03:52 -08001500
Mike Klein16885072018-12-11 09:54:31 -05001501 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001502 this->drawShapeUsingPathRenderer(
1503 clip, std::move(paint), aa, viewMatrix,
Mike Reed4241f5e2019-09-14 19:13:23 +00001504 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001505}
1506
Brian Osman11052242016-10-27 14:47:55 -04001507void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001508 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001509 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001510 const SkMatrix& viewMatrix,
1511 const SkRect& oval,
1512 SkScalar startAngle,
1513 SkScalar sweepAngle,
1514 bool useCenter,
1515 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001516 ASSERT_SINGLE_OWNER
1517 RETURN_IF_ABANDONED
1518 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001519 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001520
1521 AutoCheckFlush acf(this->drawingManager());
1522
Chris Dalton7d6748e2019-03-13 00:34:52 -06001523 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001524 if (GrAAType::kCoverage == aaType) {
1525 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1526 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1527 std::move(paint),
1528 viewMatrix,
1529 oval,
1530 startAngle,
1531 sweepAngle,
1532 useCenter,
1533 style,
1534 shaderCaps);
1535 if (op) {
1536 this->addDrawOp(clip, std::move(op));
1537 return;
1538 }
1539 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001540 }
Brian Salomone4949402018-04-26 15:22:04 -04001541 this->drawShapeUsingPathRenderer(
1542 clip, std::move(paint), aa, viewMatrix,
1543 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001544}
1545
Brian Osman11052242016-10-27 14:47:55 -04001546void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001547 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001548 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001549 sk_sp<GrTextureProxy> image,
1550 sk_sp<GrColorSpaceXform> csxf,
1551 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001552 std::unique_ptr<SkLatticeIter> iter,
1553 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001554 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001555 RETURN_IF_ABANDONED
1556 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001557 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001558
Robert Phillips72152832017-01-25 17:31:35 -05001559 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001560
Brian Salomon2a943df2018-05-04 13:43:19 -04001561 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001562 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1563 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001564 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001565}
1566
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001567void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1568 const SkRect& bounds) {
1569 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1570 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001571 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001572}
1573
Brian Salomon031b0ba2019-05-23 11:05:26 -04001574void GrRenderTargetContext::asyncRescaleAndReadPixels(
1575 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1576 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1577 auto direct = fContext->priv().asDirectContext();
1578 if (!direct) {
1579 callback(context, nullptr, 0);
1580 return;
1581 }
1582 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1583 callback(context, nullptr, 0);
1584 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001585 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001586 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1587 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001588 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001589 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001590 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001591 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001592 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1593 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001594 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001595 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001596 backendFormatOfFinalContext, dstCT);
1597 // Fail if we can't read from the source surface's color type.
1598 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001599 callback(context, nullptr, 0);
1600 return;
1601 }
Brian Salomon624f9062019-07-02 14:23:00 -04001602 // Fail if read color type does not have all of dstCT's color channels and those missing color
1603 // channels are in the src.
1604 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1605 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1606 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1607 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001608 callback(context, nullptr, 0);
1609 return;
1610 }
1611
Brian Salomonbf6b9792019-08-21 09:38:10 -04001612 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001613 int x = srcRect.fLeft;
1614 int y = srcRect.fTop;
1615 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001616 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1617 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001618 callback(context, nullptr, 0);
1619 return;
1620 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001621 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1622 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001623 x = y = 0;
1624 } else {
1625 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001626 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1627 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001628 info.colorSpace(), info.alphaType());
1629 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1630 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001631 // We flip or color convert by drawing and we don't currently support drawing to
1632 // kPremul.
1633 if (info.alphaType() == kUnpremul_SkAlphaType) {
1634 callback(context, nullptr, 0);
1635 return;
1636 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001637 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001638 SkRect srcRectToDraw = SkRect::Make(srcRect);
1639 // If the src is not texturable first try to make a copy to a texture.
1640 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001641 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1642 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1643 SkBudgeted::kNo);
1644 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001645 callback(context, nullptr, 0);
1646 return;
1647 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001648 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1649 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001650 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001651 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1652 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1653 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001654 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001655 callback(context, nullptr, 0);
1656 return;
1657 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001658 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1659 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1660 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1661 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1662 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001663 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001664 }
1665 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001666 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001667 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1668 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001669}
1670
Brian Salomon024bd002019-06-11 11:38:16 -04001671void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1672 ReadPixelsCallback callback,
1673 ReadPixelsContext context) {
1674 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1675 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1676
1677 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1678
1679 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001680 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001681 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1682 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001683 this->colorSpaceInfo().refColorSpace());
1684 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001685 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001686 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001687 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001688 callback(context, pm.addr(), pm.rowBytes());
1689 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001690 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001691
Brian Salomonab32f652019-05-10 14:24:50 -04001692 struct FinishContext {
1693 ReadPixelsCallback* fClientCallback;
1694 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001695 int fW, fH;
1696 SkColorType fColorType;
1697 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001698 };
1699 // Assumption is that the caller would like to flush. We could take a parameter or require an
1700 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1701 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001702 auto* finishContext = new FinishContext{callback, context, rect.width(),
1703 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001704 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001705 const auto* context = reinterpret_cast<const FinishContext*>(c);
1706 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001707 if (!data) {
1708 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1709 delete context;
1710 return;
1711 }
Brian Salomon4d036892019-07-02 15:10:58 -04001712 std::unique_ptr<char[]> tmp;
1713 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001714 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001715 tmp.reset(new char[rowBytes * context->fH]);
1716 context->fTransferResult.fPixelConverter(tmp.get(), data);
1717 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001718 }
Brian Salomon4d036892019-07-02 15:10:58 -04001719 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001720 delete context;
1721 };
1722 GrFlushInfo flushInfo;
1723 flushInfo.fFinishedContext = finishContext;
1724 flushInfo.fFinishedProc = finishCallback;
1725 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1726}
1727
1728void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1729 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1730 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1731 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1732 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1733 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1734 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1735 auto direct = fContext->priv().asDirectContext();
1736 if (!direct) {
1737 callback(context, nullptr, nullptr);
1738 return;
1739 }
1740 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1741 callback(context, nullptr, nullptr);
1742 return;
1743 }
1744 if (dstW & 0x1) {
1745 return;
1746 }
1747 int x = srcRect.fLeft;
1748 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001749 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001750 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1751 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001752 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001753 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1754 dstColorSpace);
1755 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001756 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1757 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001758 callback(context, nullptr, nullptr);
1759 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001760 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001761 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1762 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001763 x = y = 0;
1764 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001765 // We assume the caller wants kPremul. There is no way to indicate a preference.
1766 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1767 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1768 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001769 if (xform) {
1770 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1771 // TODO: Do something if the input is not a texture already.
1772 if (!texProxy) {
1773 callback(context, nullptr, nullptr);
1774 return;
1775 }
Brian Salomon024bd002019-06-11 11:38:16 -04001776 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001777 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001778 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1779 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001780 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001781 callback(context, nullptr, nullptr);
1782 return;
1783 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001784 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1785 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1786 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1787 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1788 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001789 x = y = 0;
1790 }
1791 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001792 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001793 // TODO: Do something if the input is not a texture already.
1794 if (!srcProxy) {
1795 callback(context, nullptr, nullptr);
1796 return;
1797 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001798 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1799 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1800 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1801 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1802 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1803 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1804 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1805 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1806 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001807 if (!yRTC || !uRTC || !vRTC) {
1808 callback(context, nullptr, nullptr);
1809 return;
1810 }
1811
1812 static constexpr float kRec601M[] {
1813 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1814 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1815 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1816 };
1817 static constexpr float kRec709M[] {
1818 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1819 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1820 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1821 };
1822 static constexpr float kJpegM[] {
1823 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1824 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1825 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1826 };
1827 static constexpr float kIM[] {
1828 1.f, 0.f, 0.f, 0.f,
1829 0.f, 1.f, 0.f, 0.f,
1830 0.f, 0.f, 1.f, 0.f,
1831 };
1832 const float* baseM = kIM;
1833 switch (yuvColorSpace) {
1834 case kRec601_SkYUVColorSpace:
1835 baseM = kRec601M;
1836 break;
1837 case kRec709_SkYUVColorSpace:
1838 baseM = kRec709M;
1839 break;
1840 case kJPEG_SkYUVColorSpace:
1841 baseM = kJpegM;
1842 break;
1843 case kIdentity_SkYUVColorSpace:
1844 baseM = kIM;
1845 break;
1846 }
1847 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1848
1849 auto texMatrix = SkMatrix::MakeTrans(x, y);
1850
Michael Ludwig61328202019-06-19 14:48:58 +00001851 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1852 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1853
Brian Salomon024bd002019-06-11 11:38:16 -04001854 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1855 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001856 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001857 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1858 GrPaint yPaint;
1859 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1860 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1861 yPaint.addColorFragmentProcessor(std::move(yFP));
1862 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001863 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1864 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001865 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1866 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1867 if (!yTransfer.fTransferBuffer) {
1868 callback(context, nullptr, nullptr);
1869 return;
1870 }
1871
1872 texMatrix.preScale(2.f, 2.f);
1873 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1874 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001875 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001876 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1877 GrPaint uPaint;
1878 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1879 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1880 uPaint.addColorFragmentProcessor(std::move(uFP));
1881 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001882 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1883 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001884 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1885 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1886 if (!uTransfer.fTransferBuffer) {
1887 callback(context, nullptr, nullptr);
1888 return;
1889 }
1890
1891 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1892 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001893 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001894 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1895 GrPaint vPaint;
1896 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1897 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1898 vPaint.addColorFragmentProcessor(std::move(vFP));
1899 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001900 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1901 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001902 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1903 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1904 if (!vTransfer.fTransferBuffer) {
1905 callback(context, nullptr, nullptr);
1906 return;
1907 }
1908
1909 struct FinishContext {
1910 ReadPixelsCallbackYUV420* fClientCallback;
1911 ReadPixelsContext fClientContext;
1912 int fW, fH;
1913 PixelTransferResult fYTransfer;
1914 PixelTransferResult fUTransfer;
1915 PixelTransferResult fVTransfer;
1916 };
1917 // Assumption is that the caller would like to flush. We could take a parameter or require an
1918 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1919 // callback to GrGpu until after the next flush that flushes our op list, though.
1920 auto* finishContext = new FinishContext{callback,
1921 context,
1922 dstW,
1923 dstH,
1924 std::move(yTransfer),
1925 std::move(uTransfer),
1926 std::move(vTransfer)};
1927 auto finishCallback = [](GrGpuFinishedContext c) {
1928 const auto* context = reinterpret_cast<const FinishContext*>(c);
1929 const void* y = context->fYTransfer.fTransferBuffer->map();
1930 const void* u = context->fUTransfer.fTransferBuffer->map();
1931 const void* v = context->fVTransfer.fTransferBuffer->map();
1932 if (!y || !u || !v) {
1933 if (y) {
1934 context->fYTransfer.fTransferBuffer->unmap();
1935 }
1936 if (u) {
1937 context->fUTransfer.fTransferBuffer->unmap();
1938 }
1939 if (v) {
1940 context->fVTransfer.fTransferBuffer->unmap();
1941 }
1942 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1943 delete context;
1944 return;
1945 }
1946 size_t w = SkToSizeT(context->fW);
1947 size_t h = SkToSizeT(context->fH);
1948 std::unique_ptr<uint8_t[]> yTemp;
1949 if (context->fYTransfer.fPixelConverter) {
1950 yTemp.reset(new uint8_t[w * h]);
1951 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1952 y = yTemp.get();
1953 }
1954 std::unique_ptr<uint8_t[]> uTemp;
1955 if (context->fUTransfer.fPixelConverter) {
1956 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1957 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1958 u = uTemp.get();
1959 }
1960 std::unique_ptr<uint8_t[]> vTemp;
1961 if (context->fVTransfer.fPixelConverter) {
1962 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1963 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1964 v = vTemp.get();
1965 }
1966 const void* data[] = {y, u, v};
1967 size_t rowBytes[] = {w, w / 2, w / 2};
1968 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1969 context->fYTransfer.fTransferBuffer->unmap();
1970 context->fUTransfer.fTransferBuffer->unmap();
1971 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001972 delete context;
1973 };
1974 GrFlushInfo flushInfo;
1975 flushInfo.fFinishedContext = finishContext;
1976 flushInfo.fFinishedProc = finishCallback;
1977 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001978}
1979
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001980GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1981 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001982 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001983 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001984 return GrSemaphoresSubmitted::kNo;
1985 }
robertphillips8c523e02016-07-26 07:41:00 -07001986 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001987 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001988
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001989 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001990}
1991
Greg Danielc64ee462017-06-15 16:59:49 -04001992bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001993 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001994 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001995 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001996 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001997 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001998
1999 AutoCheckFlush acf(this->drawingManager());
2000
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002001 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002002 return false;
2003 }
2004
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002005 auto direct = fContext->priv().asDirectContext();
2006 if (!direct) {
2007 return false;
2008 }
2009
2010 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002011
Greg Danielc30f1a92019-09-06 15:28:58 -04002012 std::unique_ptr<sk_sp<GrSemaphore>[]> grSemaphores(new sk_sp<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002013 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002014 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002015 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2016 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002017 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002018 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2019 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002020 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002021}
joshualitt33a5fce2015-11-18 13:28:51 -08002022
Robert Phillips65a88fa2017-08-08 08:36:22 -04002023void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04002024 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06002025 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04002026}
2027
Brian Osman11052242016-10-27 14:47:55 -04002028void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002029 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002030 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002031 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002032 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002033 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002034 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002035 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002036 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002037 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2038
Brian Salomon40b77a62017-12-22 11:25:52 -05002039 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002040
Robert Phillips27927a52018-08-20 13:18:12 -04002041 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002042}
2043
2044void GrRenderTargetContext::drawShape(const GrClip& clip,
2045 GrPaint&& paint,
2046 GrAA aa,
2047 const SkMatrix& viewMatrix,
2048 const GrShape& shape) {
2049 ASSERT_SINGLE_OWNER
2050 RETURN_IF_ABANDONED
2051 SkDEBUGCODE(this->validate();)
2052 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2053
Brian Salomon2fad74a2017-12-20 13:28:55 -05002054 if (shape.isEmpty()) {
2055 if (shape.inverseFilled()) {
2056 this->drawPaint(clip, std::move(paint), viewMatrix);
2057 }
2058 return;
robertphillipsea461502015-05-26 11:38:03 -07002059 }
2060
Robert Phillips72152832017-01-25 17:31:35 -05002061 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002062
Brian Salomon2fad74a2017-12-20 13:28:55 -05002063 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002064 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002065 SkRRect rrect;
2066 // We can ignore the starting point and direction since there is no path effect.
2067 bool inverted;
2068 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2069 if (rrect.isRect()) {
2070 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2071 &shape.style());
2072 return;
2073 } else if (rrect.isOval()) {
2074 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002075 return;
2076 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002077 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2078 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002079 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2080 viewMatrix.rectStaysRect()) {
2081 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2082 // the matrix to all the points individually rather than just to the rect
2083 SkRect rects[2];
2084 if (shape.asNestedRects(rects)) {
2085 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002086 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002087 fContext, std::move(paint), viewMatrix, rects);
2088 if (op) {
2089 this->addDrawOp(clip, std::move(op));
2090 }
2091 // Returning here indicates that there is nothing to draw in this case.
2092 return;
2093 }
robertphillipsea461502015-05-26 11:38:03 -07002094 }
2095 }
robertphillips4bc31812016-03-01 12:22:49 -08002096
Brian Salomon2fad74a2017-12-20 13:28:55 -05002097 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002098}
2099
Chris Daltonbbfd5162017-11-07 13:35:22 -07002100bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002101 const GrUserStencilSettings* ss,
2102 SkRegion::Op op,
2103 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002104 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002105 const SkMatrix& viewMatrix,
2106 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002107 ASSERT_SINGLE_OWNER_PRIV
2108 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002109 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002110 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2111 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002112
2113 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002114 GrPaint paint;
2115 paint.setCoverageSetOpXPFactory(op, invert);
2116 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2117 SkRect::MakeIWH(fRenderTargetContext->width(),
2118 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002119 return true;
2120 }
2121
Robert Phillips72152832017-01-25 17:31:35 -05002122 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002123
2124 // An Assumption here is that path renderer would use some form of tweaking
2125 // the src color (either the input alpha or in the frag shader) to implement
2126 // aa. If we have some future driver-mojo path AA that can do the right
2127 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002128 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002129 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002130
Chris Daltondb91c6e2017-09-08 16:25:08 -06002131 SkIRect clipConservativeBounds;
2132 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2133 &clipConservativeBounds, nullptr);
2134
bsalomon8acedde2016-06-24 10:42:16 -07002135 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002136 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002137 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002138 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002139 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002140 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002141 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002142 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002143 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2144 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002145 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002146
2147 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002148 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002149 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002150 if (!pr) {
2151 return false;
2152 }
2153
2154 GrPaint paint;
2155 paint.setCoverageSetOpXPFactory(op, invert);
2156
Brian Salomonf3569f02017-10-24 12:52:33 -04002157 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2158 std::move(paint),
2159 ss,
2160 fRenderTargetContext,
2161 &clip,
2162 &clipConservativeBounds,
2163 &viewMatrix,
2164 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002165 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002166 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002167 pr->drawPath(args);
2168 return true;
2169}
2170
Brian Osman11052242016-10-27 14:47:55 -04002171SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002172 ASSERT_SINGLE_OWNER_PRIV
2173
Robert Phillips6a6de562019-02-15 15:19:15 -05002174 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002175 return SkBudgeted::kNo;
2176 }
2177
Brian Osman11052242016-10-27 14:47:55 -04002178 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002179
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002180 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002181}
2182
Brian Salomon2fad74a2017-12-20 13:28:55 -05002183void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2184 GrPaint&& paint,
2185 GrAA aa,
2186 const SkMatrix& viewMatrix,
2187 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002188 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002189 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002190 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2191
Jim Van Verthf86073a2018-10-02 13:05:38 -04002192 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2193 return;
2194 }
2195
Chris Daltondb91c6e2017-09-08 16:25:08 -06002196 SkIRect clipConservativeBounds;
2197 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2198
Brian Salomon2fad74a2017-12-20 13:28:55 -05002199 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002200 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002201
robertphillips68737822015-10-29 12:12:21 -07002202 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002203 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002204 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002205 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002206 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002207 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002208 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002209 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002210
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002211 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002212 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002213 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002214 return;
2215 }
2216
Chris Dalton6ce447a2019-06-23 18:07:38 -06002217 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002218
2219 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2220 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2221 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2222
Brian Salomon2fad74a2017-12-20 13:28:55 -05002223 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002224 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002225 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2226 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002227 return;
2228 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002229 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002230 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002231 }
2232 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002233 if (canDrawArgs.fShape->style().applies()) {
2234 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2235 styleScale);
2236 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002237 return;
2238 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002239 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002240 // This time, allow SW renderer
2241 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2242 } else {
2243 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002244 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002245 }
robertphillipsea461502015-05-26 11:38:03 -07002246
bsalomon8acedde2016-06-24 10:42:16 -07002247 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002248#ifdef SK_DEBUG
2249 SkDebugf("Unable to find path renderer compatible with path.\n");
2250#endif
2251 return;
2252 }
2253
Robert Phillips256c37b2017-03-01 14:32:46 -05002254 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002255 std::move(paint),
2256 &GrUserStencilSettings::kUnused,
2257 this,
2258 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002259 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002260 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002261 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002262 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002263 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002264 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002265}
2266
Brian Salomon467921e2017-03-06 16:17:12 -05002267static void op_bounds(SkRect* bounds, const GrOp* op) {
2268 *bounds = op->bounds();
2269 if (op->hasZeroArea()) {
2270 if (op->hasAABloat()) {
2271 bounds->outset(0.5f, 0.5f);
2272 } else {
2273 // We don't know which way the particular GPU will snap lines or points at integer
2274 // coords. So we ensure that the bounds is large enough for either snap.
2275 SkRect before = *bounds;
2276 bounds->roundOut(bounds);
2277 if (bounds->fLeft == before.fLeft) {
2278 bounds->fLeft -= 1;
2279 }
2280 if (bounds->fTop == before.fTop) {
2281 bounds->fTop -= 1;
2282 }
2283 if (bounds->fRight == before.fRight) {
2284 bounds->fRight += 1;
2285 }
2286 if (bounds->fBottom == before.fBottom) {
2287 bounds->fBottom += 1;
2288 }
2289 }
2290 }
2291}
2292
Chris Dalton08755122019-08-05 16:13:47 -06002293void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002294 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002295 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2296}
2297
Brian Salomon348a0372018-10-31 10:42:18 -04002298void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2299 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002300 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002301 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002302 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002303 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002304 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002305 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002306 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002307 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002308
Brian Salomon467921e2017-03-06 16:17:12 -05002309 // Setup clip
2310 SkRect bounds;
2311 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002312 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002313 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002314 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2315 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2316
2317 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002318 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002319 }
2320
2321 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002322 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002323 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002324 }
2325
Chris Daltoneffee202019-07-01 22:28:03 -06002326 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002327
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002328 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002329 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2330 // here as well.
2331 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2332#ifdef SK_DEBUG
2333 if (hasMixedSampledCoverage) {
2334 SkASSERT(usesStencil);
2335 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2336 }
2337#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002338 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002339 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2340
2341 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002342 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002343 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002344 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002345 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002346 }
2347 }
2348
2349 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002350 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002351 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002352 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002353 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002354 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2355 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002356}
2357
Chris Dalton2243c7b2019-08-21 14:46:35 -06002358bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2359 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002360 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2361 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2362 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002363 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002364 return false;
2365 }
2366
Chris Dalton2243c7b2019-08-21 14:46:35 -06002367 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2368 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002369 // The render target is a texture, so we can read from it directly in the shader. The XP
2370 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002371 dstProxy->setProxy(sk_ref_sp(texProxy));
2372 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002373 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002374 }
2375 }
2376
Chris Dalton2243c7b2019-08-21 14:46:35 -06002377 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002378
Eric Karl74480882017-04-03 14:49:05 -07002379 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002380 clip.getConservativeBounds(
2381 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002382 SkRect opBounds = op.bounds();
2383 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2384 // 0.5 pixels.
2385 if (op.hasAABloat() || op.hasZeroArea()) {
2386 opBounds.outset(0.5f, 0.5f);
2387 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2388 // performance we may ignore the clip when the draw is entirely inside the clip is float
2389 // space but will hit pixels just outside the clip when actually rasterizing.
2390 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002391 clippedRect.intersect(SkIRect::MakeWH(
2392 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002393 }
2394 SkIRect opIBounds;
2395 opBounds.roundOut(&opIBounds);
2396 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002397#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002398 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002399#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002400 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002401 }
2402
2403 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2404 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002405 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002406 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002407
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002408 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002409 copyRect = clippedRect;
2410 }
Brian Salomon467921e2017-03-06 16:17:12 -05002411
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002412 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002413 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002414 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002415 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002416 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002417 } else {
Eric Karl74480882017-04-03 14:49:05 -07002418 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002419 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002420 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002421 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2422 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2423 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002424 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002425
Greg Daniel46cfbc62019-06-07 11:43:30 -04002426 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002427 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002428 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002429}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002430
2431bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2432 const SkIPoint& dstPoint) {
2433 SkIRect clippedSrcRect;
2434 SkIPoint clippedDstPoint;
2435 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2436 &clippedSrcRect, &clippedDstPoint)) {
2437 return false;
2438 }
2439
2440 GrPaint paint;
2441 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2442 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2443 SkMatrix::I());
2444 if (!fp) {
2445 return false;
2446 }
2447 paint.addColorFragmentProcessor(std::move(fp));
2448
2449 this->fillRectToRect(
2450 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2451 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2452 clippedSrcRect.height()),
2453 SkRect::Make(clippedSrcRect));
2454 return true;
2455}
2456