blob: edb76114939e4a628c0e64c5d14153a275fdce0d [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkDrawable.h"
9#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
11#include "include/private/SkShadowFlags.h"
12#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkDrawShadowInfo.h"
16#include "src/core/SkGlyphRunPainter.h"
17#include "src/core/SkLatticeIter.h"
18#include "src/core/SkMatrixPriv.h"
19#include "src/core/SkRRectPriv.h"
20#include "src/core/SkSurfacePriv.h"
21#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrBlurUtils.h"
24#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040025#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040027#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrDrawingManager.h"
29#include "src/gpu/GrFixedClip.h"
30#include "src/gpu/GrGpuResourcePriv.h"
31#include "src/gpu/GrMemoryPool.h"
32#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040034#include "src/gpu/GrRenderTarget.h"
Mike Klein52337de2019-07-25 09:00:52 -050035#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrRenderTargetContextPriv.h"
37#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrStencilAttachment.h"
39#include "src/gpu/GrStyle.h"
40#include "src/gpu/GrTracing.h"
41#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040042#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/effects/GrRRectEffect.h"
44#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040045#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040046#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000047#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040048#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/ops/GrAtlasTextOp.h"
50#include "src/gpu/ops/GrClearOp.h"
51#include "src/gpu/ops/GrClearStencilClipOp.h"
52#include "src/gpu/ops/GrDebugMarkerOp.h"
53#include "src/gpu/ops/GrDrawAtlasOp.h"
54#include "src/gpu/ops/GrDrawOp.h"
55#include "src/gpu/ops/GrDrawVerticesOp.h"
56#include "src/gpu/ops/GrDrawableOp.h"
57#include "src/gpu/ops/GrFillRRectOp.h"
58#include "src/gpu/ops/GrFillRectOp.h"
59#include "src/gpu/ops/GrLatticeOp.h"
60#include "src/gpu/ops/GrOp.h"
61#include "src/gpu/ops/GrOvalOpFactory.h"
62#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050063#include "src/gpu/ops/GrShadowRRectOp.h"
64#include "src/gpu/ops/GrStencilPathOp.h"
65#include "src/gpu/ops/GrStrokeRectOp.h"
66#include "src/gpu/ops/GrTextureOp.h"
67#include "src/gpu/text/GrTextContext.h"
68#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040069
Herb Derbyc1b482c2018-08-09 15:02:27 -040070class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040071public:
72 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040073 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Robert Phillips7e90be92019-02-15 12:22:59 -050074 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040075 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040076 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040077
Robert Phillips7c525e62018-06-12 10:11:12 -040078 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040079 fRenderTargetContext->addDrawOp(clip, std::move(op));
80 }
81
Robert Phillips46a13382018-08-23 13:53:01 -040082 void drawShape(const GrClip& clip, const SkPaint& paint,
83 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040084 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
85 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040086 }
87
88 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040089 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050090 auto context = fRenderTargetContext->fContext;
Brian Salomonf18b1d82017-10-27 11:30:49 -040091 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
92 if (kARGB_GrMaskFormat == maskFormat) {
93 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
94 } else {
95 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
96 }
97 }
98
Robert Phillips69893702019-02-22 11:16:30 -050099 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400100 return fRenderTargetContext->fContext;
101 }
102
Herb Derby65956872018-08-21 16:55:04 -0400103 SkGlyphRunListPainter* glyphPainter() override {
104 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400105 }
106
Brian Salomonf18b1d82017-10-27 11:30:49 -0400107private:
108 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400109 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400110
Brian Salomonf18b1d82017-10-27 11:30:49 -0400111};
joshualittbc907352016-01-13 06:45:40 -0800112
Robert Phillips72152832017-01-25 17:31:35 -0500113#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800114#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400115 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800116#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400117 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500118#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
119#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
120#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
121#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
122#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700123
Brian Salomone225b562017-06-14 13:00:03 -0400124//////////////////////////////////////////////////////////////////////////////
125
robertphillipsea461502015-05-26 11:38:03 -0700126class AutoCheckFlush {
127public:
halcanary9d524f22016-03-29 09:03:52 -0700128 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700129 SkASSERT(fDrawingManager);
130 }
bsalomonb77a9072016-09-07 10:02:04 -0700131 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700132
133private:
robertphillips77a2e522015-10-17 07:43:27 -0700134 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700135};
136
Greg Danielf41b2bd2019-08-22 16:19:24 -0400137// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
138// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
139// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
140// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500141GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400142 sk_sp<GrRenderTargetProxy> rtp,
Brian Salomond6287472019-06-24 15:50:07 -0400143 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400144 sk_sp<SkColorSpace> colorSpace,
145 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400146 bool managedOpsTask)
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400147 : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace))
Brian Salomonf3569f02017-10-24 12:52:33 -0400148 , fRenderTargetProxy(std::move(rtp))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400149 , fOpsTask(sk_ref_sp(fRenderTargetProxy->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400150 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400151 , fManagedOpsTask(managedOpsTask) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400152 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700153 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700154}
155
robertphillips2e1e51f2015-10-15 08:01:48 -0700156#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400157void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400158 SkASSERT(fRenderTargetProxy);
159 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700160
Greg Danielf41b2bd2019-08-22 16:19:24 -0400161 if (fOpsTask && !fOpsTask->isClosed()) {
162 SkASSERT(fRenderTargetProxy->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700163 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700164}
165#endif
166
Brian Osman11052242016-10-27 14:47:55 -0400167GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800168 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700169}
170
Chris Dalton7d6748e2019-03-13 00:34:52 -0600171inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600172 if (GrAA::kNo == aa) {
173 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
174 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600175 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600176 return GrAAType::kMSAA;
177 }
178 return GrAAType::kNone;
179 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600180 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600181}
182
Robert Phillipsf200a902017-01-30 13:27:37 -0500183GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000184 return fRenderTargetProxy->asTextureProxy();
185}
186
Greg Daniele252f082017-10-23 16:05:23 -0400187const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
188 return fRenderTargetProxy->asTextureProxy();
189}
190
Robert Phillipsf200a902017-01-30 13:27:37 -0500191sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
192 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
193}
194
Greg Daniele252f082017-10-23 16:05:23 -0400195GrMipMapped GrRenderTargetContext::mipMapped() const {
196 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
197 return proxy->mipMapped();
198 }
199 return GrMipMapped::kNo;
200}
201
Greg Danielf41b2bd2019-08-22 16:19:24 -0400202GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800203 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700204 SkDEBUGCODE(this->validate();)
205
Greg Danielf41b2bd2019-08-22 16:19:24 -0400206 if (!fOpsTask || fOpsTask->isClosed()) {
207 fOpsTask = this->drawingManager()->newOpsTask(fRenderTargetProxy, fManagedOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700208 }
209
Greg Danielf41b2bd2019-08-22 16:19:24 -0400210 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700211}
212
Herb Derbycddab252018-07-16 11:19:04 -0400213void GrRenderTargetContext::drawGlyphRunList(
214 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400215 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800216 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700217 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700218 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400219 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700220
Greg Danielbe7fc462019-01-03 16:40:42 -0500221 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
222 // secondary command buffers because it would require stopping and starting a render pass which
223 // we don't have access to.
224 if (this->wrapsVkSecondaryCB()) {
225 return;
226 }
227
Herb Derby26cbe512018-05-24 14:39:01 -0400228 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400229 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400230 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700231}
232
Brian Osman11052242016-10-27 14:47:55 -0400233void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800234 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700235 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700236 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400237 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700238
Robert Phillips72152832017-01-25 17:31:35 -0500239 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400240
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700242}
243
Brian Osman11052242016-10-27 14:47:55 -0400244void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500245 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700246 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800247 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700248 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700249 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400250 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700251
Robert Phillips72152832017-01-25 17:31:35 -0500252 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700253 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
254 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700255}
robertphillips9199a9f2016-07-13 07:48:43 -0700256
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500257void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
258 const SkPMColor4f& color,
259 CanClearFullscreen canClearFullscreen) {
260 ASSERT_SINGLE_OWNER_PRIV
261 RETURN_IF_ABANDONED_PRIV
262 SkDEBUGCODE(fRenderTargetContext->validate();)
263 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
264 fRenderTargetContext->fContext);
265
266 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
267 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
268}
269
270static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
271 paint->setColor4f(color);
272 if (color.isOpaque()) {
273 // Can just rely on the src-over blend mode to do the right thing
274 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
275 } else {
276 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
277 // were src blended
278 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
279 }
280}
281
282void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
283 const SkPMColor4f& color,
284 CanClearFullscreen canClearFullscreen) {
285 bool isFull = false;
286 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400287 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
288 // only clear the entire target if we knew it had not been cleared before. As
289 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500290 isFull = !clip.scissorEnabled() ||
291 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400292 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500293 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
294 }
295
296 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400297 GrOpsTask* opsTask = this->getOpsTask();
298 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500299 !this->caps()->performColorClearsAsDraws()) {
300 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400301 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500302 return;
303 } else {
304 // Will use an op for the clear, reset the load op to discard since the op will
305 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400306 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500307 }
308
309 // Must add an op to the list (either because we couldn't use a load op, or because the
310 // clear load op isn't supported)
311 if (this->caps()->performColorClearsAsDraws()) {
312 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
313 GrPaint paint;
314 clear_to_grpaint(color, &paint);
315 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400316 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
317 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500318 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600319 this->addOp(GrClearOp::Make(
320 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500321 }
322 } else {
323 if (this->caps()->performPartialClearsAsDraws()) {
324 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500325 GrPaint paint;
326 clear_to_grpaint(color, &paint);
327
Michael Ludwig64b28a72019-05-28 12:02:00 -0400328 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400329 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
330 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500331 } else {
332 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
333 this->asSurfaceProxy()));
334 // This version of the clear op factory can return null if the clip doesn't intersect
335 // with the surface proxy's boundary
336 if (!op) {
337 return;
338 }
Chris Dalton08755122019-08-05 16:13:47 -0600339 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500340 }
341 }
342}
343
Greg Daniel674ee742019-08-27 13:12:33 -0400344void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500345 ASSERT_SINGLE_OWNER_PRIV
346 RETURN_IF_ABANDONED_PRIV
347 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400348 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
349 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500350
Robert Phillips72152832017-01-25 17:31:35 -0500351 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500352
Brian Salomonbb5711a2017-05-17 13:49:59 -0400353 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
354 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500355
356 if (clearRect) {
357 if (clearRect->contains(rtRect)) {
358 clearRect = nullptr; // full screen
359 } else {
360 if (!rtRect.intersect(*clearRect)) {
361 return;
362 }
363 }
364 }
365
Greg Daniel674ee742019-08-27 13:12:33 -0400366 static const SkPMColor4f kColor = SK_PMColor4fTRANSPARENT;
367
Greg Danielf41b2bd2019-08-22 16:19:24 -0400368 // TODO: in a post-MDB world this should be handled at the OpsTask level.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500369 // This makes sure to always add an op to the list, instead of marking the clear as a load op.
370 // This code follows very similar logic to internalClear() below, but critical differences are
371 // highlighted in line related to absClear()'s unique behavior.
372 if (clearRect) {
373 if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400374 GrPaint paint;
Greg Daniel674ee742019-08-27 13:12:33 -0400375 clear_to_grpaint(kColor, &paint);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400376
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500377 // Use the disabled clip; the rect geometry already matches the clear rectangle and
378 // if it were added to a scissor, that would be intersected with the logical surface
379 // bounds and not the worst case dimensions required here.
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400380 fRenderTargetContext->addDrawOp(
381 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400382 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
383 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500384 } else {
385 // Must use the ClearOp factory that takes a boolean (false) instead of a surface
386 // proxy. The surface proxy variant would intersect the clip rect with its logical
387 // bounds, which is not desired in this special case.
Chris Dalton08755122019-08-05 16:13:47 -0600388 fRenderTargetContext->addOp(GrClearOp::Make(
Greg Daniel674ee742019-08-27 13:12:33 -0400389 fRenderTargetContext->fContext, rtRect, kColor, /* fullscreen */ false));
csmartdalton29df7602016-08-31 11:55:52 -0700390 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500391 } else {
Greg Daniel674ee742019-08-27 13:12:33 -0400392 if (fRenderTargetContext->getOpsTask()->resetForFullscreenClear(
393 fRenderTargetContext->canDiscardPreviousOpsOnFullClear()) &&
394 !fRenderTargetContext->caps()->performColorClearsAsDraws()) {
395 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kClear, kColor);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500396 } else {
Greg Daniel674ee742019-08-27 13:12:33 -0400397 fRenderTargetContext->getOpsTask()->setColorLoadOp(GrLoadOp::kDiscard);
398
399 if (fRenderTargetContext->caps()->performColorClearsAsDraws()) {
400 // This draws a quad covering the worst case dimensions instead of just the logical
401 // width and height like in internalClear().
402 GrPaint paint;
403 clear_to_grpaint(kColor, &paint);
404 fRenderTargetContext->addDrawOp(
405 GrFixedClip::Disabled(),
406 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext,
407 std::move(paint), SkMatrix::I(),
408 SkRect::Make(rtRect)));
409 } else {
410 // Nothing special about this path in absClear compared to internalClear()
411 fRenderTargetContext->addOp(GrClearOp::Make(
412 fRenderTargetContext->fContext, SkIRect::MakeEmpty(), kColor,
413 /* fullscreen */ true));
414 }
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400415 }
robertphillips9199a9f2016-07-13 07:48:43 -0700416 }
robertphillipsea461502015-05-26 11:38:03 -0700417}
418
Brian Osman11052242016-10-27 14:47:55 -0400419void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500420 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400421 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000422 // Start with the render target, since that is the maximum content we could possibly fill.
423 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400424 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400425 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000426 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
427 // inverse view matrix.
428 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
429 } else {
430 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
431 SkMatrix localMatrix;
432 if (!viewMatrix.invert(&localMatrix)) {
433 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400434 }
Michael Ludwig61328202019-06-19 14:48:58 +0000435 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
436 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700437 }
robertphillipsea461502015-05-26 11:38:03 -0700438}
439
Michael Ludwig61328202019-06-19 14:48:58 +0000440enum class GrRenderTargetContext::QuadOptimization {
441 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
442 kDiscarded,
443 // The rect to draw was converted to some other op and appended to the oplist, so no additional
444 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
445 // a constColor is provided.
446 kSubmitted,
447 // The clip was folded into the device quad, with updated edge flags and local coords, and
448 // caller is responsible for adding an appropriate op.
449 kClipApplied,
450 // No change to clip, but quad updated to better fit clip/render target, and caller is
451 // responsible for adding an appropriate op.
452 kCropped
453};
Michael Ludwig61a16512019-01-15 11:15:13 -0500454
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400455static bool make_vertex_finite(float* value) {
456 if (SkScalarIsNaN(*value)) {
457 return false;
458 }
459
460 if (!SkScalarIsFinite(*value)) {
461 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
462 // left when calculating crops.
463 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
464 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
465 }
466
467 return true;
468}
469
Michael Ludwig61328202019-06-19 14:48:58 +0000470GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400471 const GrClip& clip, const SkPMColor4f* constColor,
472 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
473 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000474 // Optimization requirements:
475 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
476 // 2. kClear applies when constColor and final geom is pixel aligned rect;
477 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
478 // 3. kRRect applies when constColor and rrect clip and quad covers clip
479 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
480 // 5. kCropped applies when rect quad (currently)
481 // 6. kNone always applies
482 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000483
Michael Ludwige08b4432019-06-19 18:00:48 -0400484 SkRect rtRect;
485 if (stencilSettings) {
486 // Must use worst case bounds so that stencil buffer updates on approximately sized render
487 // targets don't get corrupted.
488 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
489 fRenderTargetProxy->worstCaseHeight());
490 } else {
491 // Use the logical size of the render target, which allows for "fullscreen" clears even if
492 // the render target has an approximate backing fit
493 rtRect = SkRect::MakeWH(this->width(), this->height());
494 }
495
Michael Ludwig61328202019-06-19 14:48:58 +0000496 SkRect drawBounds = deviceQuad->bounds();
497 if (constColor) {
498 // Don't bother updating local coordinates when the paint will ignore them anyways
499 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400500 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
501 // will be cropped to the finite 'clip' or render target and there is no local space mapping
502 if (!deviceQuad->isFinite()) {
503 for (int i = 0; i < 4; ++i) {
504 if (!make_vertex_finite(deviceQuad->xs() + i) ||
505 !make_vertex_finite(deviceQuad->ys() + i) ||
506 !make_vertex_finite(deviceQuad->ws() + i)) {
507 // Discard if we see a nan
508 return QuadOptimization::kDiscarded;
509 }
510 }
511 SkASSERT(deviceQuad->isFinite());
512 }
513 } else {
514 // CropToRect requires the quads to be finite. If they are not finite and we have local
515 // coordinates, the mapping from local space to device space is poorly defined so drop it
516 if (!deviceQuad->isFinite()) {
517 return QuadOptimization::kDiscarded;
518 }
Brian Salomon7694b902019-06-18 21:00:21 +0000519 }
520
Michael Ludwig61328202019-06-19 14:48:58 +0000521 // If the quad is entirely off screen, it doesn't matter what the clip does
522 if (!rtRect.intersects(drawBounds)) {
523 return QuadOptimization::kDiscarded;
524 }
Brian Salomon7694b902019-06-18 21:00:21 +0000525
Michael Ludwig61328202019-06-19 14:48:58 +0000526 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
527 // the render target).
528 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400529 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
530 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
531 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
532 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000533 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000534 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000535 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
536 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000537 }
Brian Salomon7694b902019-06-18 21:00:21 +0000538 }
539
Michael Ludwig61328202019-06-19 14:48:58 +0000540 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
541 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400542 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000543 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
544 // of the render target and the clip rect)
545 SkRect clipBounds = rtRect;
546 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
547 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000548 }
549
Michael Ludwig61328202019-06-19 14:48:58 +0000550 if (clipRRect.isRect()) {
551 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
552 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
553 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
554 // Clear optimization is possible
555 drawBounds = deviceQuad->bounds();
556 if (drawBounds.contains(rtRect)) {
557 // Fullscreen clear
558 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
559 return QuadOptimization::kSubmitted;
560 } else if (GrClip::IsPixelAligned(drawBounds) &&
561 drawBounds.width() > 256 && drawBounds.height() > 256) {
562 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
563 SkIRect scissorRect;
564 drawBounds.round(&scissorRect);
565 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
566 return QuadOptimization::kSubmitted;
567 }
568 }
569
570 // Update overall AA setting.
571 *edgeFlags = newFlags;
572 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
573 newFlags != GrQuadAAFlags::kNone) {
574 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
575 // properly reflect the smooth edge of the clip.
576 *aa = GrAA::kYes;
577 }
578 // We intentionally do not downgrade AA here because we don't know if we need to
579 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
580 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
581
582 // deviceQuad is exactly the intersection of original quad and clip, so it can be
583 // drawn with no clip (submitted by caller)
584 return QuadOptimization::kClipApplied;
585 } else {
586 // The quads have been updated to better fit the clip bounds, but can't get rid of
587 // the clip entirely
588 return QuadOptimization::kCropped;
589 }
590 } else if (constColor) {
591 // Rounded corners and constant filled color (limit ourselves to solid colors because
592 // there is no way to use custom local coordinates with drawRRect).
593 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
594 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
595 deviceQuad->bounds().contains(clipBounds)) {
596 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
597 // we can draw the rrect directly and ignore the edge flags
598 GrPaint paint;
599 clear_to_grpaint(*constColor, &paint);
600 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
601 clipRRect, GrStyle::SimpleFill());
602 return QuadOptimization::kSubmitted;
603 } else {
604 // The quad has been updated to better fit clip bounds, but can't remove the clip
605 return QuadOptimization::kCropped;
606 }
Brian Salomon7694b902019-06-18 21:00:21 +0000607 }
Brian Salomon7694b902019-06-18 21:00:21 +0000608 }
609
Michael Ludwig61328202019-06-19 14:48:58 +0000610 // Crop the quad to the conservative bounds of the clip.
611 SkIRect clipDevBounds;
612 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
613 SkRect clipBounds = SkRect::Make(clipDevBounds);
614
615 // One final check for discarding, since we may have gone here directly due to a complex clip
616 if (!clipBounds.intersects(drawBounds)) {
617 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000618 }
619
Michael Ludwig61328202019-06-19 14:48:58 +0000620 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
621 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
622 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
623
624 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000625}
626
Michael Ludwig61328202019-06-19 14:48:58 +0000627void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500628 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500629 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000630 GrQuadAAFlags edgeFlags,
631 const GrQuad& deviceQuad,
632 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400633 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000634 ASSERT_SINGLE_OWNER
635 RETURN_IF_ABANDONED
636 SkDEBUGCODE(this->validate();)
637 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500638
Michael Ludwig61328202019-06-19 14:48:58 +0000639 AutoCheckFlush acf(this->drawingManager());
640
641 SkPMColor4f* constColor = nullptr;
642 SkPMColor4f paintColor;
643 if (!ss && !paint.numCoverageFragmentProcessors() &&
644 paint.isConstantBlendedColor(&paintColor)) {
645 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
646 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500647 }
648
Michael Ludwig61328202019-06-19 14:48:58 +0000649 GrQuad croppedDeviceQuad = deviceQuad;
650 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400651 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
652 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000653 if (opt >= QuadOptimization::kClipApplied) {
654 // These optimizations require caller to add an op themselves
655 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
656 : clip;
657 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
658 : this->chooseAAType(aa);
659 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
660 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700661 }
Michael Ludwig61328202019-06-19 14:48:58 +0000662 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800663}
664
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000665void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
666 sk_sp<GrTextureProxy> proxy,
667 sk_sp<GrColorSpaceXform> textureXform,
668 GrSamplerState::Filter filter,
669 const SkPMColor4f& color,
670 SkBlendMode blendMode,
671 GrAA aa,
672 GrQuadAAFlags edgeFlags,
673 const GrQuad& deviceQuad,
674 const GrQuad& localQuad,
675 const SkRect* domain) {
676 ASSERT_SINGLE_OWNER
677 RETURN_IF_ABANDONED
678 SkDEBUGCODE(this->validate();)
679 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
680
681 AutoCheckFlush acf(this->drawingManager());
682
683 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
684 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
685 GrQuad croppedDeviceQuad = deviceQuad;
686 GrQuad croppedLocalQuad = localQuad;
687 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
688 &croppedDeviceQuad, &croppedLocalQuad);
689
690 SkASSERT(opt != QuadOptimization::kSubmitted);
691 if (opt != QuadOptimization::kDiscarded) {
692 // And the texture op if not discarded
693 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
694 : clip;
695 GrAAType aaType = this->chooseAAType(aa);
696 // Use the provided domain, although hypothetically we could detect that the cropped local
697 // quad is sufficiently inside the domain and the constraint could be dropped.
698 this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy),
699 std::move(textureXform), filter, color,
700 blendMode, aaType, edgeFlags,
701 croppedDeviceQuad, croppedLocalQuad, domain));
702 }
703}
704
Brian Osman11052242016-10-27 14:47:55 -0400705void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500706 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500707 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400708 const SkMatrix& viewMatrix,
709 const SkRect& rect,
710 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700711 if (!style) {
712 style = &GrStyle::SimpleFill();
713 }
joshualitt1de610a2016-01-06 08:26:09 -0800714 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700715 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700716 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400717 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700718
bsalomon6663acf2016-05-10 09:14:17 -0700719 // Path effects should've been devolved to a path in SkGpuDevice
720 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700721
Robert Phillips72152832017-01-25 17:31:35 -0500722 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700723
bsalomon6663acf2016-05-10 09:14:17 -0700724 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400725 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000726 // Fills the rect, using rect as its own local coordinates
727 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500728 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700729 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
730 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
731 if ((!rect.width() || !rect.height()) &&
732 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
733 SkScalar r = stroke.getWidth() / 2;
734 // TODO: Move these stroke->fill fallbacks to GrShape?
735 switch (stroke.getJoin()) {
736 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500737 this->drawRect(
738 clip, std::move(paint), aa, viewMatrix,
739 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
740 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700741 return;
742 case SkPaint::kRound_Join:
743 // Raster draws nothing when both dimensions are empty.
744 if (rect.width() || rect.height()){
745 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500746 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
747 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700748 return;
749 }
750 case SkPaint::kBevel_Join:
751 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500752 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700753 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
754 &GrStyle::SimpleFill());
755 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500756 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700757 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
758 &GrStyle::SimpleFill());
759 }
760 return;
761 }
762 }
robertphillips44302392016-07-08 14:43:03 -0700763
Brian Salomonbaaf4392017-06-15 09:59:23 -0400764 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700765
Chris Dalton7d6748e2019-03-13 00:34:52 -0600766 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500767 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
768 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
769 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500770 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400771 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700772 return;
robertphillips4bc31812016-03-01 12:22:49 -0800773 }
robertphillips4bc31812016-03-01 12:22:49 -0800774 }
Mike Klein16885072018-12-11 09:54:31 -0500775 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500776 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700777}
778
Michael Ludwig69858532018-11-28 15:34:34 -0500779void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
780 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
781 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600782 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500783 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
784 quads, cnt));
785}
786
Robert Phillipsec2249f2016-11-09 08:54:35 -0500787int GrRenderTargetContextPriv::maxWindowRectangles() const {
788 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400789 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500790}
791
Greg Danielf41b2bd2019-08-22 16:19:24 -0400792GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600793 ) const {
794#if GR_TEST_UTILS
795 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400796 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600797 }
798#endif
799 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
800 // would normally be overwritten. The one exception is if the render target context is marked as
801 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
802 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
803 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
804 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400805 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600806}
807
Chris Daltoneffee202019-07-01 22:28:03 -0600808void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
809 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600810 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600811 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600812
Chris Daltoneffee202019-07-01 22:28:03 -0600813 int numRequiredSamples = this->numSamples();
814 if (multisampled && 1 == numRequiredSamples) {
815 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
816 // mixed samples.
817 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
818 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400819 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600820 }
821 SkASSERT(numRequiredSamples > 0);
822
823 if (numRequiredSamples > fNumStencilSamples) {
824 fNumStencilSamples = numRequiredSamples;
825 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
826 }
Chris Dalton6b982802019-06-27 13:53:46 -0600827
828 if (needsStencilClear) {
829 if (this->caps()->performStencilClearsAsDraws()) {
830 // There is a driver bug with clearing stencil. We must use an op to manually clear the
831 // stencil buffer before the op that required 'setNeedsStencil'.
832 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
833 } else {
834 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
835 // code note when the instantiated stencil buffer is already clear and skip the clear
836 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
837 // preserving the previous contents.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400838 this->getOpsTask()->setStencilLoadOp(GrLoadOp::kClear);
Chris Dalton6b982802019-06-27 13:53:46 -0600839 }
840 }
841}
842
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000843void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700844 ASSERT_SINGLE_OWNER_PRIV
845 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400846 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400847 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
848 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700849
Robert Phillips72152832017-01-25 17:31:35 -0500850 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400851
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500852 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
853}
854
855void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
856 if (this->caps()->performStencilClearsAsDraws()) {
857 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
858 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
859
860 // Configure the paint to have no impact on the color buffer
861 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400862 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400863 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
864 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500865 } else {
866 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
867 fRenderTargetProxy.get()));
868 if (!op) {
869 return;
870 }
Chris Dalton08755122019-08-05 16:13:47 -0600871 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500872 }
robertphillips976f5f02016-06-03 10:59:20 -0700873}
874
Chris Daltonbbfd5162017-11-07 13:35:22 -0700875void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600876 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400877 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400878 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500879 ASSERT_SINGLE_OWNER_PRIV
880 RETURN_IF_ABANDONED_PRIV
881 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400882 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
883 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500884
Brian Salomon467921e2017-03-06 16:17:12 -0500885 // TODO: extract portions of checkDraw that are relevant to path stenciling.
886 SkASSERT(path);
887 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
888
889 // FIXME: Use path bounds instead of this WAR once
890 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
891 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
892
893 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700894 GrAppliedHardClip appliedClip;
895 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
896 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500897 return;
898 }
899
Robert Phillips7c525e62018-06-12 10:11:12 -0400900 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
901 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600902 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500903 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500904 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400905 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400906 if (!op) {
907 return;
908 }
Brian Salomon97180af2017-03-14 13:42:58 -0400909 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600910
Chris Daltoneffee202019-07-01 22:28:03 -0600911 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600912 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700913}
914
Brian Salomond7065e72018-10-12 11:42:02 -0400915void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500916 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400917 GrAA aa, SkCanvas::SrcRectConstraint constraint,
918 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500919 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400920 ASSERT_SINGLE_OWNER
921 RETURN_IF_ABANDONED
922 SkDEBUGCODE(this->validate();)
923 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500924
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500925 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500926 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400927 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
928 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500929 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500930 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500931 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500932 ctm = viewMatrix;
933 if (set[i].fPreViewMatrix) {
934 ctm.preConcat(*set[i].fPreViewMatrix);
935 }
936
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000937 GrQuad quad, srcQuad;
938 if (set[i].fDstClipQuad) {
939 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
940
941 SkPoint srcPts[4];
942 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
943 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500944 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000945 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
946 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500947 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000948
949 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
950 ? &set[i].fSrcRect : nullptr;
951 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
952 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
953 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500954 }
955 } else {
956 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500957 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600958 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400959 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500960 std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500961 this->addDrawOp(clip, std::move(op));
962 }
Brian Salomond7065e72018-10-12 11:42:02 -0400963}
964
Brian Osman11052242016-10-27 14:47:55 -0400965void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500966 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400967 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400968 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400969 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400970 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400971 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500972 ASSERT_SINGLE_OWNER
973 RETURN_IF_ABANDONED
974 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400975 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500976
977 AutoCheckFlush acf(this->drawingManager());
978
979 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600980 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400981 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400982 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400983 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400984 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700985}
986
987///////////////////////////////////////////////////////////////////////////////
988
Brian Osman4d92b892019-03-24 00:53:23 +0000989void GrRenderTargetContext::drawAtlas(const GrClip& clip,
990 GrPaint&& paint,
991 const SkMatrix& viewMatrix,
992 int spriteCount,
993 const SkRSXform xform[],
994 const SkRect texRect[],
995 const SkColor colors[]) {
996 ASSERT_SINGLE_OWNER
997 RETURN_IF_ABANDONED
998 SkDEBUGCODE(this->validate();)
999 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1000
1001 AutoCheckFlush acf(this->drawingManager());
1002
1003 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1004 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1005 aaType, spriteCount, xform, texRect, colors);
1006 this->addDrawOp(clip, std::move(op));
1007}
1008
1009///////////////////////////////////////////////////////////////////////////////
1010
Brian Osman11052242016-10-27 14:47:55 -04001011void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001012 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001013 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001014 const SkMatrix& viewMatrix,
1015 const SkRRect& rrect,
1016 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001017 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001018 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001019 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001020 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001021
1022 const SkStrokeRec& stroke = style.strokeRec();
1023 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001024 return;
1025 }
1026
bsalomon7f0d9f32016-08-15 14:49:10 -07001027 GrNoClip noclip;
1028 const GrClip* clip = &origClip;
1029#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1030 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001031 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001032 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1033 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001034 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1035 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001036 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001037 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1038 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001039 clip = &noclip;
1040 }
1041#endif
bsalomon6663acf2016-05-10 09:14:17 -07001042 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001043
Robert Phillips72152832017-01-25 17:31:35 -05001044 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001045
Chris Dalton7d6748e2019-03-13 00:34:52 -06001046 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001047
Chris Dalton0dffbab2019-03-27 13:08:50 -06001048 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001049 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1050 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1051 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1052 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1053 // to avoid perf regressions on some platforms.
1054 assert_alive(paint);
1055 op = GrOvalOpFactory::MakeCircularRRectOp(
1056 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1057 }
1058 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001059 assert_alive(paint);
1060 op = GrFillRRectOp::Make(
1061 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1062 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001063 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001064 assert_alive(paint);
1065 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001066 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001067 }
1068 if (op) {
1069 this->addDrawOp(*clip, std::move(op));
1070 return;
robertphillipsea461502015-05-26 11:38:03 -07001071 }
robertphillipsb56f9272016-02-25 11:03:52 -08001072
Mike Klein16885072018-12-11 09:54:31 -05001073 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001074 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1075 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001076}
1077
Jim Van Verthc5903412016-11-17 15:27:09 -05001078///////////////////////////////////////////////////////////////////////////////
1079
Jim Van Verth3af1af92017-05-18 15:06:54 -04001080static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1081 SkPoint3 result;
1082 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1083 result.fZ = pt.fZ;
1084 return result;
1085}
1086
1087bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001088 const SkMatrix& viewMatrix,
1089 const SkPath& path,
1090 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001091 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001092 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001093 return true;
1094 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001095 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001096 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001097
1098 // check z plane
1099 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1100 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1101 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1102 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1103 return false;
1104 }
1105
1106 SkRRect rrect;
1107 SkRect rect;
1108 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001109 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001110 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1111 if (!isRRect &&
1112 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1113 rect.width() > SK_ScalarNearlyZero) {
1114 rrect.setOval(rect);
1115 isRRect = true;
1116 }
1117 if (!isRRect && path.isRect(&rect)) {
1118 rrect.setRect(rect);
1119 isRRect = true;
1120 }
1121
1122 if (!isRRect) {
1123 return false;
1124 }
1125
Jim Van Verthc5903412016-11-17 15:27:09 -05001126 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001127 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001128 }
1129
Robert Phillips72152832017-01-25 17:31:35 -05001130 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001131
Jim Van Verth3af1af92017-05-18 15:06:54 -04001132 // transform light
1133 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1134
1135 // 1/scale
1136 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001137 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001138 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1139 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1140
1141 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001142 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1143
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001144 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001145 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1146 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1147 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001148
1149 // Outset the shadow rrect to the border of the penumbra
1150 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1151 SkRRect ambientRRect;
1152 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1153 // If the rrect was an oval then its outset will also be one.
1154 // We set it explicitly to avoid errors.
1155 if (rrect.isOval()) {
1156 ambientRRect = SkRRect::MakeOval(outsetRect);
1157 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001158 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001159 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1160 }
1161
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001162 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001163 if (transparent) {
1164 // set a large inset to force a fill
1165 devSpaceInsetWidth = ambientRRect.width();
1166 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001167
Robert Phillips7c525e62018-06-12 10:11:12 -04001168 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1169 ambientColor,
1170 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001171 ambientRRect,
1172 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001173 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001174 if (op) {
1175 this->addDrawOp(clip, std::move(op));
1176 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001177 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001178
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001179 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001180 SkScalar devSpaceSpotBlur;
1181 SkScalar spotScale;
1182 SkVector spotOffset;
1183 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1184 devLightPos.fZ, rec.fLightRadius,
1185 &devSpaceSpotBlur, &spotScale, &spotOffset);
1186 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001187 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1188
Jim Van Verth3af1af92017-05-18 15:06:54 -04001189 // Adjust translate for the effect of the scale.
1190 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1191 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1192 // This offset is in dev space, need to transform it into source space.
1193 SkMatrix ctmInverse;
1194 if (viewMatrix.invert(&ctmInverse)) {
1195 ctmInverse.mapPoints(&spotOffset, 1);
1196 } else {
1197 // Since the matrix is a similarity, this should never happen, but just in case...
1198 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1199 SkASSERT(false);
1200 }
1201
1202 // Compute the transformed shadow rrect
1203 SkRRect spotShadowRRect;
1204 SkMatrix shadowTransform;
1205 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1206 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001207 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001208
1209 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001210 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001211 SkScalar insetWidth = blurOutset;
1212 if (transparent) {
1213 // If transparent, just do a fill
1214 insetWidth += spotShadowRRect.width();
1215 } else {
1216 // For shadows, instead of using a stroke we specify an inset from the penumbra
1217 // border. We want to extend this inset area so that it meets up with the caster
1218 // geometry. The inset geometry will by default already be inset by the blur width.
1219 //
1220 // We compare the min and max corners inset by the radius between the original
1221 // rrect and the shadow rrect. The distance between the two plus the difference
1222 // between the scaled radius and the original radius gives the distance from the
1223 // transformed shadow shape to the original shape in that corner. The max
1224 // of these gives the maximum distance we need to cover.
1225 //
1226 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1227 // that to get the full insetWidth.
1228 SkScalar maxOffset;
1229 if (rrect.isRect()) {
1230 // Manhattan distance works better for rects
1231 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1232 rrect.rect().fLeft),
1233 SkTAbs(spotShadowRRect.rect().fTop -
1234 rrect.rect().fTop)),
1235 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1236 rrect.rect().fRight),
1237 SkTAbs(spotShadowRRect.rect().fBottom -
1238 rrect.rect().fBottom)));
1239 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001240 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001241 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1242 rrect.rect().fLeft + dr,
1243 spotShadowRRect.rect().fTop -
1244 rrect.rect().fTop + dr);
1245 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1246 rrect.rect().fRight - dr,
1247 spotShadowRRect.rect().fBottom -
1248 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001249 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1250 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001251 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001252 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001253 }
1254
1255 // Outset the shadow rrect to the border of the penumbra
1256 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1257 if (spotShadowRRect.isOval()) {
1258 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1259 } else {
1260 SkScalar outsetRad = spotRadius + blurOutset;
1261 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1262 }
1263
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001264 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001265
Robert Phillips7c525e62018-06-12 10:11:12 -04001266 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1267 spotColor,
1268 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001269 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001270 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001271 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001272 if (op) {
1273 this->addDrawOp(clip, std::move(op));
1274 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001275 }
1276
1277 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001278}
1279
1280///////////////////////////////////////////////////////////////////////////////
1281
Brian Osman11052242016-10-27 14:47:55 -04001282bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001283 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001284 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001285 const SkMatrix& viewMatrix,
1286 const SkRRect& origOuter,
1287 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001288 SkASSERT(!origInner.isEmpty());
1289 SkASSERT(!origOuter.isEmpty());
1290
Brian Salomon65749212017-12-01 16:01:47 -05001291 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1292
Chris Dalton7d6748e2019-03-13 00:34:52 -06001293 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001294
1295 if (GrAAType::kMSAA == aaType) {
1296 return false;
1297 }
1298
Greg Daniel2655ede2019-04-10 00:49:28 +00001299 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1300 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001301 auto outerR = outer->width() / 2.f;
1302 auto innerR = inner->width() / 2.f;
1303 auto cx = outer->getBounds().fLeft + outerR;
1304 auto cy = outer->getBounds().fTop + outerR;
1305 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1306 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1307 auto avgR = (innerR + outerR) / 2.f;
1308 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1309 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1310 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001311 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001312 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001313 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001314 if (op) {
1315 this->addDrawOp(clip, std::move(op));
1316 return true;
1317 }
Mike Klein16885072018-12-11 09:54:31 -05001318 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001319 }
1320 }
1321
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001322 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001323 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001324 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1325 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001326 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001327 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1328 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001329 }
robertphillips00095892016-02-29 13:50:40 -08001330
robertphillips00095892016-02-29 13:50:40 -08001331 SkMatrix inverseVM;
1332 if (!viewMatrix.isIdentity()) {
1333 if (!origInner.transform(viewMatrix, inner.writable())) {
1334 return false;
1335 }
1336 if (!origOuter.transform(viewMatrix, outer.writable())) {
1337 return false;
1338 }
1339 if (!viewMatrix.invert(&inverseVM)) {
1340 return false;
1341 }
1342 } else {
1343 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001344 }
robertphillips00095892016-02-29 13:50:40 -08001345
Ethan Nicholaseace9352018-10-15 20:09:54 +00001346 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001347 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001348 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001349 if (!innerEffect) {
1350 return false;
1351 }
1352
Ethan Nicholaseace9352018-10-15 20:09:54 +00001353 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001354 if (!outerEffect) {
1355 return false;
1356 }
1357
Brian Salomon82f44312017-01-11 13:42:54 -05001358 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1359 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001360
1361 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001362 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001363 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1364 }
halcanary9d524f22016-03-29 09:03:52 -07001365
Brian Salomon82f44312017-01-11 13:42:54 -05001366 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1367 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001368 return true;
1369}
1370
Brian Osman11052242016-10-27 14:47:55 -04001371void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001372 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001373 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001374 const SkMatrix& viewMatrix,
1375 const SkRRect& outer,
1376 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001377 ASSERT_SINGLE_OWNER
1378 RETURN_IF_ABANDONED
1379 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001380 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001381
1382 SkASSERT(!outer.isEmpty());
1383 SkASSERT(!inner.isEmpty());
1384
Robert Phillips72152832017-01-25 17:31:35 -05001385 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001386
Brian Salomon82f44312017-01-11 13:42:54 -05001387 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001388 return;
1389 }
Mike Klein16885072018-12-11 09:54:31 -05001390 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001391
1392 SkPath path;
1393 path.setIsVolatile(true);
1394 path.addRRect(inner);
1395 path.addRRect(outer);
1396 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001397 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001398}
1399
robertphillipsea461502015-05-26 11:38:03 -07001400///////////////////////////////////////////////////////////////////////////////
1401
Brian Osman11052242016-10-27 14:47:55 -04001402void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001403 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001404 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001405 const SkMatrix& viewMatrix,
1406 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001407 const GrStyle& style,
1408 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001409 ASSERT_SINGLE_OWNER
1410 RETURN_IF_ABANDONED
1411 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001412 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001413
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001414 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001415 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001416 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001417 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001418 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1419 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001420 aa = GrAA::kNo;
1421 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001422 }
msarettcc319b92016-08-25 18:07:18 -07001423 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001424 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001425 SkPath path;
1426 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001427 path.setIsVolatile(true);
1428
Brian Salomon82f44312017-01-11 13:42:54 -05001429 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001430 }
1431
Chris Dalton7d6748e2019-03-13 00:34:52 -06001432 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001433 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1434 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001435 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001436}
1437
Brian Osman11052242016-10-27 14:47:55 -04001438void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001439 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001440 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001441 const SkMatrix& viewMatrix,
1442 const SkRect& oval,
1443 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001444 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001445 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001446 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001447 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001448
Robert Phillips7484d202018-07-03 09:09:08 -04001449 const SkStrokeRec& stroke = style.strokeRec();
1450
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001451 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001452 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1453 return;
1454 }
1455
1456 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001457 return;
robertphillipsea461502015-05-26 11:38:03 -07001458 }
1459
Robert Phillips72152832017-01-25 17:31:35 -05001460 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001461
Chris Dalton7d6748e2019-03-13 00:34:52 -06001462 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001463
1464 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001465 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1466 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001467 // We don't draw true circles as round rects in coverage mode, because it can
1468 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1469 assert_alive(paint);
1470 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1471 this->caps()->shaderCaps());
1472 }
1473 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001474 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1475 // the arc equation. This same special geometry and fragment branch also turn out to be a
1476 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1477 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1478 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001479 assert_alive(paint);
1480 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1481 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001482 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001483 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001484 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001485 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1486 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001487 }
1488 if (op) {
1489 this->addDrawOp(clip, std::move(op));
1490 return;
robertphillipsea461502015-05-26 11:38:03 -07001491 }
robertphillipsb56f9272016-02-25 11:03:52 -08001492
Mike Klein16885072018-12-11 09:54:31 -05001493 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001494 this->drawShapeUsingPathRenderer(
1495 clip, std::move(paint), aa, viewMatrix,
Mike Reede1af4442019-09-13 15:58:25 -04001496 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001497}
1498
Brian Osman11052242016-10-27 14:47:55 -04001499void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001500 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001501 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001502 const SkMatrix& viewMatrix,
1503 const SkRect& oval,
1504 SkScalar startAngle,
1505 SkScalar sweepAngle,
1506 bool useCenter,
1507 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001508 ASSERT_SINGLE_OWNER
1509 RETURN_IF_ABANDONED
1510 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001511 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001512
1513 AutoCheckFlush acf(this->drawingManager());
1514
Chris Dalton7d6748e2019-03-13 00:34:52 -06001515 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001516 if (GrAAType::kCoverage == aaType) {
1517 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1518 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1519 std::move(paint),
1520 viewMatrix,
1521 oval,
1522 startAngle,
1523 sweepAngle,
1524 useCenter,
1525 style,
1526 shaderCaps);
1527 if (op) {
1528 this->addDrawOp(clip, std::move(op));
1529 return;
1530 }
1531 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001532 }
Brian Salomone4949402018-04-26 15:22:04 -04001533 this->drawShapeUsingPathRenderer(
1534 clip, std::move(paint), aa, viewMatrix,
1535 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001536}
1537
Brian Osman11052242016-10-27 14:47:55 -04001538void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001539 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001540 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001541 sk_sp<GrTextureProxy> image,
1542 sk_sp<GrColorSpaceXform> csxf,
1543 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001544 std::unique_ptr<SkLatticeIter> iter,
1545 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001546 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001547 RETURN_IF_ABANDONED
1548 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001549 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001550
Robert Phillips72152832017-01-25 17:31:35 -05001551 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001552
Brian Salomon2a943df2018-05-04 13:43:19 -04001553 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001554 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1555 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001556 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001557}
1558
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001559void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1560 const SkRect& bounds) {
1561 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1562 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001563 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001564}
1565
Brian Salomon031b0ba2019-05-23 11:05:26 -04001566void GrRenderTargetContext::asyncRescaleAndReadPixels(
1567 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1568 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1569 auto direct = fContext->priv().asDirectContext();
1570 if (!direct) {
1571 callback(context, nullptr, 0);
1572 return;
1573 }
1574 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1575 callback(context, nullptr, 0);
1576 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001577 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001578 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1579 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001580 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001581 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001582 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001583 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001584 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1585 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001586 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001587 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001588 backendFormatOfFinalContext, dstCT);
1589 // Fail if we can't read from the source surface's color type.
1590 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001591 callback(context, nullptr, 0);
1592 return;
1593 }
Brian Salomon624f9062019-07-02 14:23:00 -04001594 // Fail if read color type does not have all of dstCT's color channels and those missing color
1595 // channels are in the src.
1596 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1597 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1598 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1599 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001600 callback(context, nullptr, 0);
1601 return;
1602 }
1603
Brian Salomonbf6b9792019-08-21 09:38:10 -04001604 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001605 int x = srcRect.fLeft;
1606 int y = srcRect.fTop;
1607 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001608 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1609 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001610 callback(context, nullptr, 0);
1611 return;
1612 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001613 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1614 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001615 x = y = 0;
1616 } else {
1617 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001618 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1619 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001620 info.colorSpace(), info.alphaType());
1621 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1622 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001623 // We flip or color convert by drawing and we don't currently support drawing to
1624 // kPremul.
1625 if (info.alphaType() == kUnpremul_SkAlphaType) {
1626 callback(context, nullptr, 0);
1627 return;
1628 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001629 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001630 SkRect srcRectToDraw = SkRect::Make(srcRect);
1631 // If the src is not texturable first try to make a copy to a texture.
1632 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001633 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1634 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1635 SkBudgeted::kNo);
1636 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001637 callback(context, nullptr, 0);
1638 return;
1639 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001640 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1641 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001642 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001643 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1644 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1645 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001646 if (!tempRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001647 callback(context, nullptr, 0);
1648 return;
1649 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001650 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1651 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1652 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1653 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1654 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001655 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001656 }
1657 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001658 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001659 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1660 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001661}
1662
Brian Salomon024bd002019-06-11 11:38:16 -04001663void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1664 ReadPixelsCallback callback,
1665 ReadPixelsContext context) {
1666 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1667 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1668
1669 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1670
1671 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001672 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001673 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1674 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001675 this->colorSpaceInfo().refColorSpace());
1676 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001677 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001678 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001679 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001680 callback(context, pm.addr(), pm.rowBytes());
1681 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001682 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001683
Brian Salomonab32f652019-05-10 14:24:50 -04001684 struct FinishContext {
1685 ReadPixelsCallback* fClientCallback;
1686 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001687 int fW, fH;
1688 SkColorType fColorType;
1689 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001690 };
1691 // Assumption is that the caller would like to flush. We could take a parameter or require an
1692 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1693 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001694 auto* finishContext = new FinishContext{callback, context, rect.width(),
1695 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001696 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001697 const auto* context = reinterpret_cast<const FinishContext*>(c);
1698 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001699 if (!data) {
1700 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1701 delete context;
1702 return;
1703 }
Brian Salomon4d036892019-07-02 15:10:58 -04001704 std::unique_ptr<char[]> tmp;
1705 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001706 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001707 tmp.reset(new char[rowBytes * context->fH]);
1708 context->fTransferResult.fPixelConverter(tmp.get(), data);
1709 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001710 }
Brian Salomon4d036892019-07-02 15:10:58 -04001711 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001712 delete context;
1713 };
1714 GrFlushInfo flushInfo;
1715 flushInfo.fFinishedContext = finishContext;
1716 flushInfo.fFinishedProc = finishCallback;
1717 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1718}
1719
1720void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1721 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1722 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1723 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1724 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1725 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1726 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1727 auto direct = fContext->priv().asDirectContext();
1728 if (!direct) {
1729 callback(context, nullptr, nullptr);
1730 return;
1731 }
1732 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1733 callback(context, nullptr, nullptr);
1734 return;
1735 }
1736 if (dstW & 0x1) {
1737 return;
1738 }
1739 int x = srcRect.fLeft;
1740 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001741 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon024bd002019-06-11 11:38:16 -04001742 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1743 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001744 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001745 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1746 dstColorSpace);
1747 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001748 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1749 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001750 callback(context, nullptr, nullptr);
1751 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001752 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001753 SkASSERT(SkColorSpace::Equals(tempRTC->colorSpaceInfo().colorSpace(), info.colorSpace()));
1754 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001755 x = y = 0;
1756 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001757 // We assume the caller wants kPremul. There is no way to indicate a preference.
1758 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1759 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1760 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001761 if (xform) {
1762 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1763 // TODO: Do something if the input is not a texture already.
1764 if (!texProxy) {
1765 callback(context, nullptr, nullptr);
1766 return;
1767 }
Brian Salomon024bd002019-06-11 11:38:16 -04001768 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001769 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001770 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
1771 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001772 if (!tempRTC) {
Brian Salomon024bd002019-06-11 11:38:16 -04001773 callback(context, nullptr, nullptr);
1774 return;
1775 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001776 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1777 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1778 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1779 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1780 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001781 x = y = 0;
1782 }
1783 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001784 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001785 // TODO: Do something if the input is not a texture already.
1786 if (!srcProxy) {
1787 callback(context, nullptr, nullptr);
1788 return;
1789 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04001790 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1791 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
1792 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1793 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1794 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1795 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1796 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
1797 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
1798 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001799 if (!yRTC || !uRTC || !vRTC) {
1800 callback(context, nullptr, nullptr);
1801 return;
1802 }
1803
1804 static constexpr float kRec601M[] {
1805 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
1806 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
1807 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
1808 };
1809 static constexpr float kRec709M[] {
1810 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
1811 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
1812 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
1813 };
1814 static constexpr float kJpegM[] {
1815 0.299f , 0.587f , 0.114f , 0.f / 255, // y
1816 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
1817 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
1818 };
1819 static constexpr float kIM[] {
1820 1.f, 0.f, 0.f, 0.f,
1821 0.f, 1.f, 0.f, 0.f,
1822 0.f, 0.f, 1.f, 0.f,
1823 };
1824 const float* baseM = kIM;
1825 switch (yuvColorSpace) {
1826 case kRec601_SkYUVColorSpace:
1827 baseM = kRec601M;
1828 break;
1829 case kRec709_SkYUVColorSpace:
1830 baseM = kRec709M;
1831 break;
1832 case kJPEG_SkYUVColorSpace:
1833 baseM = kJpegM;
1834 break;
1835 case kIdentity_SkYUVColorSpace:
1836 baseM = kIM;
1837 break;
1838 }
1839 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1840
1841 auto texMatrix = SkMatrix::MakeTrans(x, y);
1842
Michael Ludwig61328202019-06-19 14:48:58 +00001843 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
1844 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
1845
Brian Salomon024bd002019-06-11 11:38:16 -04001846 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1847 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001848 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001849 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
1850 GrPaint yPaint;
1851 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
1852 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1853 yPaint.addColorFragmentProcessor(std::move(yFP));
1854 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001855 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1856 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001857 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1858 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1859 if (!yTransfer.fTransferBuffer) {
1860 callback(context, nullptr, nullptr);
1861 return;
1862 }
1863
1864 texMatrix.preScale(2.f, 2.f);
1865 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1866 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001867 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001868 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
1869 GrPaint uPaint;
1870 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1871 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1872 uPaint.addColorFragmentProcessor(std::move(uFP));
1873 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001874 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1875 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001876 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1877 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1878 if (!uTransfer.fTransferBuffer) {
1879 callback(context, nullptr, nullptr);
1880 return;
1881 }
1882
1883 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1884 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001885 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04001886 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
1887 GrPaint vPaint;
1888 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
1889 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1890 vPaint.addColorFragmentProcessor(std::move(vFP));
1891 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001892 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1893 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001894 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1895 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1896 if (!vTransfer.fTransferBuffer) {
1897 callback(context, nullptr, nullptr);
1898 return;
1899 }
1900
1901 struct FinishContext {
1902 ReadPixelsCallbackYUV420* fClientCallback;
1903 ReadPixelsContext fClientContext;
1904 int fW, fH;
1905 PixelTransferResult fYTransfer;
1906 PixelTransferResult fUTransfer;
1907 PixelTransferResult fVTransfer;
1908 };
1909 // Assumption is that the caller would like to flush. We could take a parameter or require an
1910 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1911 // callback to GrGpu until after the next flush that flushes our op list, though.
1912 auto* finishContext = new FinishContext{callback,
1913 context,
1914 dstW,
1915 dstH,
1916 std::move(yTransfer),
1917 std::move(uTransfer),
1918 std::move(vTransfer)};
1919 auto finishCallback = [](GrGpuFinishedContext c) {
1920 const auto* context = reinterpret_cast<const FinishContext*>(c);
1921 const void* y = context->fYTransfer.fTransferBuffer->map();
1922 const void* u = context->fUTransfer.fTransferBuffer->map();
1923 const void* v = context->fVTransfer.fTransferBuffer->map();
1924 if (!y || !u || !v) {
1925 if (y) {
1926 context->fYTransfer.fTransferBuffer->unmap();
1927 }
1928 if (u) {
1929 context->fUTransfer.fTransferBuffer->unmap();
1930 }
1931 if (v) {
1932 context->fVTransfer.fTransferBuffer->unmap();
1933 }
1934 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1935 delete context;
1936 return;
1937 }
1938 size_t w = SkToSizeT(context->fW);
1939 size_t h = SkToSizeT(context->fH);
1940 std::unique_ptr<uint8_t[]> yTemp;
1941 if (context->fYTransfer.fPixelConverter) {
1942 yTemp.reset(new uint8_t[w * h]);
1943 context->fYTransfer.fPixelConverter(yTemp.get(), y);
1944 y = yTemp.get();
1945 }
1946 std::unique_ptr<uint8_t[]> uTemp;
1947 if (context->fUTransfer.fPixelConverter) {
1948 uTemp.reset(new uint8_t[w / 2 * h / 2]);
1949 context->fUTransfer.fPixelConverter(uTemp.get(), u);
1950 u = uTemp.get();
1951 }
1952 std::unique_ptr<uint8_t[]> vTemp;
1953 if (context->fVTransfer.fPixelConverter) {
1954 vTemp.reset(new uint8_t[w / 2 * h / 2]);
1955 context->fVTransfer.fPixelConverter(vTemp.get(), v);
1956 v = vTemp.get();
1957 }
1958 const void* data[] = {y, u, v};
1959 size_t rowBytes[] = {w, w / 2, w / 2};
1960 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
1961 context->fYTransfer.fTransferBuffer->unmap();
1962 context->fUTransfer.fTransferBuffer->unmap();
1963 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04001964 delete context;
1965 };
1966 GrFlushInfo flushInfo;
1967 flushInfo.fFinishedContext = finishContext;
1968 flushInfo.fFinishedProc = finishCallback;
1969 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001970}
1971
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001972GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1973 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001974 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001975 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001976 return GrSemaphoresSubmitted::kNo;
1977 }
robertphillips8c523e02016-07-26 07:41:00 -07001978 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001979 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001980
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001981 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001982}
1983
Greg Danielc64ee462017-06-15 16:59:49 -04001984bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001985 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001986 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001987 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001988 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001989 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001990
1991 AutoCheckFlush acf(this->drawingManager());
1992
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001993 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001994 return false;
1995 }
1996
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001997 auto direct = fContext->priv().asDirectContext();
1998 if (!direct) {
1999 return false;
2000 }
2001
2002 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002003
Greg Danielc30f1a92019-09-06 15:28:58 -04002004 std::unique_ptr<sk_sp<GrSemaphore>[]> grSemaphores(new sk_sp<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04002005 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04002006 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002007 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2008 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002009 }
Greg Danielc30f1a92019-09-06 15:28:58 -04002010 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
2011 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04002012 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002013}
joshualitt33a5fce2015-11-18 13:28:51 -08002014
Robert Phillips65a88fa2017-08-08 08:36:22 -04002015void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04002016 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06002017 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04002018}
2019
Brian Osman11052242016-10-27 14:47:55 -04002020void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002021 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002022 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002023 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002024 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002025 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002026 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002027 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002028 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002029 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2030
Brian Salomon40b77a62017-12-22 11:25:52 -05002031 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002032
Robert Phillips27927a52018-08-20 13:18:12 -04002033 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002034}
2035
2036void GrRenderTargetContext::drawShape(const GrClip& clip,
2037 GrPaint&& paint,
2038 GrAA aa,
2039 const SkMatrix& viewMatrix,
2040 const GrShape& shape) {
2041 ASSERT_SINGLE_OWNER
2042 RETURN_IF_ABANDONED
2043 SkDEBUGCODE(this->validate();)
2044 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2045
Brian Salomon2fad74a2017-12-20 13:28:55 -05002046 if (shape.isEmpty()) {
2047 if (shape.inverseFilled()) {
2048 this->drawPaint(clip, std::move(paint), viewMatrix);
2049 }
2050 return;
robertphillipsea461502015-05-26 11:38:03 -07002051 }
2052
Robert Phillips72152832017-01-25 17:31:35 -05002053 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002054
Brian Salomon2fad74a2017-12-20 13:28:55 -05002055 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002056 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002057 SkRRect rrect;
2058 // We can ignore the starting point and direction since there is no path effect.
2059 bool inverted;
2060 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2061 if (rrect.isRect()) {
2062 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2063 &shape.style());
2064 return;
2065 } else if (rrect.isOval()) {
2066 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002067 return;
2068 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002069 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2070 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002071 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2072 viewMatrix.rectStaysRect()) {
2073 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2074 // the matrix to all the points individually rather than just to the rect
2075 SkRect rects[2];
2076 if (shape.asNestedRects(rects)) {
2077 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002078 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002079 fContext, std::move(paint), viewMatrix, rects);
2080 if (op) {
2081 this->addDrawOp(clip, std::move(op));
2082 }
2083 // Returning here indicates that there is nothing to draw in this case.
2084 return;
2085 }
robertphillipsea461502015-05-26 11:38:03 -07002086 }
2087 }
robertphillips4bc31812016-03-01 12:22:49 -08002088
Brian Salomon2fad74a2017-12-20 13:28:55 -05002089 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002090}
2091
Chris Daltonbbfd5162017-11-07 13:35:22 -07002092bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002093 const GrUserStencilSettings* ss,
2094 SkRegion::Op op,
2095 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002096 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002097 const SkMatrix& viewMatrix,
2098 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002099 ASSERT_SINGLE_OWNER_PRIV
2100 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002101 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002102 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2103 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002104
2105 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002106 GrPaint paint;
2107 paint.setCoverageSetOpXPFactory(op, invert);
2108 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2109 SkRect::MakeIWH(fRenderTargetContext->width(),
2110 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002111 return true;
2112 }
2113
Robert Phillips72152832017-01-25 17:31:35 -05002114 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002115
2116 // An Assumption here is that path renderer would use some form of tweaking
2117 // the src color (either the input alpha or in the frag shader) to implement
2118 // aa. If we have some future driver-mojo path AA that can do the right
2119 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002120 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002121 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002122
Chris Daltondb91c6e2017-09-08 16:25:08 -06002123 SkIRect clipConservativeBounds;
2124 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2125 &clipConservativeBounds, nullptr);
2126
bsalomon8acedde2016-06-24 10:42:16 -07002127 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002128 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002129 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002130 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002131 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002132 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002133 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002134 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002135 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2136 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002137 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002138
2139 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002140 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002141 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002142 if (!pr) {
2143 return false;
2144 }
2145
2146 GrPaint paint;
2147 paint.setCoverageSetOpXPFactory(op, invert);
2148
Brian Salomonf3569f02017-10-24 12:52:33 -04002149 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2150 std::move(paint),
2151 ss,
2152 fRenderTargetContext,
2153 &clip,
2154 &clipConservativeBounds,
2155 &viewMatrix,
2156 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002157 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002158 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002159 pr->drawPath(args);
2160 return true;
2161}
2162
Brian Osman11052242016-10-27 14:47:55 -04002163SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002164 ASSERT_SINGLE_OWNER_PRIV
2165
Robert Phillips6a6de562019-02-15 15:19:15 -05002166 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002167 return SkBudgeted::kNo;
2168 }
2169
Brian Osman11052242016-10-27 14:47:55 -04002170 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002171
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002172 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002173}
2174
Brian Salomon2fad74a2017-12-20 13:28:55 -05002175void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2176 GrPaint&& paint,
2177 GrAA aa,
2178 const SkMatrix& viewMatrix,
2179 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002180 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002181 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002182 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2183
Jim Van Verthf86073a2018-10-02 13:05:38 -04002184 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2185 return;
2186 }
2187
Chris Daltondb91c6e2017-09-08 16:25:08 -06002188 SkIRect clipConservativeBounds;
2189 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2190
Brian Salomon2fad74a2017-12-20 13:28:55 -05002191 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002192 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002193
robertphillips68737822015-10-29 12:12:21 -07002194 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002195 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002196 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002197 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002198 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002199 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002200 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002201 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002202
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002203 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002204 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002205 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002206 return;
2207 }
2208
Chris Dalton6ce447a2019-06-23 18:07:38 -06002209 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002210
2211 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2212 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2213 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2214
Brian Salomon2fad74a2017-12-20 13:28:55 -05002215 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002216 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002217 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2218 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002219 return;
2220 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002221 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002222 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002223 }
2224 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002225 if (canDrawArgs.fShape->style().applies()) {
2226 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2227 styleScale);
2228 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002229 return;
2230 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002231 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002232 // This time, allow SW renderer
2233 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2234 } else {
2235 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002236 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002237 }
robertphillipsea461502015-05-26 11:38:03 -07002238
bsalomon8acedde2016-06-24 10:42:16 -07002239 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002240#ifdef SK_DEBUG
2241 SkDebugf("Unable to find path renderer compatible with path.\n");
2242#endif
2243 return;
2244 }
2245
Robert Phillips256c37b2017-03-01 14:32:46 -05002246 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002247 std::move(paint),
2248 &GrUserStencilSettings::kUnused,
2249 this,
2250 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002251 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002252 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002253 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002254 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002255 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002256 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002257}
2258
Brian Salomon467921e2017-03-06 16:17:12 -05002259static void op_bounds(SkRect* bounds, const GrOp* op) {
2260 *bounds = op->bounds();
2261 if (op->hasZeroArea()) {
2262 if (op->hasAABloat()) {
2263 bounds->outset(0.5f, 0.5f);
2264 } else {
2265 // We don't know which way the particular GPU will snap lines or points at integer
2266 // coords. So we ensure that the bounds is large enough for either snap.
2267 SkRect before = *bounds;
2268 bounds->roundOut(bounds);
2269 if (bounds->fLeft == before.fLeft) {
2270 bounds->fLeft -= 1;
2271 }
2272 if (bounds->fTop == before.fTop) {
2273 bounds->fTop -= 1;
2274 }
2275 if (bounds->fRight == before.fRight) {
2276 bounds->fRight += 1;
2277 }
2278 if (bounds->fBottom == before.fBottom) {
2279 bounds->fBottom += 1;
2280 }
2281 }
2282 }
2283}
2284
Chris Dalton08755122019-08-05 16:13:47 -06002285void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002286 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002287 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2288}
2289
Brian Salomon348a0372018-10-31 10:42:18 -04002290void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2291 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002292 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002293 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002294 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002295 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002296 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002297 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002298 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002299 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002300
Brian Salomon467921e2017-03-06 16:17:12 -05002301 // Setup clip
2302 SkRect bounds;
2303 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002304 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002305 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002306 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2307 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2308
2309 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002310 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002311 }
2312
2313 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002314 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002315 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002316 }
2317
Chris Daltoneffee202019-07-01 22:28:03 -06002318 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002319
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002320 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002321 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2322 // here as well.
2323 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2324#ifdef SK_DEBUG
2325 if (hasMixedSampledCoverage) {
2326 SkASSERT(usesStencil);
2327 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2328 }
2329#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002330 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002331 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2332
2333 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002334 if (analysis.requiresDstTexture()) {
Chris Dalton2243c7b2019-08-21 14:46:35 -06002335 if (!this->setupDstProxy(clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002336 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002337 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002338 }
2339 }
2340
2341 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002342 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002343 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002344 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002345 }
Greg Danielf41b2bd2019-08-22 16:19:24 -04002346 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy,
2347 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002348}
2349
Chris Dalton2243c7b2019-08-21 14:46:35 -06002350bool GrRenderTargetContext::setupDstProxy(const GrClip& clip, const GrOp& op,
2351 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002352 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2353 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2354 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002355 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002356 return false;
2357 }
2358
Chris Dalton2243c7b2019-08-21 14:46:35 -06002359 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
2360 if (GrTextureProxy* texProxy = fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002361 // The render target is a texture, so we can read from it directly in the shader. The XP
2362 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002363 dstProxy->setProxy(sk_ref_sp(texProxy));
2364 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002365 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002366 }
2367 }
2368
Chris Dalton2243c7b2019-08-21 14:46:35 -06002369 SkIRect copyRect = SkIRect::MakeWH(fRenderTargetProxy->width(), fRenderTargetProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002370
Eric Karl74480882017-04-03 14:49:05 -07002371 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002372 clip.getConservativeBounds(
2373 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002374 SkRect opBounds = op.bounds();
2375 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2376 // 0.5 pixels.
2377 if (op.hasAABloat() || op.hasZeroArea()) {
2378 opBounds.outset(0.5f, 0.5f);
2379 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2380 // performance we may ignore the clip when the draw is entirely inside the clip is float
2381 // space but will hit pixels just outside the clip when actually rasterizing.
2382 clippedRect.outset(1, 1);
Chris Dalton2243c7b2019-08-21 14:46:35 -06002383 clippedRect.intersect(SkIRect::MakeWH(
2384 fRenderTargetProxy->width(), fRenderTargetProxy->height()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002385 }
2386 SkIRect opIBounds;
2387 opBounds.roundOut(&opIBounds);
2388 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002389#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002390 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002391#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002392 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002393 }
2394
2395 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2396 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002397 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Chris Dalton2243c7b2019-08-21 14:46:35 -06002398 fRenderTargetProxy.get(), this->colorSpaceInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002399
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002400 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002401 copyRect = clippedRect;
2402 }
Brian Salomon467921e2017-03-06 16:17:12 -05002403
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002404 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002405 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002406 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002407 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002408 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002409 } else {
Eric Karl74480882017-04-03 14:49:05 -07002410 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002411 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002412 }
Chris Dalton2243c7b2019-08-21 14:46:35 -06002413 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(
2414 fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect, fit, SkBudgeted::kYes,
2415 restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002416 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002417
Greg Daniel46cfbc62019-06-07 11:43:30 -04002418 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002419 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002420 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002421}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002422
2423bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2424 const SkIPoint& dstPoint) {
2425 SkIRect clippedSrcRect;
2426 SkIPoint clippedDstPoint;
2427 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2428 &clippedSrcRect, &clippedDstPoint)) {
2429 return false;
2430 }
2431
2432 GrPaint paint;
2433 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2434 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2435 SkMatrix::I());
2436 if (!fp) {
2437 return false;
2438 }
2439 paint.addColorFragmentProcessor(std::move(fp));
2440
2441 this->fillRectToRect(
2442 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2443 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2444 clippedSrcRect.height()),
2445 SkRect::Make(clippedSrcRect));
2446 return true;
2447}
2448