blob: 26813d667ab5193a46dc291b45232a103207ad1a [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkDrawable.h"
9#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "include/private/SkShadowFlags.h"
12#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkDrawShadowInfo.h"
16#include "src/core/SkGlyphRunPainter.h"
17#include "src/core/SkLatticeIter.h"
18#include "src/core/SkMatrixPriv.h"
19#include "src/core/SkRRectPriv.h"
20#include "src/core/SkSurfacePriv.h"
21#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrBlurUtils.h"
24#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040027#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrDrawingManager.h"
29#include "src/gpu/GrFixedClip.h"
30#include "src/gpu/GrGpuResourcePriv.h"
31#include "src/gpu/GrMemoryPool.h"
32#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040034#include "src/gpu/GrRenderTarget.h"
Mike Klein52337de2019-07-25 09:00:52 -050035#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrRenderTargetContextPriv.h"
37#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrStencilAttachment.h"
39#include "src/gpu/GrStyle.h"
40#include "src/gpu/GrTracing.h"
41#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040042#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/effects/GrRRectEffect.h"
44#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040045#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040046#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000047#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040048#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/ops/GrAtlasTextOp.h"
50#include "src/gpu/ops/GrClearOp.h"
51#include "src/gpu/ops/GrClearStencilClipOp.h"
52#include "src/gpu/ops/GrDebugMarkerOp.h"
53#include "src/gpu/ops/GrDrawAtlasOp.h"
54#include "src/gpu/ops/GrDrawOp.h"
55#include "src/gpu/ops/GrDrawVerticesOp.h"
56#include "src/gpu/ops/GrDrawableOp.h"
57#include "src/gpu/ops/GrFillRRectOp.h"
58#include "src/gpu/ops/GrFillRectOp.h"
59#include "src/gpu/ops/GrLatticeOp.h"
60#include "src/gpu/ops/GrOp.h"
61#include "src/gpu/ops/GrOvalOpFactory.h"
62#include "src/gpu/ops/GrRegionOp.h"
63#include "src/gpu/ops/GrSemaphoreOp.h"
64#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
Greg Daniel674ee742019-08-27 13:12:33 -0400345void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500346 ASSERT_SINGLE_OWNER_PRIV
347 RETURN_IF_ABANDONED_PRIV
348 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400349 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
350 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500351
Robert Phillips72152832017-01-25 17:31:35 -0500352 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500353
Brian Salomonbb5711a2017-05-17 13:49:59 -0400354 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
355 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500356
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
Greg Daniel674ee742019-08-27 13:12:33 -0400367 static const SkPMColor4f kColor = SK_PMColor4fTRANSPARENT;
368
Greg Danielf41b2bd2019-08-22 16:19:24 -0400369 // TODO: in a post-MDB world this should be handled at the OpsTask level.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500370 // 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()) {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400375 GrPaint paint;
Greg Daniel674ee742019-08-27 13:12:33 -0400376 clear_to_grpaint(kColor, &paint);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400377
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500378 // 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.
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400381 fRenderTargetContext->addDrawOp(
382 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400383 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
384 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500385 } 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.
Chris Dalton08755122019-08-05 16:13:47 -0600389 fRenderTargetContext->addOp(GrClearOp::Make(
Greg Daniel674ee742019-08-27 13:12:33 -0400390 fRenderTargetContext->fContext, rtRect, kColor, /* fullscreen */ false));
csmartdalton29df7602016-08-31 11:55:52 -0700391 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500392 } else {
Greg Daniel674ee742019-08-27 13:12:33 -0400393 if (fRenderTargetContext->getOpsTask()->resetForFullscreenClear(
394 fRenderTargetContext->canDiscardPreviousOpsOnFullClear()) &&
395 !fRenderTargetContext->caps()->performColorClearsAsDraws()) {
396 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kClear, kColor);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500397 } else {
Greg Daniel674ee742019-08-27 13:12:33 -0400398 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 }
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400416 }
robertphillips9199a9f2016-07-13 07:48:43 -0700417 }
robertphillipsea461502015-05-26 11:38:03 -0700418}
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();)
680 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
681
682 AutoCheckFlush acf(this->drawingManager());
683
684 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
685 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
686 GrQuad croppedDeviceQuad = deviceQuad;
687 GrQuad croppedLocalQuad = localQuad;
688 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
689 &croppedDeviceQuad, &croppedLocalQuad);
690
691 SkASSERT(opt != QuadOptimization::kSubmitted);
692 if (opt != QuadOptimization::kDiscarded) {
693 // And the texture op if not discarded
694 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
695 : clip;
696 GrAAType aaType = this->chooseAAType(aa);
697 // Use the provided domain, although hypothetically we could detect that the cropped local
698 // quad is sufficiently inside the domain and the constraint could be dropped.
699 this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy),
700 std::move(textureXform), filter, color,
701 blendMode, aaType, edgeFlags,
702 croppedDeviceQuad, croppedLocalQuad, domain));
703 }
704}
705
Brian Osman11052242016-10-27 14:47:55 -0400706void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500707 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500708 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400709 const SkMatrix& viewMatrix,
710 const SkRect& rect,
711 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700712 if (!style) {
713 style = &GrStyle::SimpleFill();
714 }
joshualitt1de610a2016-01-06 08:26:09 -0800715 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700716 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700717 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400718 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700719
bsalomon6663acf2016-05-10 09:14:17 -0700720 // Path effects should've been devolved to a path in SkGpuDevice
721 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700722
Robert Phillips72152832017-01-25 17:31:35 -0500723 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700724
bsalomon6663acf2016-05-10 09:14:17 -0700725 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400726 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000727 // Fills the rect, using rect as its own local coordinates
728 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500729 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700730 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
731 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
732 if ((!rect.width() || !rect.height()) &&
733 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
734 SkScalar r = stroke.getWidth() / 2;
735 // TODO: Move these stroke->fill fallbacks to GrShape?
736 switch (stroke.getJoin()) {
737 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500738 this->drawRect(
739 clip, std::move(paint), aa, viewMatrix,
740 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
741 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700742 return;
743 case SkPaint::kRound_Join:
744 // Raster draws nothing when both dimensions are empty.
745 if (rect.width() || rect.height()){
746 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500747 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
748 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700749 return;
750 }
751 case SkPaint::kBevel_Join:
752 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500753 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700754 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
755 &GrStyle::SimpleFill());
756 } else {
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, rect.fTop - r, rect.fRight, rect.fBottom + r},
759 &GrStyle::SimpleFill());
760 }
761 return;
762 }
763 }
robertphillips44302392016-07-08 14:43:03 -0700764
Brian Salomonbaaf4392017-06-15 09:59:23 -0400765 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700766
Chris Dalton7d6748e2019-03-13 00:34:52 -0600767 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500768 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
769 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
770 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500771 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400772 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700773 return;
robertphillips4bc31812016-03-01 12:22:49 -0800774 }
robertphillips4bc31812016-03-01 12:22:49 -0800775 }
Mike Klein16885072018-12-11 09:54:31 -0500776 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500777 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700778}
779
Michael Ludwig69858532018-11-28 15:34:34 -0500780void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
781 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
782 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600783 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500784 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
785 quads, cnt));
786}
787
Robert Phillipsec2249f2016-11-09 08:54:35 -0500788int GrRenderTargetContextPriv::maxWindowRectangles() const {
789 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400790 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500791}
792
Greg Danielf41b2bd2019-08-22 16:19:24 -0400793GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600794 ) const {
795#if GR_TEST_UTILS
796 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400797 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600798 }
799#endif
800 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
801 // would normally be overwritten. The one exception is if the render target context is marked as
802 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
803 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
804 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
805 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400806 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600807}
808
Chris Daltoneffee202019-07-01 22:28:03 -0600809void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
810 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600811 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600812 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600813
Chris Daltoneffee202019-07-01 22:28:03 -0600814 int numRequiredSamples = this->numSamples();
815 if (multisampled && 1 == numRequiredSamples) {
816 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
817 // mixed samples.
818 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
819 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400820 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600821 }
822 SkASSERT(numRequiredSamples > 0);
823
824 if (numRequiredSamples > fNumStencilSamples) {
825 fNumStencilSamples = numRequiredSamples;
826 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
827 }
Chris Dalton6b982802019-06-27 13:53:46 -0600828
829 if (needsStencilClear) {
830 if (this->caps()->performStencilClearsAsDraws()) {
831 // There is a driver bug with clearing stencil. We must use an op to manually clear the
832 // stencil buffer before the op that required 'setNeedsStencil'.
833 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
834 } else {
835 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
836 // code note when the instantiated stencil buffer is already clear and skip the clear
837 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
838 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400839 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600840 }
841 }
842}
843
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000844void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700845 ASSERT_SINGLE_OWNER_PRIV
846 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400847 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400848 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
849 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700850
Robert Phillips72152832017-01-25 17:31:35 -0500851 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400852
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500853 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
854}
855
856void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
857 if (this->caps()->performStencilClearsAsDraws()) {
858 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
859 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
860
861 // Configure the paint to have no impact on the color buffer
862 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400863 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400864 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
865 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500866 } else {
867 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
868 fRenderTargetProxy.get()));
869 if (!op) {
870 return;
871 }
Chris Dalton08755122019-08-05 16:13:47 -0600872 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500873 }
robertphillips976f5f02016-06-03 10:59:20 -0700874}
875
Chris Daltonbbfd5162017-11-07 13:35:22 -0700876void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600877 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400878 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400879 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500880 ASSERT_SINGLE_OWNER_PRIV
881 RETURN_IF_ABANDONED_PRIV
882 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400883 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
884 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500885
Brian Salomon467921e2017-03-06 16:17:12 -0500886 // TODO: extract portions of checkDraw that are relevant to path stenciling.
887 SkASSERT(path);
888 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
889
890 // FIXME: Use path bounds instead of this WAR once
891 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
892 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
893
894 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700895 GrAppliedHardClip appliedClip;
896 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
897 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500898 return;
899 }
900
Robert Phillips7c525e62018-06-12 10:11:12 -0400901 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
902 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600903 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500904 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500905 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400906 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400907 if (!op) {
908 return;
909 }
Brian Salomon97180af2017-03-14 13:42:58 -0400910 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600911
Chris Daltoneffee202019-07-01 22:28:03 -0600912 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600913 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700914}
915
Brian Salomond7065e72018-10-12 11:42:02 -0400916void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500917 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400918 GrAA aa, SkCanvas::SrcRectConstraint constraint,
919 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500920 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400921 ASSERT_SINGLE_OWNER
922 RETURN_IF_ABANDONED
923 SkDEBUGCODE(this->validate();)
924 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500925
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500926 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500927 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400928 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
929 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500930 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500931 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500932 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500933 ctm = viewMatrix;
934 if (set[i].fPreViewMatrix) {
935 ctm.preConcat(*set[i].fPreViewMatrix);
936 }
937
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000938 GrQuad quad, srcQuad;
939 if (set[i].fDstClipQuad) {
940 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
941
942 SkPoint srcPts[4];
943 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
944 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500945 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000946 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
947 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500948 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000949
950 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
951 ? &set[i].fSrcRect : nullptr;
952 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
953 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
954 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500955 }
956 } else {
957 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500958 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600959 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400960 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500961 std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500962 this->addDrawOp(clip, std::move(op));
963 }
Brian Salomond7065e72018-10-12 11:42:02 -0400964}
965
Brian Osman11052242016-10-27 14:47:55 -0400966void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500967 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400968 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400969 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400970 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400971 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400972 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500973 ASSERT_SINGLE_OWNER
974 RETURN_IF_ABANDONED
975 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400976 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500977
978 AutoCheckFlush acf(this->drawingManager());
979
980 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600981 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400982 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400983 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400984 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400985 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700986}
987
988///////////////////////////////////////////////////////////////////////////////
989
Brian Osman4d92b892019-03-24 00:53:23 +0000990void GrRenderTargetContext::drawAtlas(const GrClip& clip,
991 GrPaint&& paint,
992 const SkMatrix& viewMatrix,
993 int spriteCount,
994 const SkRSXform xform[],
995 const SkRect texRect[],
996 const SkColor colors[]) {
997 ASSERT_SINGLE_OWNER
998 RETURN_IF_ABANDONED
999 SkDEBUGCODE(this->validate();)
1000 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1001
1002 AutoCheckFlush acf(this->drawingManager());
1003
1004 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1005 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1006 aaType, spriteCount, xform, texRect, colors);
1007 this->addDrawOp(clip, std::move(op));
1008}
1009
1010///////////////////////////////////////////////////////////////////////////////
1011
Brian Osman11052242016-10-27 14:47:55 -04001012void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001013 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001014 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001015 const SkMatrix& viewMatrix,
1016 const SkRRect& rrect,
1017 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001018 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001019 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001020 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001021 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001022
1023 const SkStrokeRec& stroke = style.strokeRec();
1024 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001025 return;
1026 }
1027
bsalomon7f0d9f32016-08-15 14:49:10 -07001028 GrNoClip noclip;
1029 const GrClip* clip = &origClip;
1030#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1031 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001032 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001033 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1034 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001035 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1036 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001037 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001038 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1039 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001040 clip = &noclip;
1041 }
1042#endif
bsalomon6663acf2016-05-10 09:14:17 -07001043 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001044
Robert Phillips72152832017-01-25 17:31:35 -05001045 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001046
Chris Dalton7d6748e2019-03-13 00:34:52 -06001047 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001048
Chris Dalton0dffbab2019-03-27 13:08:50 -06001049 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001050 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1051 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1052 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1053 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1054 // to avoid perf regressions on some platforms.
1055 assert_alive(paint);
1056 op = GrOvalOpFactory::MakeCircularRRectOp(
1057 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1058 }
1059 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001060 assert_alive(paint);
1061 op = GrFillRRectOp::Make(
1062 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1063 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001064 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001065 assert_alive(paint);
1066 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001067 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001068 }
1069 if (op) {
1070 this->addDrawOp(*clip, std::move(op));
1071 return;
robertphillipsea461502015-05-26 11:38:03 -07001072 }
robertphillipsb56f9272016-02-25 11:03:52 -08001073
Mike Klein16885072018-12-11 09:54:31 -05001074 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001075 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1076 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001077}
1078
Jim Van Verthc5903412016-11-17 15:27:09 -05001079///////////////////////////////////////////////////////////////////////////////
1080
Jim Van Verth3af1af92017-05-18 15:06:54 -04001081static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1082 SkPoint3 result;
1083 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1084 result.fZ = pt.fZ;
1085 return result;
1086}
1087
1088bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001089 const SkMatrix& viewMatrix,
1090 const SkPath& path,
1091 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001092 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001093 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001094 return true;
1095 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001096 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001097 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001098
1099 // check z plane
1100 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1101 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1102 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1103 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1104 return false;
1105 }
1106
1107 SkRRect rrect;
1108 SkRect rect;
1109 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001110 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001111 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1112 if (!isRRect &&
1113 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1114 rect.width() > SK_ScalarNearlyZero) {
1115 rrect.setOval(rect);
1116 isRRect = true;
1117 }
1118 if (!isRRect && path.isRect(&rect)) {
1119 rrect.setRect(rect);
1120 isRRect = true;
1121 }
1122
1123 if (!isRRect) {
1124 return false;
1125 }
1126
Jim Van Verthc5903412016-11-17 15:27:09 -05001127 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001128 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001129 }
1130
Robert Phillips72152832017-01-25 17:31:35 -05001131 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001132
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133 // transform light
1134 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1135
1136 // 1/scale
1137 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001138 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001139 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1140 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1141
1142 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001143 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1144
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001145 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001146 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1147 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1148 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001149
1150 // Outset the shadow rrect to the border of the penumbra
1151 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1152 SkRRect ambientRRect;
1153 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1154 // If the rrect was an oval then its outset will also be one.
1155 // We set it explicitly to avoid errors.
1156 if (rrect.isOval()) {
1157 ambientRRect = SkRRect::MakeOval(outsetRect);
1158 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001159 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001160 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1161 }
1162
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001163 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001164 if (transparent) {
1165 // set a large inset to force a fill
1166 devSpaceInsetWidth = ambientRRect.width();
1167 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001168
Robert Phillips7c525e62018-06-12 10:11:12 -04001169 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1170 ambientColor,
1171 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001172 ambientRRect,
1173 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001174 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001175 if (op) {
1176 this->addDrawOp(clip, std::move(op));
1177 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001178 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001179
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001180 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001181 SkScalar devSpaceSpotBlur;
1182 SkScalar spotScale;
1183 SkVector spotOffset;
1184 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1185 devLightPos.fZ, rec.fLightRadius,
1186 &devSpaceSpotBlur, &spotScale, &spotOffset);
1187 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001188 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1189
Jim Van Verth3af1af92017-05-18 15:06:54 -04001190 // Adjust translate for the effect of the scale.
1191 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1192 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1193 // This offset is in dev space, need to transform it into source space.
1194 SkMatrix ctmInverse;
1195 if (viewMatrix.invert(&ctmInverse)) {
1196 ctmInverse.mapPoints(&spotOffset, 1);
1197 } else {
1198 // Since the matrix is a similarity, this should never happen, but just in case...
1199 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1200 SkASSERT(false);
1201 }
1202
1203 // Compute the transformed shadow rrect
1204 SkRRect spotShadowRRect;
1205 SkMatrix shadowTransform;
1206 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1207 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001208 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001209
1210 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001211 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001212 SkScalar insetWidth = blurOutset;
1213 if (transparent) {
1214 // If transparent, just do a fill
1215 insetWidth += spotShadowRRect.width();
1216 } else {
1217 // For shadows, instead of using a stroke we specify an inset from the penumbra
1218 // border. We want to extend this inset area so that it meets up with the caster
1219 // geometry. The inset geometry will by default already be inset by the blur width.
1220 //
1221 // We compare the min and max corners inset by the radius between the original
1222 // rrect and the shadow rrect. The distance between the two plus the difference
1223 // between the scaled radius and the original radius gives the distance from the
1224 // transformed shadow shape to the original shape in that corner. The max
1225 // of these gives the maximum distance we need to cover.
1226 //
1227 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1228 // that to get the full insetWidth.
1229 SkScalar maxOffset;
1230 if (rrect.isRect()) {
1231 // Manhattan distance works better for rects
1232 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1233 rrect.rect().fLeft),
1234 SkTAbs(spotShadowRRect.rect().fTop -
1235 rrect.rect().fTop)),
1236 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1237 rrect.rect().fRight),
1238 SkTAbs(spotShadowRRect.rect().fBottom -
1239 rrect.rect().fBottom)));
1240 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001241 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001242 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1243 rrect.rect().fLeft + dr,
1244 spotShadowRRect.rect().fTop -
1245 rrect.rect().fTop + dr);
1246 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1247 rrect.rect().fRight - dr,
1248 spotShadowRRect.rect().fBottom -
1249 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001250 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1251 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001252 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001253 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001254 }
1255
1256 // Outset the shadow rrect to the border of the penumbra
1257 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1258 if (spotShadowRRect.isOval()) {
1259 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1260 } else {
1261 SkScalar outsetRad = spotRadius + blurOutset;
1262 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1263 }
1264
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001265 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001266
Robert Phillips7c525e62018-06-12 10:11:12 -04001267 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1268 spotColor,
1269 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001270 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001271 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001272 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001273 if (op) {
1274 this->addDrawOp(clip, std::move(op));
1275 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001276 }
1277
1278 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001279}
1280
1281///////////////////////////////////////////////////////////////////////////////
1282
Brian Osman11052242016-10-27 14:47:55 -04001283bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001284 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001285 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001286 const SkMatrix& viewMatrix,
1287 const SkRRect& origOuter,
1288 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001289 SkASSERT(!origInner.isEmpty());
1290 SkASSERT(!origOuter.isEmpty());
1291
Brian Salomon65749212017-12-01 16:01:47 -05001292 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1293
Chris Dalton7d6748e2019-03-13 00:34:52 -06001294 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001295
1296 if (GrAAType::kMSAA == aaType) {
1297 return false;
1298 }
1299
Greg Daniel2655ede2019-04-10 00:49:28 +00001300 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1301 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001302 auto outerR = outer->width() / 2.f;
1303 auto innerR = inner->width() / 2.f;
1304 auto cx = outer->getBounds().fLeft + outerR;
1305 auto cy = outer->getBounds().fTop + outerR;
1306 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1307 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1308 auto avgR = (innerR + outerR) / 2.f;
1309 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1310 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1311 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001312 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001313 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001314 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001315 if (op) {
1316 this->addDrawOp(clip, std::move(op));
1317 return true;
1318 }
Mike Klein16885072018-12-11 09:54:31 -05001319 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001320 }
1321 }
1322
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001323 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001324 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001325 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1326 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001327 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001328 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1329 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001330 }
robertphillips00095892016-02-29 13:50:40 -08001331
robertphillips00095892016-02-29 13:50:40 -08001332 SkMatrix inverseVM;
1333 if (!viewMatrix.isIdentity()) {
1334 if (!origInner.transform(viewMatrix, inner.writable())) {
1335 return false;
1336 }
1337 if (!origOuter.transform(viewMatrix, outer.writable())) {
1338 return false;
1339 }
1340 if (!viewMatrix.invert(&inverseVM)) {
1341 return false;
1342 }
1343 } else {
1344 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001345 }
robertphillips00095892016-02-29 13:50:40 -08001346
Ethan Nicholaseace9352018-10-15 20:09:54 +00001347 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001348 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001349 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001350 if (!innerEffect) {
1351 return false;
1352 }
1353
Ethan Nicholaseace9352018-10-15 20:09:54 +00001354 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001355 if (!outerEffect) {
1356 return false;
1357 }
1358
Brian Salomon82f44312017-01-11 13:42:54 -05001359 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1360 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001361
1362 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001363 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001364 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1365 }
halcanary9d524f22016-03-29 09:03:52 -07001366
Brian Salomon82f44312017-01-11 13:42:54 -05001367 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1368 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001369 return true;
1370}
1371
Brian Osman11052242016-10-27 14:47:55 -04001372void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001373 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001374 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001375 const SkMatrix& viewMatrix,
1376 const SkRRect& outer,
1377 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001378 ASSERT_SINGLE_OWNER
1379 RETURN_IF_ABANDONED
1380 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001381 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001382
1383 SkASSERT(!outer.isEmpty());
1384 SkASSERT(!inner.isEmpty());
1385
Robert Phillips72152832017-01-25 17:31:35 -05001386 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001387
Brian Salomon82f44312017-01-11 13:42:54 -05001388 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001389 return;
1390 }
Mike Klein16885072018-12-11 09:54:31 -05001391 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001392
1393 SkPath path;
1394 path.setIsVolatile(true);
1395 path.addRRect(inner);
1396 path.addRRect(outer);
1397 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001398 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001399}
1400
robertphillipsea461502015-05-26 11:38:03 -07001401///////////////////////////////////////////////////////////////////////////////
1402
Brian Osman11052242016-10-27 14:47:55 -04001403void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001404 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001405 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001406 const SkMatrix& viewMatrix,
1407 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001408 const GrStyle& style,
1409 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001410 ASSERT_SINGLE_OWNER
1411 RETURN_IF_ABANDONED
1412 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001413 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001414
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001415 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001416 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001417 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001418 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001419 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1420 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001421 aa = GrAA::kNo;
1422 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001423 }
msarettcc319b92016-08-25 18:07:18 -07001424 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001426 SkPath path;
1427 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001428 path.setIsVolatile(true);
1429
Brian Salomon82f44312017-01-11 13:42:54 -05001430 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001431 }
1432
Chris Dalton7d6748e2019-03-13 00:34:52 -06001433 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001434 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1435 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001436 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001437}
1438
Brian Osman11052242016-10-27 14:47:55 -04001439void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001440 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001441 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001442 const SkMatrix& viewMatrix,
1443 const SkRect& oval,
1444 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001445 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001446 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001447 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001448 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001449
Robert Phillips7484d202018-07-03 09:09:08 -04001450 const SkStrokeRec& stroke = style.strokeRec();
1451
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001452 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001453 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1454 return;
1455 }
1456
1457 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001458 return;
robertphillipsea461502015-05-26 11:38:03 -07001459 }
1460
Robert Phillips72152832017-01-25 17:31:35 -05001461 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001462
Chris Dalton7d6748e2019-03-13 00:34:52 -06001463 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001464
1465 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001466 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1467 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001468 // We don't draw true circles as round rects in coverage mode, because it can
1469 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1470 assert_alive(paint);
1471 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1472 this->caps()->shaderCaps());
1473 }
1474 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001475 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1476 // the arc equation. This same special geometry and fragment branch also turn out to be a
1477 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1478 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1479 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001480 assert_alive(paint);
1481 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1482 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001483 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001484 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001485 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001486 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1487 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001488 }
1489 if (op) {
1490 this->addDrawOp(clip, std::move(op));
1491 return;
robertphillipsea461502015-05-26 11:38:03 -07001492 }
robertphillipsb56f9272016-02-25 11:03:52 -08001493
Mike Klein16885072018-12-11 09:54:31 -05001494 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001495 this->drawShapeUsingPathRenderer(
1496 clip, std::move(paint), aa, viewMatrix,
1497 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001498}
1499
Brian Osman11052242016-10-27 14:47:55 -04001500void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001501 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001502 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001503 const SkMatrix& viewMatrix,
1504 const SkRect& oval,
1505 SkScalar startAngle,
1506 SkScalar sweepAngle,
1507 bool useCenter,
1508 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001509 ASSERT_SINGLE_OWNER
1510 RETURN_IF_ABANDONED
1511 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001512 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001513
1514 AutoCheckFlush acf(this->drawingManager());
1515
Chris Dalton7d6748e2019-03-13 00:34:52 -06001516 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001517 if (GrAAType::kCoverage == aaType) {
1518 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1519 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1520 std::move(paint),
1521 viewMatrix,
1522 oval,
1523 startAngle,
1524 sweepAngle,
1525 useCenter,
1526 style,
1527 shaderCaps);
1528 if (op) {
1529 this->addDrawOp(clip, std::move(op));
1530 return;
1531 }
1532 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001533 }
Brian Salomone4949402018-04-26 15:22:04 -04001534 this->drawShapeUsingPathRenderer(
1535 clip, std::move(paint), aa, viewMatrix,
1536 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001537}
1538
Brian Osman11052242016-10-27 14:47:55 -04001539void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001540 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001541 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001542 sk_sp<GrTextureProxy> image,
1543 sk_sp<GrColorSpaceXform> csxf,
1544 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001545 std::unique_ptr<SkLatticeIter> iter,
1546 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001547 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001548 RETURN_IF_ABANDONED
1549 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001550 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001551
Robert Phillips72152832017-01-25 17:31:35 -05001552 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001553
Brian Salomon2a943df2018-05-04 13:43:19 -04001554 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001555 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1556 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001557 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001558}
1559
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001560void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1561 const SkRect& bounds) {
1562 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1563 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001564 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001565}
1566
Brian Salomon031b0ba2019-05-23 11:05:26 -04001567void GrRenderTargetContext::asyncRescaleAndReadPixels(
1568 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1569 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1570 auto direct = fContext->priv().asDirectContext();
1571 if (!direct) {
1572 callback(context, nullptr, 0);
1573 return;
1574 }
1575 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1576 callback(context, nullptr, 0);
1577 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001578 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001579 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1580 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001581 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001582 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001583 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001584 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001585 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1586 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001587 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001588 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001589 backendFormatOfFinalContext, dstCT);
1590 // Fail if we can't read from the source surface's color type.
1591 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001592 callback(context, nullptr, 0);
1593 return;
1594 }
Brian Salomon624f9062019-07-02 14:23:00 -04001595 // Fail if read color type does not have all of dstCT's color channels and those missing color
1596 // channels are in the src.
1597 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1598 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1599 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1600 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001601 callback(context, nullptr, 0);
1602 return;
1603 }
1604
Brian Salomonbf6b9792019-08-21 09:38:10 -04001605 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001606 int x = srcRect.fLeft;
1607 int y = srcRect.fTop;
1608 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001609 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1610 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001611 callback(context, nullptr, 0);
1612 return;
1613 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001614 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1615 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001616 x = y = 0;
1617 } else {
1618 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001619 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1620 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001621 info.colorSpace(), info.alphaType());
1622 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1623 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001624 // We flip or color convert by drawing and we don't currently support drawing to
1625 // kPremul.
1626 if (info.alphaType() == kUnpremul_SkAlphaType) {
1627 callback(context, nullptr, 0);
1628 return;
1629 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001630 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001631 SkRect srcRectToDraw = SkRect::Make(srcRect);
1632 // If the src is not texturable first try to make a copy to a texture.
1633 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001634 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1635 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1636 SkBudgeted::kNo);
1637 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001638 callback(context, nullptr, 0);
1639 return;
1640 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001641 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1642 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001643 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001644 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1645 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1646 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001647 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001648 callback(context, nullptr, 0);
1649 return;
1650 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001651 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1652 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1653 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1654 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1655 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001656 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001657 }
1658 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001659 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001660 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1661 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001662}
1663
Brian Salomon024bd002019-06-11 11:38:16 -04001664void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1665 ReadPixelsCallback callback,
1666 ReadPixelsContext context) {
1667 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1668 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1669
1670 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1671
1672 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001673 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001674 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1675 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001676 this->colorSpaceInfo().refColorSpace());
1677 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001678 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001679 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001680 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001681 callback(context, pm.addr(), pm.rowBytes());
1682 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001683 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001684
Brian Salomonab32f652019-05-10 14:24:50 -04001685 struct FinishContext {
1686 ReadPixelsCallback* fClientCallback;
1687 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001688 int fW, fH;
1689 SkColorType fColorType;
1690 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001691 };
1692 // Assumption is that the caller would like to flush. We could take a parameter or require an
1693 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1694 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001695 auto* finishContext = new FinishContext{callback, context, rect.width(),
1696 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001697 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001698 const auto* context = reinterpret_cast<const FinishContext*>(c);
1699 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001700 if (!data) {
1701 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1702 delete context;
1703 return;
1704 }
Brian Salomon4d036892019-07-02 15:10:58 -04001705 std::unique_ptr<char[]> tmp;
1706 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001707 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001708 tmp.reset(new char[rowBytes * context->fH]);
1709 context->fTransferResult.fPixelConverter(tmp.get(), data);
1710 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001711 }
Brian Salomon4d036892019-07-02 15:10:58 -04001712 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001713 delete context;
1714 };
1715 GrFlushInfo flushInfo;
1716 flushInfo.fFinishedContext = finishContext;
1717 flushInfo.fFinishedProc = finishCallback;
1718 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1719}
1720
1721void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1722 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1723 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1724 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1725 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1726 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1727 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1728 auto direct = fContext->priv().asDirectContext();
1729 if (!direct) {
1730 callback(context, nullptr, nullptr);
1731 return;
1732 }
1733 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1734 callback(context, nullptr, nullptr);
1735 return;
1736 }
1737 if (dstW & 0x1) {
1738 return;
1739 }
1740 int x = srcRect.fLeft;
1741 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001742 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001743 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1744 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001745 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001746 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1747 dstColorSpace);
1748 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001749 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1750 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001751 callback(context, nullptr, nullptr);
1752 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001753 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001754 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1755 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001756 x = y = 0;
1757 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001758 // We assume the caller wants kPremul. There is no way to indicate a preference.
1759 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1760 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1761 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001762 if (xform) {
1763 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1764 // TODO: Do something if the input is not a texture already.
1765 if (!texProxy) {
1766 callback(context, nullptr, nullptr);
1767 return;
1768 }
Brian Salomon024bd002019-06-11 11:38:16 -04001769 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001770 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001771 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1772 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001773 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001774 callback(context, nullptr, nullptr);
1775 return;
1776 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001777 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1778 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1779 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1780 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1781 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001782 x = y = 0;
1783 }
1784 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001785 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001786 // TODO: Do something if the input is not a texture already.
1787 if (!srcProxy) {
1788 callback(context, nullptr, nullptr);
1789 return;
1790 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001791 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1792 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1793 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1794 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1795 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1796 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1797 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1798 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1799 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001800 if (!yRTC || !uRTC || !vRTC) {
1801 callback(context, nullptr, nullptr);
1802 return;
1803 }
1804
1805 static constexpr float kRec601M[] {
1806 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1807 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1808 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1809 };
1810 static constexpr float kRec709M[] {
1811 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1812 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1813 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1814 };
1815 static constexpr float kJpegM[] {
1816 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1817 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1818 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1819 };
1820 static constexpr float kIM[] {
1821 1.f, 0.f, 0.f, 0.f,
1822 0.f, 1.f, 0.f, 0.f,
1823 0.f, 0.f, 1.f, 0.f,
1824 };
1825 const float* baseM = kIM;
1826 switch (yuvColorSpace) {
1827 case kRec601_SkYUVColorSpace:
1828 baseM = kRec601M;
1829 break;
1830 case kRec709_SkYUVColorSpace:
1831 baseM = kRec709M;
1832 break;
1833 case kJPEG_SkYUVColorSpace:
1834 baseM = kJpegM;
1835 break;
1836 case kIdentity_SkYUVColorSpace:
1837 baseM = kIM;
1838 break;
1839 }
1840 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1841
1842 auto texMatrix = SkMatrix::MakeTrans(x, y);
1843
Michael Ludwig61328202019-06-19 14:48:58 +00001844 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1845 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1846
Brian Salomon024bd002019-06-11 11:38:16 -04001847 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1848 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001849 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001850 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1851 GrPaint yPaint;
1852 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1853 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1854 yPaint.addColorFragmentProcessor(std::move(yFP));
1855 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001856 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1857 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001858 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1859 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1860 if (!yTransfer.fTransferBuffer) {
1861 callback(context, nullptr, nullptr);
1862 return;
1863 }
1864
1865 texMatrix.preScale(2.f, 2.f);
1866 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1867 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001868 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001869 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1870 GrPaint uPaint;
1871 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1872 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1873 uPaint.addColorFragmentProcessor(std::move(uFP));
1874 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001875 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1876 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001877 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1878 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1879 if (!uTransfer.fTransferBuffer) {
1880 callback(context, nullptr, nullptr);
1881 return;
1882 }
1883
1884 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1885 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001886 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001887 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1888 GrPaint vPaint;
1889 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1890 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1891 vPaint.addColorFragmentProcessor(std::move(vFP));
1892 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001893 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1894 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001895 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1896 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1897 if (!vTransfer.fTransferBuffer) {
1898 callback(context, nullptr, nullptr);
1899 return;
1900 }
1901
1902 struct FinishContext {
1903 ReadPixelsCallbackYUV420* fClientCallback;
1904 ReadPixelsContext fClientContext;
1905 int fW, fH;
1906 PixelTransferResult fYTransfer;
1907 PixelTransferResult fUTransfer;
1908 PixelTransferResult fVTransfer;
1909 };
1910 // Assumption is that the caller would like to flush. We could take a parameter or require an
1911 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1912 // callback to GrGpu until after the next flush that flushes our op list, though.
1913 auto* finishContext = new FinishContext{callback,
1914 context,
1915 dstW,
1916 dstH,
1917 std::move(yTransfer),
1918 std::move(uTransfer),
1919 std::move(vTransfer)};
1920 auto finishCallback = [](GrGpuFinishedContext c) {
1921 const auto* context = reinterpret_cast<const FinishContext*>(c);
1922 const void* y = context->fYTransfer.fTransferBuffer->map();
1923 const void* u = context->fUTransfer.fTransferBuffer->map();
1924 const void* v = context->fVTransfer.fTransferBuffer->map();
1925 if (!y || !u || !v) {
1926 if (y) {
1927 context->fYTransfer.fTransferBuffer->unmap();
1928 }
1929 if (u) {
1930 context->fUTransfer.fTransferBuffer->unmap();
1931 }
1932 if (v) {
1933 context->fVTransfer.fTransferBuffer->unmap();
1934 }
1935 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1936 delete context;
1937 return;
1938 }
1939 size_t w = SkToSizeT(context->fW);
1940 size_t h = SkToSizeT(context->fH);
1941 std::unique_ptr<uint8_t[]> yTemp;
1942 if (context->fYTransfer.fPixelConverter) {
1943 yTemp.reset(new uint8_t[w * h]);
1944 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1945 y = yTemp.get();
1946 }
1947 std::unique_ptr<uint8_t[]> uTemp;
1948 if (context->fUTransfer.fPixelConverter) {
1949 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1950 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1951 u = uTemp.get();
1952 }
1953 std::unique_ptr<uint8_t[]> vTemp;
1954 if (context->fVTransfer.fPixelConverter) {
1955 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1956 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1957 v = vTemp.get();
1958 }
1959 const void* data[] = {y, u, v};
1960 size_t rowBytes[] = {w, w / 2, w / 2};
1961 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1962 context->fYTransfer.fTransferBuffer->unmap();
1963 context->fUTransfer.fTransferBuffer->unmap();
1964 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001965 delete context;
1966 };
1967 GrFlushInfo flushInfo;
1968 flushInfo.fFinishedContext = finishContext;
1969 flushInfo.fFinishedProc = finishCallback;
1970 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001971}
1972
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001973GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1974 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001975 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001976 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001977 return GrSemaphoresSubmitted::kNo;
1978 }
robertphillips8c523e02016-07-26 07:41:00 -07001979 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001980 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001981
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001982 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001983}
1984
Greg Danielc64ee462017-06-15 16:59:49 -04001985bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001986 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001987 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001988 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001989 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001990 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001991
1992 AutoCheckFlush acf(this->drawingManager());
1993
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001994 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001995 return false;
1996 }
1997
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001998 auto direct = fContext->priv().asDirectContext();
1999 if (!direct) {
2000 return false;
2001 }
2002
2003 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002004
Greg Daniela5cb7812017-06-16 09:45:32 -04002005 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05002006 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002007 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2008 kAdopt_GrWrapOwnership);
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002009 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, std::move(sema),
Robert Phillips7c525e62018-06-12 10:11:12 -04002010 fRenderTargetProxy.get()));
Greg Danielf41b2bd2019-08-22 16:19:24 -04002011 this->getOpsTask()->addWaitOp(
Chris Dalton08755122019-08-05 16:13:47 -06002012 std::move(waitOp), GrTextureResolveManager(this->drawingManager()), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04002013 }
Greg Danielc64ee462017-06-15 16:59:49 -04002014 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002015}
joshualitt33a5fce2015-11-18 13:28:51 -08002016
Robert Phillips65a88fa2017-08-08 08:36:22 -04002017void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04002018 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06002019 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04002020}
2021
Brian Osman11052242016-10-27 14:47:55 -04002022void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002023 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002024 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002025 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002026 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002027 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002028 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002029 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002030 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002031 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2032
Brian Salomon40b77a62017-12-22 11:25:52 -05002033 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002034
Robert Phillips27927a52018-08-20 13:18:12 -04002035 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002036}
2037
2038void GrRenderTargetContext::drawShape(const GrClip& clip,
2039 GrPaint&& paint,
2040 GrAA aa,
2041 const SkMatrix& viewMatrix,
2042 const GrShape& shape) {
2043 ASSERT_SINGLE_OWNER
2044 RETURN_IF_ABANDONED
2045 SkDEBUGCODE(this->validate();)
2046 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2047
Brian Salomon2fad74a2017-12-20 13:28:55 -05002048 if (shape.isEmpty()) {
2049 if (shape.inverseFilled()) {
2050 this->drawPaint(clip, std::move(paint), viewMatrix);
2051 }
2052 return;
robertphillipsea461502015-05-26 11:38:03 -07002053 }
2054
Robert Phillips72152832017-01-25 17:31:35 -05002055 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002056
Brian Salomon2fad74a2017-12-20 13:28:55 -05002057 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002058 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002059 SkRRect rrect;
2060 // We can ignore the starting point and direction since there is no path effect.
2061 bool inverted;
2062 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2063 if (rrect.isRect()) {
2064 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2065 &shape.style());
2066 return;
2067 } else if (rrect.isOval()) {
2068 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002069 return;
2070 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002071 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2072 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002073 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2074 viewMatrix.rectStaysRect()) {
2075 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2076 // the matrix to all the points individually rather than just to the rect
2077 SkRect rects[2];
2078 if (shape.asNestedRects(rects)) {
2079 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002080 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002081 fContext, std::move(paint), viewMatrix, rects);
2082 if (op) {
2083 this->addDrawOp(clip, std::move(op));
2084 }
2085 // Returning here indicates that there is nothing to draw in this case.
2086 return;
2087 }
robertphillipsea461502015-05-26 11:38:03 -07002088 }
2089 }
robertphillips4bc31812016-03-01 12:22:49 -08002090
Brian Salomon2fad74a2017-12-20 13:28:55 -05002091 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002092}
2093
Chris Daltonbbfd5162017-11-07 13:35:22 -07002094bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002095 const GrUserStencilSettings* ss,
2096 SkRegion::Op op,
2097 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002098 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002099 const SkMatrix& viewMatrix,
2100 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002101 ASSERT_SINGLE_OWNER_PRIV
2102 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002103 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002104 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2105 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002106
2107 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002108 GrPaint paint;
2109 paint.setCoverageSetOpXPFactory(op, invert);
2110 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2111 SkRect::MakeIWH(fRenderTargetContext->width(),
2112 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002113 return true;
2114 }
2115
Robert Phillips72152832017-01-25 17:31:35 -05002116 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002117
2118 // An Assumption here is that path renderer would use some form of tweaking
2119 // the src color (either the input alpha or in the frag shader) to implement
2120 // aa. If we have some future driver-mojo path AA that can do the right
2121 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002122 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002123 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002124
Chris Daltondb91c6e2017-09-08 16:25:08 -06002125 SkIRect clipConservativeBounds;
2126 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2127 &clipConservativeBounds, nullptr);
2128
bsalomon8acedde2016-06-24 10:42:16 -07002129 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002130 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002131 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002132 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002133 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002134 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002135 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002136 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002137 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2138 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002139 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002140
2141 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002142 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002143 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002144 if (!pr) {
2145 return false;
2146 }
2147
2148 GrPaint paint;
2149 paint.setCoverageSetOpXPFactory(op, invert);
2150
Brian Salomonf3569f02017-10-24 12:52:33 -04002151 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2152 std::move(paint),
2153 ss,
2154 fRenderTargetContext,
2155 &clip,
2156 &clipConservativeBounds,
2157 &viewMatrix,
2158 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002159 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002160 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002161 pr->drawPath(args);
2162 return true;
2163}
2164
Brian Osman11052242016-10-27 14:47:55 -04002165SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002166 ASSERT_SINGLE_OWNER_PRIV
2167
Robert Phillips6a6de562019-02-15 15:19:15 -05002168 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002169 return SkBudgeted::kNo;
2170 }
2171
Brian Osman11052242016-10-27 14:47:55 -04002172 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002173
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002174 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002175}
2176
Brian Salomon2fad74a2017-12-20 13:28:55 -05002177void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2178 GrPaint&& paint,
2179 GrAA aa,
2180 const SkMatrix& viewMatrix,
2181 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002182 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002183 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002184 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2185
Jim Van Verthf86073a2018-10-02 13:05:38 -04002186 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2187 return;
2188 }
2189
Chris Daltondb91c6e2017-09-08 16:25:08 -06002190 SkIRect clipConservativeBounds;
2191 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2192
Brian Salomon2fad74a2017-12-20 13:28:55 -05002193 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002194 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002195
robertphillips68737822015-10-29 12:12:21 -07002196 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002197 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002198 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002199 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002200 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002201 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002202 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002203 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002204
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002205 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002206 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002207 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002208 return;
2209 }
2210
Chris Dalton6ce447a2019-06-23 18:07:38 -06002211 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002212
2213 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2214 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2215 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2216
Brian Salomon2fad74a2017-12-20 13:28:55 -05002217 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002218 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002219 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2220 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002221 return;
2222 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002223 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002224 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002225 }
2226 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002227 if (canDrawArgs.fShape->style().applies()) {
2228 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2229 styleScale);
2230 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002231 return;
2232 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002233 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002234 // This time, allow SW renderer
2235 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2236 } else {
2237 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002238 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002239 }
robertphillipsea461502015-05-26 11:38:03 -07002240
bsalomon8acedde2016-06-24 10:42:16 -07002241 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002242#ifdef SK_DEBUG
2243 SkDebugf("Unable to find path renderer compatible with path.\n");
2244#endif
2245 return;
2246 }
2247
Robert Phillips256c37b2017-03-01 14:32:46 -05002248 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002249 std::move(paint),
2250 &GrUserStencilSettings::kUnused,
2251 this,
2252 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002253 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002254 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002255 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002256 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002257 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002258 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002259}
2260
Brian Salomon467921e2017-03-06 16:17:12 -05002261static void op_bounds(SkRect* bounds, const GrOp* op) {
2262 *bounds = op->bounds();
2263 if (op->hasZeroArea()) {
2264 if (op->hasAABloat()) {
2265 bounds->outset(0.5f, 0.5f);
2266 } else {
2267 // We don't know which way the particular GPU will snap lines or points at integer
2268 // coords. So we ensure that the bounds is large enough for either snap.
2269 SkRect before = *bounds;
2270 bounds->roundOut(bounds);
2271 if (bounds->fLeft == before.fLeft) {
2272 bounds->fLeft -= 1;
2273 }
2274 if (bounds->fTop == before.fTop) {
2275 bounds->fTop -= 1;
2276 }
2277 if (bounds->fRight == before.fRight) {
2278 bounds->fRight += 1;
2279 }
2280 if (bounds->fBottom == before.fBottom) {
2281 bounds->fBottom += 1;
2282 }
2283 }
2284 }
2285}
2286
Chris Dalton08755122019-08-05 16:13:47 -06002287void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002288 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002289 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2290}
2291
Brian Salomon348a0372018-10-31 10:42:18 -04002292void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2293 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002294 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002295 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002296 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002297 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002298 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002299 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002300 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002301 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002302
Brian Salomon467921e2017-03-06 16:17:12 -05002303 // Setup clip
2304 SkRect bounds;
2305 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002306 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002307 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002308 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2309 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2310
2311 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002312 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002313 }
2314
2315 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002316 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002317 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002318 }
2319
Chris Daltoneffee202019-07-01 22:28:03 -06002320 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002321
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002322 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002323 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2324 // here as well.
2325 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2326#ifdef SK_DEBUG
2327 if (hasMixedSampledCoverage) {
2328 SkASSERT(usesStencil);
2329 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2330 }
2331#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002332 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002333 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2334
2335 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002336 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002337 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002338 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002339 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002340 }
2341 }
2342
2343 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002344 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002345 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002346 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002347 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002348 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2349 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002350}
2351
Chris Dalton2243c7b2019-08-21 14:46:35 -06002352bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2353 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002354 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2355 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2356 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002357 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002358 return false;
2359 }
2360
Chris Dalton2243c7b2019-08-21 14:46:35 -06002361 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2362 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002363 // The render target is a texture, so we can read from it directly in the shader. The XP
2364 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002365 dstProxy->setProxy(sk_ref_sp(texProxy));
2366 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002367 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002368 }
2369 }
2370
Chris Dalton2243c7b2019-08-21 14:46:35 -06002371 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002372
Eric Karl74480882017-04-03 14:49:05 -07002373 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002374 clip.getConservativeBounds(
2375 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002376 SkRect opBounds = op.bounds();
2377 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2378 // 0.5 pixels.
2379 if (op.hasAABloat() || op.hasZeroArea()) {
2380 opBounds.outset(0.5f, 0.5f);
2381 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2382 // performance we may ignore the clip when the draw is entirely inside the clip is float
2383 // space but will hit pixels just outside the clip when actually rasterizing.
2384 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002385 clippedRect.intersect(SkIRect::MakeWH(
2386 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002387 }
2388 SkIRect opIBounds;
2389 opBounds.roundOut(&opIBounds);
2390 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002391#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002392 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002393#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002394 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002395 }
2396
2397 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2398 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002399 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002400 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002401
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002402 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002403 copyRect = clippedRect;
2404 }
Brian Salomon467921e2017-03-06 16:17:12 -05002405
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002406 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002407 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002408 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002409 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002410 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002411 } else {
Eric Karl74480882017-04-03 14:49:05 -07002412 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002413 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002414 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002415 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2416 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2417 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002418 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002419
Greg Daniel46cfbc62019-06-07 11:43:30 -04002420 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002421 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002422 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002423}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002424
2425bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2426 const SkIPoint& dstPoint) {
2427 SkIRect clippedSrcRect;
2428 SkIPoint clippedDstPoint;
2429 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2430 &clippedSrcRect, &clippedDstPoint)) {
2431 return false;
2432 }
2433
2434 GrPaint paint;
2435 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2436 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2437 SkMatrix::I());
2438 if (!fp) {
2439 return false;
2440 }
2441 paint.addColorFragmentProcessor(std::move(fp));
2442
2443 this->fillRectToRect(
2444 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2445 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2446 clippedSrcRect.height()),
2447 SkRect::Make(clippedSrcRect));
2448 return true;
2449}
2450