blob: 862b79c460e917b0f517704f88d7ef1d04e2c06d [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
Brian Osman9a9baae2018-11-05 15:06:26 -0500345void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const SkPMColor4f& color) {
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 Danielf41b2bd2019-08-22 16:19:24 -0400367 // TODO: in a post-MDB world this should be handled at the OpsTask level.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500368 // This makes sure to always add an op to the list, instead of marking the clear as a load op.
369 // This code follows very similar logic to internalClear() below, but critical differences are
370 // highlighted in line related to absClear()'s unique behavior.
371 if (clearRect) {
372 if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400373 GrPaint paint;
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500374 clear_to_grpaint(color, &paint);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400375
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500376 // Use the disabled clip; the rect geometry already matches the clear rectangle and
377 // if it were added to a scissor, that would be intersected with the logical surface
378 // bounds and not the worst case dimensions required here.
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400379 fRenderTargetContext->addDrawOp(
380 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400381 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
382 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500383 } else {
384 // Must use the ClearOp factory that takes a boolean (false) instead of a surface
385 // proxy. The surface proxy variant would intersect the clip rect with its logical
386 // bounds, which is not desired in this special case.
Chris Dalton08755122019-08-05 16:13:47 -0600387 fRenderTargetContext->addOp(GrClearOp::Make(
388 fRenderTargetContext->fContext, rtRect, color, /* fullscreen */ false));
csmartdalton29df7602016-08-31 11:55:52 -0700389 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500390 } else {
391 // Reset the oplist like in internalClear(), but do not rely on a load op for the clear
Greg Danielf41b2bd2019-08-22 16:19:24 -0400392 fRenderTargetContext->getOpsTask()->resetForFullscreenClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600393 fRenderTargetContext->canDiscardPreviousOpsOnFullClear());
Greg Danielf41b2bd2019-08-22 16:19:24 -0400394 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500395
396 if (fRenderTargetContext->caps()->performColorClearsAsDraws()) {
397 // This draws a quad covering the worst case dimensions instead of just the logical
398 // width and height like in internalClear().
399 GrPaint paint;
400 clear_to_grpaint(color, &paint);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400401 fRenderTargetContext->addDrawOp(
402 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400403 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
404 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500405 } else {
406 // Nothing special about this path in absClear compared to internalClear()
Chris Dalton08755122019-08-05 16:13:47 -0600407 fRenderTargetContext->addOp(GrClearOp::Make(
408 fRenderTargetContext->fContext, SkIRect::MakeEmpty(), color,
409 /* fullscreen */ true));
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400410 }
robertphillips9199a9f2016-07-13 07:48:43 -0700411 }
robertphillipsea461502015-05-26 11:38:03 -0700412}
413
Brian Osman11052242016-10-27 14:47:55 -0400414void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500415 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400416 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000417 // Start with the render target, since that is the maximum content we could possibly fill.
418 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400419 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400420 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000421 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
422 // inverse view matrix.
423 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
424 } else {
425 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
426 SkMatrix localMatrix;
427 if (!viewMatrix.invert(&localMatrix)) {
428 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400429 }
Michael Ludwig61328202019-06-19 14:48:58 +0000430 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
431 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700432 }
robertphillipsea461502015-05-26 11:38:03 -0700433}
434
Michael Ludwig61328202019-06-19 14:48:58 +0000435enum class GrRenderTargetContext::QuadOptimization {
436 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
437 kDiscarded,
438 // The rect to draw was converted to some other op and appended to the oplist, so no additional
439 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
440 // a constColor is provided.
441 kSubmitted,
442 // The clip was folded into the device quad, with updated edge flags and local coords, and
443 // caller is responsible for adding an appropriate op.
444 kClipApplied,
445 // No change to clip, but quad updated to better fit clip/render target, and caller is
446 // responsible for adding an appropriate op.
447 kCropped
448};
Michael Ludwig61a16512019-01-15 11:15:13 -0500449
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400450static bool make_vertex_finite(float* value) {
451 if (SkScalarIsNaN(*value)) {
452 return false;
453 }
454
455 if (!SkScalarIsFinite(*value)) {
456 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
457 // left when calculating crops.
458 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
459 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
460 }
461
462 return true;
463}
464
Michael Ludwig61328202019-06-19 14:48:58 +0000465GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400466 const GrClip& clip, const SkPMColor4f* constColor,
467 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
468 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000469 // Optimization requirements:
470 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
471 // 2. kClear applies when constColor and final geom is pixel aligned rect;
472 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
473 // 3. kRRect applies when constColor and rrect clip and quad covers clip
474 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
475 // 5. kCropped applies when rect quad (currently)
476 // 6. kNone always applies
477 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000478
Michael Ludwige08b4432019-06-19 18:00:48 -0400479 SkRect rtRect;
480 if (stencilSettings) {
481 // Must use worst case bounds so that stencil buffer updates on approximately sized render
482 // targets don't get corrupted.
483 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
484 fRenderTargetProxy->worstCaseHeight());
485 } else {
486 // Use the logical size of the render target, which allows for "fullscreen" clears even if
487 // the render target has an approximate backing fit
488 rtRect = SkRect::MakeWH(this->width(), this->height());
489 }
490
Michael Ludwig61328202019-06-19 14:48:58 +0000491 SkRect drawBounds = deviceQuad->bounds();
492 if (constColor) {
493 // Don't bother updating local coordinates when the paint will ignore them anyways
494 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400495 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
496 // will be cropped to the finite 'clip' or render target and there is no local space mapping
497 if (!deviceQuad->isFinite()) {
498 for (int i = 0; i < 4; ++i) {
499 if (!make_vertex_finite(deviceQuad->xs() + i) ||
500 !make_vertex_finite(deviceQuad->ys() + i) ||
501 !make_vertex_finite(deviceQuad->ws() + i)) {
502 // Discard if we see a nan
503 return QuadOptimization::kDiscarded;
504 }
505 }
506 SkASSERT(deviceQuad->isFinite());
507 }
508 } else {
509 // CropToRect requires the quads to be finite. If they are not finite and we have local
510 // coordinates, the mapping from local space to device space is poorly defined so drop it
511 if (!deviceQuad->isFinite()) {
512 return QuadOptimization::kDiscarded;
513 }
Brian Salomon7694b902019-06-18 21:00:21 +0000514 }
515
Michael Ludwig61328202019-06-19 14:48:58 +0000516 // If the quad is entirely off screen, it doesn't matter what the clip does
517 if (!rtRect.intersects(drawBounds)) {
518 return QuadOptimization::kDiscarded;
519 }
Brian Salomon7694b902019-06-18 21:00:21 +0000520
Michael Ludwig61328202019-06-19 14:48:58 +0000521 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
522 // the render target).
523 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400524 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
525 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
526 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
527 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000528 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000529 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000530 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
531 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000532 }
Brian Salomon7694b902019-06-18 21:00:21 +0000533 }
534
Michael Ludwig61328202019-06-19 14:48:58 +0000535 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
536 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400537 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000538 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
539 // of the render target and the clip rect)
540 SkRect clipBounds = rtRect;
541 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
542 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000543 }
544
Michael Ludwig61328202019-06-19 14:48:58 +0000545 if (clipRRect.isRect()) {
546 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
547 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
548 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
549 // Clear optimization is possible
550 drawBounds = deviceQuad->bounds();
551 if (drawBounds.contains(rtRect)) {
552 // Fullscreen clear
553 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
554 return QuadOptimization::kSubmitted;
555 } else if (GrClip::IsPixelAligned(drawBounds) &&
556 drawBounds.width() > 256 && drawBounds.height() > 256) {
557 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
558 SkIRect scissorRect;
559 drawBounds.round(&scissorRect);
560 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
561 return QuadOptimization::kSubmitted;
562 }
563 }
564
565 // Update overall AA setting.
566 *edgeFlags = newFlags;
567 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
568 newFlags != GrQuadAAFlags::kNone) {
569 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
570 // properly reflect the smooth edge of the clip.
571 *aa = GrAA::kYes;
572 }
573 // We intentionally do not downgrade AA here because we don't know if we need to
574 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
575 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
576
577 // deviceQuad is exactly the intersection of original quad and clip, so it can be
578 // drawn with no clip (submitted by caller)
579 return QuadOptimization::kClipApplied;
580 } else {
581 // The quads have been updated to better fit the clip bounds, but can't get rid of
582 // the clip entirely
583 return QuadOptimization::kCropped;
584 }
585 } else if (constColor) {
586 // Rounded corners and constant filled color (limit ourselves to solid colors because
587 // there is no way to use custom local coordinates with drawRRect).
588 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
589 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
590 deviceQuad->bounds().contains(clipBounds)) {
591 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
592 // we can draw the rrect directly and ignore the edge flags
593 GrPaint paint;
594 clear_to_grpaint(*constColor, &paint);
595 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
596 clipRRect, GrStyle::SimpleFill());
597 return QuadOptimization::kSubmitted;
598 } else {
599 // The quad has been updated to better fit clip bounds, but can't remove the clip
600 return QuadOptimization::kCropped;
601 }
Brian Salomon7694b902019-06-18 21:00:21 +0000602 }
Brian Salomon7694b902019-06-18 21:00:21 +0000603 }
604
Michael Ludwig61328202019-06-19 14:48:58 +0000605 // Crop the quad to the conservative bounds of the clip.
606 SkIRect clipDevBounds;
607 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
608 SkRect clipBounds = SkRect::Make(clipDevBounds);
609
610 // One final check for discarding, since we may have gone here directly due to a complex clip
611 if (!clipBounds.intersects(drawBounds)) {
612 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000613 }
614
Michael Ludwig61328202019-06-19 14:48:58 +0000615 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
616 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
617 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
618
619 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000620}
621
Michael Ludwig61328202019-06-19 14:48:58 +0000622void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500623 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500624 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000625 GrQuadAAFlags edgeFlags,
626 const GrQuad& deviceQuad,
627 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400628 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000629 ASSERT_SINGLE_OWNER
630 RETURN_IF_ABANDONED
631 SkDEBUGCODE(this->validate();)
632 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500633
Michael Ludwig61328202019-06-19 14:48:58 +0000634 AutoCheckFlush acf(this->drawingManager());
635
636 SkPMColor4f* constColor = nullptr;
637 SkPMColor4f paintColor;
638 if (!ss && !paint.numCoverageFragmentProcessors() &&
639 paint.isConstantBlendedColor(&paintColor)) {
640 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
641 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500642 }
643
Michael Ludwig61328202019-06-19 14:48:58 +0000644 GrQuad croppedDeviceQuad = deviceQuad;
645 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400646 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
647 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000648 if (opt >= QuadOptimization::kClipApplied) {
649 // These optimizations require caller to add an op themselves
650 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
651 : clip;
652 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
653 : this->chooseAAType(aa);
654 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
655 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700656 }
Michael Ludwig61328202019-06-19 14:48:58 +0000657 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800658}
659
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000660void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
661 sk_sp<GrTextureProxy> proxy,
662 sk_sp<GrColorSpaceXform> textureXform,
663 GrSamplerState::Filter filter,
664 const SkPMColor4f& color,
665 SkBlendMode blendMode,
666 GrAA aa,
667 GrQuadAAFlags edgeFlags,
668 const GrQuad& deviceQuad,
669 const GrQuad& localQuad,
670 const SkRect* domain) {
671 ASSERT_SINGLE_OWNER
672 RETURN_IF_ABANDONED
673 SkDEBUGCODE(this->validate();)
674 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
675
676 AutoCheckFlush acf(this->drawingManager());
677
678 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
679 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
680 GrQuad croppedDeviceQuad = deviceQuad;
681 GrQuad croppedLocalQuad = localQuad;
682 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
683 &croppedDeviceQuad, &croppedLocalQuad);
684
685 SkASSERT(opt != QuadOptimization::kSubmitted);
686 if (opt != QuadOptimization::kDiscarded) {
687 // And the texture op if not discarded
688 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
689 : clip;
690 GrAAType aaType = this->chooseAAType(aa);
691 // Use the provided domain, although hypothetically we could detect that the cropped local
692 // quad is sufficiently inside the domain and the constraint could be dropped.
693 this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy),
694 std::move(textureXform), filter, color,
695 blendMode, aaType, edgeFlags,
696 croppedDeviceQuad, croppedLocalQuad, domain));
697 }
698}
699
Brian Osman11052242016-10-27 14:47:55 -0400700void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500701 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500702 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400703 const SkMatrix& viewMatrix,
704 const SkRect& rect,
705 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700706 if (!style) {
707 style = &GrStyle::SimpleFill();
708 }
joshualitt1de610a2016-01-06 08:26:09 -0800709 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700710 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700711 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400712 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700713
bsalomon6663acf2016-05-10 09:14:17 -0700714 // Path effects should've been devolved to a path in SkGpuDevice
715 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700716
Robert Phillips72152832017-01-25 17:31:35 -0500717 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700718
bsalomon6663acf2016-05-10 09:14:17 -0700719 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400720 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000721 // Fills the rect, using rect as its own local coordinates
722 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500723 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700724 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
725 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
726 if ((!rect.width() || !rect.height()) &&
727 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
728 SkScalar r = stroke.getWidth() / 2;
729 // TODO: Move these stroke->fill fallbacks to GrShape?
730 switch (stroke.getJoin()) {
731 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500732 this->drawRect(
733 clip, std::move(paint), aa, viewMatrix,
734 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
735 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700736 return;
737 case SkPaint::kRound_Join:
738 // Raster draws nothing when both dimensions are empty.
739 if (rect.width() || rect.height()){
740 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500741 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
742 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700743 return;
744 }
745 case SkPaint::kBevel_Join:
746 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500747 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700748 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
749 &GrStyle::SimpleFill());
750 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500751 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700752 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
753 &GrStyle::SimpleFill());
754 }
755 return;
756 }
757 }
robertphillips44302392016-07-08 14:43:03 -0700758
Brian Salomonbaaf4392017-06-15 09:59:23 -0400759 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700760
Chris Dalton7d6748e2019-03-13 00:34:52 -0600761 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500762 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
763 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
764 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500765 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400766 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700767 return;
robertphillips4bc31812016-03-01 12:22:49 -0800768 }
robertphillips4bc31812016-03-01 12:22:49 -0800769 }
Mike Klein16885072018-12-11 09:54:31 -0500770 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500771 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700772}
773
Michael Ludwig69858532018-11-28 15:34:34 -0500774void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
775 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
776 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600777 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500778 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
779 quads, cnt));
780}
781
Robert Phillipsec2249f2016-11-09 08:54:35 -0500782int GrRenderTargetContextPriv::maxWindowRectangles() const {
783 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400784 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500785}
786
Greg Danielf41b2bd2019-08-22 16:19:24 -0400787GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600788 ) const {
789#if GR_TEST_UTILS
790 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400791 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600792 }
793#endif
794 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
795 // would normally be overwritten. The one exception is if the render target context is marked as
796 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
797 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
798 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
799 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400800 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600801}
802
Chris Daltoneffee202019-07-01 22:28:03 -0600803void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
804 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600805 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600806 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600807
Chris Daltoneffee202019-07-01 22:28:03 -0600808 int numRequiredSamples = this->numSamples();
809 if (multisampled && 1 == numRequiredSamples) {
810 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
811 // mixed samples.
812 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
813 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400814 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600815 }
816 SkASSERT(numRequiredSamples > 0);
817
818 if (numRequiredSamples > fNumStencilSamples) {
819 fNumStencilSamples = numRequiredSamples;
820 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
821 }
Chris Dalton6b982802019-06-27 13:53:46 -0600822
823 if (needsStencilClear) {
824 if (this->caps()->performStencilClearsAsDraws()) {
825 // There is a driver bug with clearing stencil. We must use an op to manually clear the
826 // stencil buffer before the op that required 'setNeedsStencil'.
827 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
828 } else {
829 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
830 // code note when the instantiated stencil buffer is already clear and skip the clear
831 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
832 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400833 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600834 }
835 }
836}
837
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000838void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700839 ASSERT_SINGLE_OWNER_PRIV
840 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400841 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400842 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
843 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700844
Robert Phillips72152832017-01-25 17:31:35 -0500845 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400846
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500847 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
848}
849
850void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
851 if (this->caps()->performStencilClearsAsDraws()) {
852 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
853 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
854
855 // Configure the paint to have no impact on the color buffer
856 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400857 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400858 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
859 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500860 } else {
861 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
862 fRenderTargetProxy.get()));
863 if (!op) {
864 return;
865 }
Chris Dalton08755122019-08-05 16:13:47 -0600866 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500867 }
robertphillips976f5f02016-06-03 10:59:20 -0700868}
869
Chris Daltonbbfd5162017-11-07 13:35:22 -0700870void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600871 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400872 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400873 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500874 ASSERT_SINGLE_OWNER_PRIV
875 RETURN_IF_ABANDONED_PRIV
876 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400877 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
878 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500879
Brian Salomon467921e2017-03-06 16:17:12 -0500880 // TODO: extract portions of checkDraw that are relevant to path stenciling.
881 SkASSERT(path);
882 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
883
884 // FIXME: Use path bounds instead of this WAR once
885 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
886 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
887
888 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700889 GrAppliedHardClip appliedClip;
890 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
891 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500892 return;
893 }
894
Robert Phillips7c525e62018-06-12 10:11:12 -0400895 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
896 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600897 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500898 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500899 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400900 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400901 if (!op) {
902 return;
903 }
Brian Salomon97180af2017-03-14 13:42:58 -0400904 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600905
Chris Daltoneffee202019-07-01 22:28:03 -0600906 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600907 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700908}
909
Brian Salomond7065e72018-10-12 11:42:02 -0400910void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500911 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400912 GrAA aa, SkCanvas::SrcRectConstraint constraint,
913 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500914 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400915 ASSERT_SINGLE_OWNER
916 RETURN_IF_ABANDONED
917 SkDEBUGCODE(this->validate();)
918 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500919
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500920 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500921 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400922 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
923 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500924 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500925 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500926 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500927 ctm = viewMatrix;
928 if (set[i].fPreViewMatrix) {
929 ctm.preConcat(*set[i].fPreViewMatrix);
930 }
931
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000932 GrQuad quad, srcQuad;
933 if (set[i].fDstClipQuad) {
934 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
935
936 SkPoint srcPts[4];
937 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
938 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500939 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000940 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
941 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500942 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000943
944 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
945 ? &set[i].fSrcRect : nullptr;
946 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
947 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
948 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500949 }
950 } else {
951 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500952 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600953 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400954 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500955 std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500956 this->addDrawOp(clip, std::move(op));
957 }
Brian Salomond7065e72018-10-12 11:42:02 -0400958}
959
Brian Osman11052242016-10-27 14:47:55 -0400960void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500961 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400962 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400963 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400964 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400965 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400966 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500967 ASSERT_SINGLE_OWNER
968 RETURN_IF_ABANDONED
969 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400970 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500971
972 AutoCheckFlush acf(this->drawingManager());
973
974 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600975 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400976 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400977 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400978 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400979 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700980}
981
982///////////////////////////////////////////////////////////////////////////////
983
Brian Osman4d92b892019-03-24 00:53:23 +0000984void GrRenderTargetContext::drawAtlas(const GrClip& clip,
985 GrPaint&& paint,
986 const SkMatrix& viewMatrix,
987 int spriteCount,
988 const SkRSXform xform[],
989 const SkRect texRect[],
990 const SkColor colors[]) {
991 ASSERT_SINGLE_OWNER
992 RETURN_IF_ABANDONED
993 SkDEBUGCODE(this->validate();)
994 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
995
996 AutoCheckFlush acf(this->drawingManager());
997
998 GrAAType aaType = this->chooseAAType(GrAA::kNo);
999 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1000 aaType, spriteCount, xform, texRect, colors);
1001 this->addDrawOp(clip, std::move(op));
1002}
1003
1004///////////////////////////////////////////////////////////////////////////////
1005
Brian Osman11052242016-10-27 14:47:55 -04001006void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001007 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001008 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001009 const SkMatrix& viewMatrix,
1010 const SkRRect& rrect,
1011 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001012 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001013 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001014 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001015 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001016
1017 const SkStrokeRec& stroke = style.strokeRec();
1018 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001019 return;
1020 }
1021
bsalomon7f0d9f32016-08-15 14:49:10 -07001022 GrNoClip noclip;
1023 const GrClip* clip = &origClip;
1024#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1025 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001026 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001027 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1028 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001029 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1030 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001031 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001032 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1033 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001034 clip = &noclip;
1035 }
1036#endif
bsalomon6663acf2016-05-10 09:14:17 -07001037 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001038
Robert Phillips72152832017-01-25 17:31:35 -05001039 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001040
Chris Dalton7d6748e2019-03-13 00:34:52 -06001041 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001042
Chris Dalton0dffbab2019-03-27 13:08:50 -06001043 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001044 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1045 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1046 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1047 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1048 // to avoid perf regressions on some platforms.
1049 assert_alive(paint);
1050 op = GrOvalOpFactory::MakeCircularRRectOp(
1051 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1052 }
1053 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001054 assert_alive(paint);
1055 op = GrFillRRectOp::Make(
1056 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1057 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001058 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001059 assert_alive(paint);
1060 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001061 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001062 }
1063 if (op) {
1064 this->addDrawOp(*clip, std::move(op));
1065 return;
robertphillipsea461502015-05-26 11:38:03 -07001066 }
robertphillipsb56f9272016-02-25 11:03:52 -08001067
Mike Klein16885072018-12-11 09:54:31 -05001068 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001069 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1070 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001071}
1072
Jim Van Verthc5903412016-11-17 15:27:09 -05001073///////////////////////////////////////////////////////////////////////////////
1074
Jim Van Verth3af1af92017-05-18 15:06:54 -04001075static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1076 SkPoint3 result;
1077 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1078 result.fZ = pt.fZ;
1079 return result;
1080}
1081
1082bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001083 const SkMatrix& viewMatrix,
1084 const SkPath& path,
1085 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001086 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001087 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088 return true;
1089 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001090 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001091 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001092
1093 // check z plane
1094 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1095 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1096 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1097 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1098 return false;
1099 }
1100
1101 SkRRect rrect;
1102 SkRect rect;
1103 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001104 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001105 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1106 if (!isRRect &&
1107 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1108 rect.width() > SK_ScalarNearlyZero) {
1109 rrect.setOval(rect);
1110 isRRect = true;
1111 }
1112 if (!isRRect && path.isRect(&rect)) {
1113 rrect.setRect(rect);
1114 isRRect = true;
1115 }
1116
1117 if (!isRRect) {
1118 return false;
1119 }
1120
Jim Van Verthc5903412016-11-17 15:27:09 -05001121 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001122 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001123 }
1124
Robert Phillips72152832017-01-25 17:31:35 -05001125 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001126
Jim Van Verth3af1af92017-05-18 15:06:54 -04001127 // transform light
1128 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1129
1130 // 1/scale
1131 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001132 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1134 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1135
1136 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001137 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1138
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001139 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001140 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1141 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1142 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001143
1144 // Outset the shadow rrect to the border of the penumbra
1145 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1146 SkRRect ambientRRect;
1147 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1148 // If the rrect was an oval then its outset will also be one.
1149 // We set it explicitly to avoid errors.
1150 if (rrect.isOval()) {
1151 ambientRRect = SkRRect::MakeOval(outsetRect);
1152 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001153 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001154 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1155 }
1156
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001157 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001158 if (transparent) {
1159 // set a large inset to force a fill
1160 devSpaceInsetWidth = ambientRRect.width();
1161 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001162
Robert Phillips7c525e62018-06-12 10:11:12 -04001163 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1164 ambientColor,
1165 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001166 ambientRRect,
1167 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001168 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001169 if (op) {
1170 this->addDrawOp(clip, std::move(op));
1171 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001172 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001173
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001174 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001175 SkScalar devSpaceSpotBlur;
1176 SkScalar spotScale;
1177 SkVector spotOffset;
1178 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1179 devLightPos.fZ, rec.fLightRadius,
1180 &devSpaceSpotBlur, &spotScale, &spotOffset);
1181 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001182 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1183
Jim Van Verth3af1af92017-05-18 15:06:54 -04001184 // Adjust translate for the effect of the scale.
1185 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1186 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1187 // This offset is in dev space, need to transform it into source space.
1188 SkMatrix ctmInverse;
1189 if (viewMatrix.invert(&ctmInverse)) {
1190 ctmInverse.mapPoints(&spotOffset, 1);
1191 } else {
1192 // Since the matrix is a similarity, this should never happen, but just in case...
1193 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1194 SkASSERT(false);
1195 }
1196
1197 // Compute the transformed shadow rrect
1198 SkRRect spotShadowRRect;
1199 SkMatrix shadowTransform;
1200 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1201 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001202 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203
1204 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001205 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001206 SkScalar insetWidth = blurOutset;
1207 if (transparent) {
1208 // If transparent, just do a fill
1209 insetWidth += spotShadowRRect.width();
1210 } else {
1211 // For shadows, instead of using a stroke we specify an inset from the penumbra
1212 // border. We want to extend this inset area so that it meets up with the caster
1213 // geometry. The inset geometry will by default already be inset by the blur width.
1214 //
1215 // We compare the min and max corners inset by the radius between the original
1216 // rrect and the shadow rrect. The distance between the two plus the difference
1217 // between the scaled radius and the original radius gives the distance from the
1218 // transformed shadow shape to the original shape in that corner. The max
1219 // of these gives the maximum distance we need to cover.
1220 //
1221 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1222 // that to get the full insetWidth.
1223 SkScalar maxOffset;
1224 if (rrect.isRect()) {
1225 // Manhattan distance works better for rects
1226 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1227 rrect.rect().fLeft),
1228 SkTAbs(spotShadowRRect.rect().fTop -
1229 rrect.rect().fTop)),
1230 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1231 rrect.rect().fRight),
1232 SkTAbs(spotShadowRRect.rect().fBottom -
1233 rrect.rect().fBottom)));
1234 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001235 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001236 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1237 rrect.rect().fLeft + dr,
1238 spotShadowRRect.rect().fTop -
1239 rrect.rect().fTop + dr);
1240 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1241 rrect.rect().fRight - dr,
1242 spotShadowRRect.rect().fBottom -
1243 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001244 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1245 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001246 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001247 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001248 }
1249
1250 // Outset the shadow rrect to the border of the penumbra
1251 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1252 if (spotShadowRRect.isOval()) {
1253 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1254 } else {
1255 SkScalar outsetRad = spotRadius + blurOutset;
1256 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1257 }
1258
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001259 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001260
Robert Phillips7c525e62018-06-12 10:11:12 -04001261 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1262 spotColor,
1263 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001264 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001265 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001266 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001267 if (op) {
1268 this->addDrawOp(clip, std::move(op));
1269 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001270 }
1271
1272 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001273}
1274
1275///////////////////////////////////////////////////////////////////////////////
1276
Brian Osman11052242016-10-27 14:47:55 -04001277bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001278 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001279 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001280 const SkMatrix& viewMatrix,
1281 const SkRRect& origOuter,
1282 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001283 SkASSERT(!origInner.isEmpty());
1284 SkASSERT(!origOuter.isEmpty());
1285
Brian Salomon65749212017-12-01 16:01:47 -05001286 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1287
Chris Dalton7d6748e2019-03-13 00:34:52 -06001288 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001289
1290 if (GrAAType::kMSAA == aaType) {
1291 return false;
1292 }
1293
Greg Daniel2655ede2019-04-10 00:49:28 +00001294 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1295 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001296 auto outerR = outer->width() / 2.f;
1297 auto innerR = inner->width() / 2.f;
1298 auto cx = outer->getBounds().fLeft + outerR;
1299 auto cy = outer->getBounds().fTop + outerR;
1300 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1301 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1302 auto avgR = (innerR + outerR) / 2.f;
1303 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1304 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1305 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001306 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001307 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001308 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001309 if (op) {
1310 this->addDrawOp(clip, std::move(op));
1311 return true;
1312 }
Mike Klein16885072018-12-11 09:54:31 -05001313 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001314 }
1315 }
1316
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001317 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001318 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001319 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1320 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001321 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001322 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1323 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001324 }
robertphillips00095892016-02-29 13:50:40 -08001325
robertphillips00095892016-02-29 13:50:40 -08001326 SkMatrix inverseVM;
1327 if (!viewMatrix.isIdentity()) {
1328 if (!origInner.transform(viewMatrix, inner.writable())) {
1329 return false;
1330 }
1331 if (!origOuter.transform(viewMatrix, outer.writable())) {
1332 return false;
1333 }
1334 if (!viewMatrix.invert(&inverseVM)) {
1335 return false;
1336 }
1337 } else {
1338 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001339 }
robertphillips00095892016-02-29 13:50:40 -08001340
Ethan Nicholaseace9352018-10-15 20:09:54 +00001341 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001342 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001343 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001344 if (!innerEffect) {
1345 return false;
1346 }
1347
Ethan Nicholaseace9352018-10-15 20:09:54 +00001348 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001349 if (!outerEffect) {
1350 return false;
1351 }
1352
Brian Salomon82f44312017-01-11 13:42:54 -05001353 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1354 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001355
1356 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001357 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001358 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1359 }
halcanary9d524f22016-03-29 09:03:52 -07001360
Brian Salomon82f44312017-01-11 13:42:54 -05001361 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1362 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001363 return true;
1364}
1365
Brian Osman11052242016-10-27 14:47:55 -04001366void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001367 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001368 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001369 const SkMatrix& viewMatrix,
1370 const SkRRect& outer,
1371 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001372 ASSERT_SINGLE_OWNER
1373 RETURN_IF_ABANDONED
1374 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001375 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001376
1377 SkASSERT(!outer.isEmpty());
1378 SkASSERT(!inner.isEmpty());
1379
Robert Phillips72152832017-01-25 17:31:35 -05001380 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001381
Brian Salomon82f44312017-01-11 13:42:54 -05001382 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001383 return;
1384 }
Mike Klein16885072018-12-11 09:54:31 -05001385 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001386
1387 SkPath path;
1388 path.setIsVolatile(true);
1389 path.addRRect(inner);
1390 path.addRRect(outer);
1391 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001392 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001393}
1394
robertphillipsea461502015-05-26 11:38:03 -07001395///////////////////////////////////////////////////////////////////////////////
1396
Brian Osman11052242016-10-27 14:47:55 -04001397void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001398 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001399 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001400 const SkMatrix& viewMatrix,
1401 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001402 const GrStyle& style,
1403 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001404 ASSERT_SINGLE_OWNER
1405 RETURN_IF_ABANDONED
1406 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001407 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001408
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001409 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001410 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001411 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001412 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001413 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1414 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001415 aa = GrAA::kNo;
1416 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001417 }
msarettcc319b92016-08-25 18:07:18 -07001418 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001419 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001420 SkPath path;
1421 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001422 path.setIsVolatile(true);
1423
Brian Salomon82f44312017-01-11 13:42:54 -05001424 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001425 }
1426
Chris Dalton7d6748e2019-03-13 00:34:52 -06001427 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001428 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1429 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001430 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001431}
1432
Brian Osman11052242016-10-27 14:47:55 -04001433void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001434 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001435 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001436 const SkMatrix& viewMatrix,
1437 const SkRect& oval,
1438 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001439 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001440 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001441 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001442 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001443
Robert Phillips7484d202018-07-03 09:09:08 -04001444 const SkStrokeRec& stroke = style.strokeRec();
1445
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001446 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001447 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1448 return;
1449 }
1450
1451 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001452 return;
robertphillipsea461502015-05-26 11:38:03 -07001453 }
1454
Robert Phillips72152832017-01-25 17:31:35 -05001455 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001456
Chris Dalton7d6748e2019-03-13 00:34:52 -06001457 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001458
1459 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001460 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1461 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001462 // We don't draw true circles as round rects in coverage mode, because it can
1463 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1464 assert_alive(paint);
1465 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1466 this->caps()->shaderCaps());
1467 }
1468 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001469 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1470 // the arc equation. This same special geometry and fragment branch also turn out to be a
1471 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1472 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1473 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001474 assert_alive(paint);
1475 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1476 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001477 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001478 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001479 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001480 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1481 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001482 }
1483 if (op) {
1484 this->addDrawOp(clip, std::move(op));
1485 return;
robertphillipsea461502015-05-26 11:38:03 -07001486 }
robertphillipsb56f9272016-02-25 11:03:52 -08001487
Mike Klein16885072018-12-11 09:54:31 -05001488 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001489 this->drawShapeUsingPathRenderer(
1490 clip, std::move(paint), aa, viewMatrix,
1491 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001492}
1493
Brian Osman11052242016-10-27 14:47:55 -04001494void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001495 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001496 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001497 const SkMatrix& viewMatrix,
1498 const SkRect& oval,
1499 SkScalar startAngle,
1500 SkScalar sweepAngle,
1501 bool useCenter,
1502 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001503 ASSERT_SINGLE_OWNER
1504 RETURN_IF_ABANDONED
1505 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001506 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001507
1508 AutoCheckFlush acf(this->drawingManager());
1509
Chris Dalton7d6748e2019-03-13 00:34:52 -06001510 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001511 if (GrAAType::kCoverage == aaType) {
1512 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1513 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1514 std::move(paint),
1515 viewMatrix,
1516 oval,
1517 startAngle,
1518 sweepAngle,
1519 useCenter,
1520 style,
1521 shaderCaps);
1522 if (op) {
1523 this->addDrawOp(clip, std::move(op));
1524 return;
1525 }
1526 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001527 }
Brian Salomone4949402018-04-26 15:22:04 -04001528 this->drawShapeUsingPathRenderer(
1529 clip, std::move(paint), aa, viewMatrix,
1530 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001531}
1532
Brian Osman11052242016-10-27 14:47:55 -04001533void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001534 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001535 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001536 sk_sp<GrTextureProxy> image,
1537 sk_sp<GrColorSpaceXform> csxf,
1538 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001539 std::unique_ptr<SkLatticeIter> iter,
1540 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001541 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001542 RETURN_IF_ABANDONED
1543 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001544 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001545
Robert Phillips72152832017-01-25 17:31:35 -05001546 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001547
Brian Salomon2a943df2018-05-04 13:43:19 -04001548 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001549 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1550 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001551 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001552}
1553
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001554void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1555 const SkRect& bounds) {
1556 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1557 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001558 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001559}
1560
Brian Salomon031b0ba2019-05-23 11:05:26 -04001561void GrRenderTargetContext::asyncRescaleAndReadPixels(
1562 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1563 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1564 auto direct = fContext->priv().asDirectContext();
1565 if (!direct) {
1566 callback(context, nullptr, 0);
1567 return;
1568 }
1569 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1570 callback(context, nullptr, 0);
1571 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001572 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001573 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1574 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001575 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001576 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001577 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001578 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001579 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1580 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001581 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001582 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001583 backendFormatOfFinalContext, dstCT);
1584 // Fail if we can't read from the source surface's color type.
1585 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001586 callback(context, nullptr, 0);
1587 return;
1588 }
Brian Salomon624f9062019-07-02 14:23:00 -04001589 // Fail if read color type does not have all of dstCT's color channels and those missing color
1590 // channels are in the src.
1591 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1592 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1593 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1594 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001595 callback(context, nullptr, 0);
1596 return;
1597 }
1598
Brian Salomonbf6b9792019-08-21 09:38:10 -04001599 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001600 int x = srcRect.fLeft;
1601 int y = srcRect.fTop;
1602 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001603 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1604 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001605 callback(context, nullptr, 0);
1606 return;
1607 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001608 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1609 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001610 x = y = 0;
1611 } else {
1612 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001613 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1614 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001615 info.colorSpace(), info.alphaType());
1616 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1617 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001618 // We flip or color convert by drawing and we don't currently support drawing to
1619 // kPremul.
1620 if (info.alphaType() == kUnpremul_SkAlphaType) {
1621 callback(context, nullptr, 0);
1622 return;
1623 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001624 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001625 SkRect srcRectToDraw = SkRect::Make(srcRect);
1626 // If the src is not texturable first try to make a copy to a texture.
1627 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001628 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1629 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1630 SkBudgeted::kNo);
1631 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001632 callback(context, nullptr, 0);
1633 return;
1634 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001635 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1636 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001637 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001638 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1639 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1640 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001641 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001642 callback(context, nullptr, 0);
1643 return;
1644 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001645 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1646 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1647 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1648 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1649 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001650 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001651 }
1652 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001653 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001654 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1655 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001656}
1657
Brian Salomon024bd002019-06-11 11:38:16 -04001658void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1659 ReadPixelsCallback callback,
1660 ReadPixelsContext context) {
1661 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1662 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1663
1664 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1665
1666 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001667 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001668 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1669 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001670 this->colorSpaceInfo().refColorSpace());
1671 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001672 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001673 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001674 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001675 callback(context, pm.addr(), pm.rowBytes());
1676 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001677 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001678
Brian Salomonab32f652019-05-10 14:24:50 -04001679 struct FinishContext {
1680 ReadPixelsCallback* fClientCallback;
1681 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001682 int fW, fH;
1683 SkColorType fColorType;
1684 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001685 };
1686 // Assumption is that the caller would like to flush. We could take a parameter or require an
1687 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1688 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001689 auto* finishContext = new FinishContext{callback, context, rect.width(),
1690 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001691 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001692 const auto* context = reinterpret_cast<const FinishContext*>(c);
1693 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001694 if (!data) {
1695 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1696 delete context;
1697 return;
1698 }
Brian Salomon4d036892019-07-02 15:10:58 -04001699 std::unique_ptr<char[]> tmp;
1700 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001701 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001702 tmp.reset(new char[rowBytes * context->fH]);
1703 context->fTransferResult.fPixelConverter(tmp.get(), data);
1704 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001705 }
Brian Salomon4d036892019-07-02 15:10:58 -04001706 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001707 delete context;
1708 };
1709 GrFlushInfo flushInfo;
1710 flushInfo.fFinishedContext = finishContext;
1711 flushInfo.fFinishedProc = finishCallback;
1712 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1713}
1714
1715void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1716 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1717 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1718 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1719 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1720 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1721 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1722 auto direct = fContext->priv().asDirectContext();
1723 if (!direct) {
1724 callback(context, nullptr, nullptr);
1725 return;
1726 }
1727 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1728 callback(context, nullptr, nullptr);
1729 return;
1730 }
1731 if (dstW & 0x1) {
1732 return;
1733 }
1734 int x = srcRect.fLeft;
1735 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001736 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001737 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1738 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001739 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001740 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1741 dstColorSpace);
1742 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001743 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1744 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001745 callback(context, nullptr, nullptr);
1746 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001747 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001748 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1749 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001750 x = y = 0;
1751 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001752 // We assume the caller wants kPremul. There is no way to indicate a preference.
1753 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1754 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1755 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001756 if (xform) {
1757 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1758 // TODO: Do something if the input is not a texture already.
1759 if (!texProxy) {
1760 callback(context, nullptr, nullptr);
1761 return;
1762 }
Brian Salomon024bd002019-06-11 11:38:16 -04001763 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001764 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001765 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1766 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001767 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001768 callback(context, nullptr, nullptr);
1769 return;
1770 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001771 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1772 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1773 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1774 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1775 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001776 x = y = 0;
1777 }
1778 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001779 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001780 // TODO: Do something if the input is not a texture already.
1781 if (!srcProxy) {
1782 callback(context, nullptr, nullptr);
1783 return;
1784 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001785 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1786 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1787 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1788 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1789 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1790 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1791 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1792 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1793 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001794 if (!yRTC || !uRTC || !vRTC) {
1795 callback(context, nullptr, nullptr);
1796 return;
1797 }
1798
1799 static constexpr float kRec601M[] {
1800 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1801 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1802 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1803 };
1804 static constexpr float kRec709M[] {
1805 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1806 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1807 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1808 };
1809 static constexpr float kJpegM[] {
1810 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1811 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1812 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1813 };
1814 static constexpr float kIM[] {
1815 1.f, 0.f, 0.f, 0.f,
1816 0.f, 1.f, 0.f, 0.f,
1817 0.f, 0.f, 1.f, 0.f,
1818 };
1819 const float* baseM = kIM;
1820 switch (yuvColorSpace) {
1821 case kRec601_SkYUVColorSpace:
1822 baseM = kRec601M;
1823 break;
1824 case kRec709_SkYUVColorSpace:
1825 baseM = kRec709M;
1826 break;
1827 case kJPEG_SkYUVColorSpace:
1828 baseM = kJpegM;
1829 break;
1830 case kIdentity_SkYUVColorSpace:
1831 baseM = kIM;
1832 break;
1833 }
1834 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1835
1836 auto texMatrix = SkMatrix::MakeTrans(x, y);
1837
Michael Ludwig61328202019-06-19 14:48:58 +00001838 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1839 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1840
Brian Salomon024bd002019-06-11 11:38:16 -04001841 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1842 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001843 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001844 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1845 GrPaint yPaint;
1846 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1847 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1848 yPaint.addColorFragmentProcessor(std::move(yFP));
1849 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001850 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1851 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001852 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1853 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1854 if (!yTransfer.fTransferBuffer) {
1855 callback(context, nullptr, nullptr);
1856 return;
1857 }
1858
1859 texMatrix.preScale(2.f, 2.f);
1860 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1861 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001862 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001863 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1864 GrPaint uPaint;
1865 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1866 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1867 uPaint.addColorFragmentProcessor(std::move(uFP));
1868 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001869 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1870 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001871 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1872 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1873 if (!uTransfer.fTransferBuffer) {
1874 callback(context, nullptr, nullptr);
1875 return;
1876 }
1877
1878 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1879 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001880 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001881 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1882 GrPaint vPaint;
1883 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1884 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1885 vPaint.addColorFragmentProcessor(std::move(vFP));
1886 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001887 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1888 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001889 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1890 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1891 if (!vTransfer.fTransferBuffer) {
1892 callback(context, nullptr, nullptr);
1893 return;
1894 }
1895
1896 struct FinishContext {
1897 ReadPixelsCallbackYUV420* fClientCallback;
1898 ReadPixelsContext fClientContext;
1899 int fW, fH;
1900 PixelTransferResult fYTransfer;
1901 PixelTransferResult fUTransfer;
1902 PixelTransferResult fVTransfer;
1903 };
1904 // Assumption is that the caller would like to flush. We could take a parameter or require an
1905 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1906 // callback to GrGpu until after the next flush that flushes our op list, though.
1907 auto* finishContext = new FinishContext{callback,
1908 context,
1909 dstW,
1910 dstH,
1911 std::move(yTransfer),
1912 std::move(uTransfer),
1913 std::move(vTransfer)};
1914 auto finishCallback = [](GrGpuFinishedContext c) {
1915 const auto* context = reinterpret_cast<const FinishContext*>(c);
1916 const void* y = context->fYTransfer.fTransferBuffer->map();
1917 const void* u = context->fUTransfer.fTransferBuffer->map();
1918 const void* v = context->fVTransfer.fTransferBuffer->map();
1919 if (!y || !u || !v) {
1920 if (y) {
1921 context->fYTransfer.fTransferBuffer->unmap();
1922 }
1923 if (u) {
1924 context->fUTransfer.fTransferBuffer->unmap();
1925 }
1926 if (v) {
1927 context->fVTransfer.fTransferBuffer->unmap();
1928 }
1929 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1930 delete context;
1931 return;
1932 }
1933 size_t w = SkToSizeT(context->fW);
1934 size_t h = SkToSizeT(context->fH);
1935 std::unique_ptr<uint8_t[]> yTemp;
1936 if (context->fYTransfer.fPixelConverter) {
1937 yTemp.reset(new uint8_t[w * h]);
1938 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1939 y = yTemp.get();
1940 }
1941 std::unique_ptr<uint8_t[]> uTemp;
1942 if (context->fUTransfer.fPixelConverter) {
1943 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1944 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1945 u = uTemp.get();
1946 }
1947 std::unique_ptr<uint8_t[]> vTemp;
1948 if (context->fVTransfer.fPixelConverter) {
1949 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1950 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1951 v = vTemp.get();
1952 }
1953 const void* data[] = {y, u, v};
1954 size_t rowBytes[] = {w, w / 2, w / 2};
1955 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1956 context->fYTransfer.fTransferBuffer->unmap();
1957 context->fUTransfer.fTransferBuffer->unmap();
1958 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001959 delete context;
1960 };
1961 GrFlushInfo flushInfo;
1962 flushInfo.fFinishedContext = finishContext;
1963 flushInfo.fFinishedProc = finishCallback;
1964 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001965}
1966
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001967GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1968 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001969 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001970 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001971 return GrSemaphoresSubmitted::kNo;
1972 }
robertphillips8c523e02016-07-26 07:41:00 -07001973 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001974 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001975
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001976 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001977}
1978
Greg Danielc64ee462017-06-15 16:59:49 -04001979bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001980 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001981 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001982 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001983 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001984 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001985
1986 AutoCheckFlush acf(this->drawingManager());
1987
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001988 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001989 return false;
1990 }
1991
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001992 auto direct = fContext->priv().asDirectContext();
1993 if (!direct) {
1994 return false;
1995 }
1996
1997 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001998
Greg Daniela5cb7812017-06-16 09:45:32 -04001999 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05002000 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002001 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2002 kAdopt_GrWrapOwnership);
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002003 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, std::move(sema),
Robert Phillips7c525e62018-06-12 10:11:12 -04002004 fRenderTargetProxy.get()));
Greg Danielf41b2bd2019-08-22 16:19:24 -04002005 this->getOpsTask()->addWaitOp(
Chris Dalton08755122019-08-05 16:13:47 -06002006 std::move(waitOp), GrTextureResolveManager(this->drawingManager()), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04002007 }
Greg Danielc64ee462017-06-15 16:59:49 -04002008 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002009}
joshualitt33a5fce2015-11-18 13:28:51 -08002010
Robert Phillips65a88fa2017-08-08 08:36:22 -04002011void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04002012 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06002013 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04002014}
2015
Brian Osman11052242016-10-27 14:47:55 -04002016void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002017 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002018 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002019 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002020 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002021 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002022 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002023 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002024 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002025 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2026
Brian Salomon40b77a62017-12-22 11:25:52 -05002027 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002028
Robert Phillips27927a52018-08-20 13:18:12 -04002029 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002030}
2031
2032void GrRenderTargetContext::drawShape(const GrClip& clip,
2033 GrPaint&& paint,
2034 GrAA aa,
2035 const SkMatrix& viewMatrix,
2036 const GrShape& shape) {
2037 ASSERT_SINGLE_OWNER
2038 RETURN_IF_ABANDONED
2039 SkDEBUGCODE(this->validate();)
2040 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2041
Brian Salomon2fad74a2017-12-20 13:28:55 -05002042 if (shape.isEmpty()) {
2043 if (shape.inverseFilled()) {
2044 this->drawPaint(clip, std::move(paint), viewMatrix);
2045 }
2046 return;
robertphillipsea461502015-05-26 11:38:03 -07002047 }
2048
Robert Phillips72152832017-01-25 17:31:35 -05002049 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002050
Brian Salomon2fad74a2017-12-20 13:28:55 -05002051 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002052 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002053 SkRRect rrect;
2054 // We can ignore the starting point and direction since there is no path effect.
2055 bool inverted;
2056 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2057 if (rrect.isRect()) {
2058 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2059 &shape.style());
2060 return;
2061 } else if (rrect.isOval()) {
2062 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002063 return;
2064 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002065 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2066 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002067 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2068 viewMatrix.rectStaysRect()) {
2069 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2070 // the matrix to all the points individually rather than just to the rect
2071 SkRect rects[2];
2072 if (shape.asNestedRects(rects)) {
2073 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002074 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002075 fContext, std::move(paint), viewMatrix, rects);
2076 if (op) {
2077 this->addDrawOp(clip, std::move(op));
2078 }
2079 // Returning here indicates that there is nothing to draw in this case.
2080 return;
2081 }
robertphillipsea461502015-05-26 11:38:03 -07002082 }
2083 }
robertphillips4bc31812016-03-01 12:22:49 -08002084
Brian Salomon2fad74a2017-12-20 13:28:55 -05002085 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002086}
2087
Chris Daltonbbfd5162017-11-07 13:35:22 -07002088bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002089 const GrUserStencilSettings* ss,
2090 SkRegion::Op op,
2091 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002092 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002093 const SkMatrix& viewMatrix,
2094 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002095 ASSERT_SINGLE_OWNER_PRIV
2096 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002097 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002098 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2099 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002100
2101 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002102 GrPaint paint;
2103 paint.setCoverageSetOpXPFactory(op, invert);
2104 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2105 SkRect::MakeIWH(fRenderTargetContext->width(),
2106 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002107 return true;
2108 }
2109
Robert Phillips72152832017-01-25 17:31:35 -05002110 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002111
2112 // An Assumption here is that path renderer would use some form of tweaking
2113 // the src color (either the input alpha or in the frag shader) to implement
2114 // aa. If we have some future driver-mojo path AA that can do the right
2115 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002116 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002117 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002118
Chris Daltondb91c6e2017-09-08 16:25:08 -06002119 SkIRect clipConservativeBounds;
2120 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2121 &clipConservativeBounds, nullptr);
2122
bsalomon8acedde2016-06-24 10:42:16 -07002123 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002124 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002125 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002126 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002127 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002128 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002129 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002130 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002131 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2132 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002133 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002134
2135 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002136 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002137 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002138 if (!pr) {
2139 return false;
2140 }
2141
2142 GrPaint paint;
2143 paint.setCoverageSetOpXPFactory(op, invert);
2144
Brian Salomonf3569f02017-10-24 12:52:33 -04002145 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2146 std::move(paint),
2147 ss,
2148 fRenderTargetContext,
2149 &clip,
2150 &clipConservativeBounds,
2151 &viewMatrix,
2152 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002153 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002154 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002155 pr->drawPath(args);
2156 return true;
2157}
2158
Brian Osman11052242016-10-27 14:47:55 -04002159SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002160 ASSERT_SINGLE_OWNER_PRIV
2161
Robert Phillips6a6de562019-02-15 15:19:15 -05002162 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002163 return SkBudgeted::kNo;
2164 }
2165
Brian Osman11052242016-10-27 14:47:55 -04002166 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002167
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002168 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002169}
2170
Brian Salomon2fad74a2017-12-20 13:28:55 -05002171void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2172 GrPaint&& paint,
2173 GrAA aa,
2174 const SkMatrix& viewMatrix,
2175 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002176 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002177 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002178 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2179
Jim Van Verthf86073a2018-10-02 13:05:38 -04002180 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2181 return;
2182 }
2183
Chris Daltondb91c6e2017-09-08 16:25:08 -06002184 SkIRect clipConservativeBounds;
2185 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2186
Brian Salomon2fad74a2017-12-20 13:28:55 -05002187 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002188 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002189
robertphillips68737822015-10-29 12:12:21 -07002190 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002191 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002192 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002193 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002194 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002195 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002196 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002197 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002198
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002199 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002200 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002201 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002202 return;
2203 }
2204
Chris Dalton6ce447a2019-06-23 18:07:38 -06002205 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002206
2207 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2208 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2209 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2210
Brian Salomon2fad74a2017-12-20 13:28:55 -05002211 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002212 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002213 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2214 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002215 return;
2216 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002217 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002218 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002219 }
2220 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002221 if (canDrawArgs.fShape->style().applies()) {
2222 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2223 styleScale);
2224 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002225 return;
2226 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002227 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002228 // This time, allow SW renderer
2229 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2230 } else {
2231 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002232 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002233 }
robertphillipsea461502015-05-26 11:38:03 -07002234
bsalomon8acedde2016-06-24 10:42:16 -07002235 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002236#ifdef SK_DEBUG
2237 SkDebugf("Unable to find path renderer compatible with path.\n");
2238#endif
2239 return;
2240 }
2241
Robert Phillips256c37b2017-03-01 14:32:46 -05002242 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002243 std::move(paint),
2244 &GrUserStencilSettings::kUnused,
2245 this,
2246 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002247 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002248 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002249 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002250 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002251 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002252 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002253}
2254
Brian Salomon467921e2017-03-06 16:17:12 -05002255static void op_bounds(SkRect* bounds, const GrOp* op) {
2256 *bounds = op->bounds();
2257 if (op->hasZeroArea()) {
2258 if (op->hasAABloat()) {
2259 bounds->outset(0.5f, 0.5f);
2260 } else {
2261 // We don't know which way the particular GPU will snap lines or points at integer
2262 // coords. So we ensure that the bounds is large enough for either snap.
2263 SkRect before = *bounds;
2264 bounds->roundOut(bounds);
2265 if (bounds->fLeft == before.fLeft) {
2266 bounds->fLeft -= 1;
2267 }
2268 if (bounds->fTop == before.fTop) {
2269 bounds->fTop -= 1;
2270 }
2271 if (bounds->fRight == before.fRight) {
2272 bounds->fRight += 1;
2273 }
2274 if (bounds->fBottom == before.fBottom) {
2275 bounds->fBottom += 1;
2276 }
2277 }
2278 }
2279}
2280
Chris Dalton08755122019-08-05 16:13:47 -06002281void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002282 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002283 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2284}
2285
Brian Salomon348a0372018-10-31 10:42:18 -04002286void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2287 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002288 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002289 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002290 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002291 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002292 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002293 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002294 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002295 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002296
Brian Salomon467921e2017-03-06 16:17:12 -05002297 // Setup clip
2298 SkRect bounds;
2299 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002300 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002301 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002302 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2303 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2304
2305 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002306 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002307 }
2308
2309 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002310 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002311 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002312 }
2313
Chris Daltoneffee202019-07-01 22:28:03 -06002314 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002315
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002316 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002317 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2318 // here as well.
2319 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2320#ifdef SK_DEBUG
2321 if (hasMixedSampledCoverage) {
2322 SkASSERT(usesStencil);
2323 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2324 }
2325#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002326 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002327 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2328
2329 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002330 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002331 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002332 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002333 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002334 }
2335 }
2336
2337 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002338 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002339 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002340 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002341 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002342 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2343 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002344}
2345
Chris Dalton2243c7b2019-08-21 14:46:35 -06002346bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2347 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002348 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2349 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2350 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002351 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002352 return false;
2353 }
2354
Chris Dalton2243c7b2019-08-21 14:46:35 -06002355 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2356 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002357 // The render target is a texture, so we can read from it directly in the shader. The XP
2358 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002359 dstProxy->setProxy(sk_ref_sp(texProxy));
2360 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002361 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002362 }
2363 }
2364
Chris Dalton2243c7b2019-08-21 14:46:35 -06002365 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002366
Eric Karl74480882017-04-03 14:49:05 -07002367 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002368 clip.getConservativeBounds(
2369 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002370 SkRect opBounds = op.bounds();
2371 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2372 // 0.5 pixels.
2373 if (op.hasAABloat() || op.hasZeroArea()) {
2374 opBounds.outset(0.5f, 0.5f);
2375 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2376 // performance we may ignore the clip when the draw is entirely inside the clip is float
2377 // space but will hit pixels just outside the clip when actually rasterizing.
2378 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002379 clippedRect.intersect(SkIRect::MakeWH(
2380 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002381 }
2382 SkIRect opIBounds;
2383 opBounds.roundOut(&opIBounds);
2384 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002385#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002386 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002387#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002388 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002389 }
2390
2391 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2392 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002393 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002394 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002395
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002396 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002397 copyRect = clippedRect;
2398 }
Brian Salomon467921e2017-03-06 16:17:12 -05002399
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002400 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002401 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002402 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002403 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002404 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002405 } else {
Eric Karl74480882017-04-03 14:49:05 -07002406 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002407 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002408 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002409 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2410 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2411 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002412 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002413
Greg Daniel46cfbc62019-06-07 11:43:30 -04002414 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002415 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002416 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002417}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002418
2419bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2420 const SkIPoint& dstPoint) {
2421 SkIRect clippedSrcRect;
2422 SkIPoint clippedDstPoint;
2423 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2424 &clippedSrcRect, &clippedDstPoint)) {
2425 return false;
2426 }
2427
2428 GrPaint paint;
2429 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2430 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2431 SkMatrix::I());
2432 if (!fp) {
2433 return false;
2434 }
2435 paint.addColorFragmentProcessor(std::move(fp));
2436
2437 this->fillRectToRect(
2438 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2439 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2440 clippedSrcRect.height()),
2441 SkRect::Make(clippedSrcRect));
2442 return true;
2443}
2444