blob: 911ed0c49bebe63665d8b0a9f6df9e29e212db68 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Salomonf30b1c12019-06-20 12:25:02 -04008#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkDrawable.h"
10#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
13#include "include/private/SkShadowFlags.h"
14#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040015#include "src/core/SkAutoPixmapStorage.h"
16#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkDrawShadowInfo.h"
18#include "src/core/SkGlyphRunPainter.h"
19#include "src/core/SkLatticeIter.h"
20#include "src/core/SkMatrixPriv.h"
21#include "src/core/SkRRectPriv.h"
22#include "src/core/SkSurfacePriv.h"
23#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrBlurUtils.h"
26#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040029#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrDrawingManager.h"
31#include "src/gpu/GrFixedClip.h"
32#include "src/gpu/GrGpuResourcePriv.h"
33#include "src/gpu/GrMemoryPool.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040034#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrRenderTargetContextPriv.h"
38#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrStencilAttachment.h"
40#include "src/gpu/GrStyle.h"
41#include "src/gpu/GrTracing.h"
42#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040043#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/effects/GrRRectEffect.h"
45#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040046#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040047#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000048#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040049#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/ops/GrAtlasTextOp.h"
51#include "src/gpu/ops/GrClearOp.h"
52#include "src/gpu/ops/GrClearStencilClipOp.h"
53#include "src/gpu/ops/GrDebugMarkerOp.h"
54#include "src/gpu/ops/GrDrawAtlasOp.h"
55#include "src/gpu/ops/GrDrawOp.h"
56#include "src/gpu/ops/GrDrawVerticesOp.h"
57#include "src/gpu/ops/GrDrawableOp.h"
58#include "src/gpu/ops/GrFillRRectOp.h"
59#include "src/gpu/ops/GrFillRectOp.h"
60#include "src/gpu/ops/GrLatticeOp.h"
61#include "src/gpu/ops/GrOp.h"
62#include "src/gpu/ops/GrOvalOpFactory.h"
63#include "src/gpu/ops/GrRegionOp.h"
64#include "src/gpu/ops/GrSemaphoreOp.h"
65#include "src/gpu/ops/GrShadowRRectOp.h"
66#include "src/gpu/ops/GrStencilPathOp.h"
67#include "src/gpu/ops/GrStrokeRectOp.h"
68#include "src/gpu/ops/GrTextureOp.h"
Brian Salomonab32f652019-05-10 14:24:50 -040069#include "src/gpu/ops/GrTransferFromOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050070#include "src/gpu/text/GrTextContext.h"
71#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040072
Herb Derbyc1b482c2018-08-09 15:02:27 -040073class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040074public:
75 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040076 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Robert Phillips7e90be92019-02-15 12:22:59 -050077 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040078 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040079 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040080
Robert Phillips7c525e62018-06-12 10:11:12 -040081 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040082 fRenderTargetContext->addDrawOp(clip, std::move(op));
83 }
84
Robert Phillips46a13382018-08-23 13:53:01 -040085 void drawShape(const GrClip& clip, const SkPaint& paint,
86 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040087 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
88 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040089 }
90
91 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040092 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050093 auto context = fRenderTargetContext->fContext;
Brian Salomonf18b1d82017-10-27 11:30:49 -040094 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
95 if (kARGB_GrMaskFormat == maskFormat) {
96 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
97 } else {
98 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
99 }
100 }
101
Robert Phillips69893702019-02-22 11:16:30 -0500102 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400103 return fRenderTargetContext->fContext;
104 }
105
Herb Derby65956872018-08-21 16:55:04 -0400106 SkGlyphRunListPainter* glyphPainter() override {
107 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400108 }
109
Brian Salomonf18b1d82017-10-27 11:30:49 -0400110private:
111 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400112 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400113
Brian Salomonf18b1d82017-10-27 11:30:49 -0400114};
joshualittbc907352016-01-13 06:45:40 -0800115
Robert Phillips72152832017-01-25 17:31:35 -0500116#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800117#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400118 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800119#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400120 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500121#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
122#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
123#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
124#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
125#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700126
Brian Salomone225b562017-06-14 13:00:03 -0400127//////////////////////////////////////////////////////////////////////////////
128
robertphillipsea461502015-05-26 11:38:03 -0700129class AutoCheckFlush {
130public:
halcanary9d524f22016-03-29 09:03:52 -0700131 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700132 SkASSERT(fDrawingManager);
133 }
bsalomonb77a9072016-09-07 10:02:04 -0700134 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700135
136private:
robertphillips77a2e522015-10-17 07:43:27 -0700137 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700138};
139
Robert Phillipsf2361d22016-10-25 14:20:06 -0400140// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400141// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400142// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400143// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -0500144GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400145 sk_sp<GrRenderTargetProxy> rtp,
Brian Salomond6287472019-06-24 15:50:07 -0400146 GrColorType colorType,
Brian Osman11052242016-10-27 14:47:55 -0400147 sk_sp<SkColorSpace> colorSpace,
148 const SkSurfaceProps* surfaceProps,
Robert Phillips941d1442017-06-14 16:37:02 -0400149 bool managedOpList)
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400150 : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace))
Brian Salomonf3569f02017-10-24 12:52:33 -0400151 , fRenderTargetProxy(std::move(rtp))
152 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400153 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
154 , fManagedOpList(managedOpList) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400155 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700156 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700157}
158
robertphillips2e1e51f2015-10-15 08:01:48 -0700159#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400160void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400161 SkASSERT(fRenderTargetProxy);
162 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700163
Robert Phillipsf2361d22016-10-25 14:20:06 -0400164 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400165 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700166 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700167}
168#endif
169
Brian Osman11052242016-10-27 14:47:55 -0400170GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800171 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700172}
173
Chris Dalton7d6748e2019-03-13 00:34:52 -0600174inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600175 if (GrAA::kNo == aa) {
176 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
177 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600178 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600179 return GrAAType::kMSAA;
180 }
181 return GrAAType::kNone;
182 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600183 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600184}
185
Robert Phillipsf200a902017-01-30 13:27:37 -0500186GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000187 return fRenderTargetProxy->asTextureProxy();
188}
189
Greg Daniele252f082017-10-23 16:05:23 -0400190const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
191 return fRenderTargetProxy->asTextureProxy();
192}
193
Robert Phillipsf200a902017-01-30 13:27:37 -0500194sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
195 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
196}
197
Greg Daniele252f082017-10-23 16:05:23 -0400198GrMipMapped GrRenderTargetContext::mipMapped() const {
199 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
200 return proxy->mipMapped();
201 }
202 return GrMipMapped::kNo;
203}
204
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400205GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800206 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700207 SkDEBUGCODE(this->validate();)
208
Robert Phillipsf2361d22016-10-25 14:20:06 -0400209 if (!fOpList || fOpList->isClosed()) {
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000210 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy, fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700211 }
212
Robert Phillipsdc83b892017-04-13 12:23:54 -0400213 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700214}
215
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400216GrOpList* GrRenderTargetContext::getOpList() {
217 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700218}
219
Herb Derbycddab252018-07-16 11:19:04 -0400220void GrRenderTargetContext::drawGlyphRunList(
221 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400222 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800223 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700224 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700225 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400226 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700227
Greg Danielbe7fc462019-01-03 16:40:42 -0500228 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
229 // secondary command buffers because it would require stopping and starting a render pass which
230 // we don't have access to.
231 if (this->wrapsVkSecondaryCB()) {
232 return;
233 }
234
Herb Derby26cbe512018-05-24 14:39:01 -0400235 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400236 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400237 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700238}
239
Brian Osman11052242016-10-27 14:47:55 -0400240void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800241 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700242 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700243 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400244 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700245
Robert Phillips72152832017-01-25 17:31:35 -0500246 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400247
Robert Phillips380b90c2017-08-30 07:41:07 -0400248 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700249}
250
Brian Osman11052242016-10-27 14:47:55 -0400251void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500252 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700253 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800254 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700255 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700256 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400257 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700258
Robert Phillips72152832017-01-25 17:31:35 -0500259 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700260 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
261 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700262}
robertphillips9199a9f2016-07-13 07:48:43 -0700263
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500264void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
265 const SkPMColor4f& color,
266 CanClearFullscreen canClearFullscreen) {
267 ASSERT_SINGLE_OWNER_PRIV
268 RETURN_IF_ABANDONED_PRIV
269 SkDEBUGCODE(fRenderTargetContext->validate();)
270 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
271 fRenderTargetContext->fContext);
272
273 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
274 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
275}
276
277static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
278 paint->setColor4f(color);
279 if (color.isOpaque()) {
280 // Can just rely on the src-over blend mode to do the right thing
281 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
282 } else {
283 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
284 // were src blended
285 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
286 }
287}
288
289void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
290 const SkPMColor4f& color,
291 CanClearFullscreen canClearFullscreen) {
292 bool isFull = false;
293 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400294 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
295 // only clear the entire target if we knew it had not been cleared before. As
296 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500297 isFull = !clip.scissorEnabled() ||
298 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400299 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500300 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
301 }
302
303 if (isFull) {
Chris Dalton6b982802019-06-27 13:53:46 -0600304 GrRenderTargetOpList* opList = this->getRTOpList();
305 if (opList->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500306 !this->caps()->performColorClearsAsDraws()) {
307 // The op list was emptied and native clears are allowed, so just use the load op
Chris Dalton6b982802019-06-27 13:53:46 -0600308 opList->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500309 return;
310 } else {
311 // Will use an op for the clear, reset the load op to discard since the op will
312 // blow away the color buffer contents
Chris Dalton6b982802019-06-27 13:53:46 -0600313 opList->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500314 }
315
316 // Must add an op to the list (either because we couldn't use a load op, or because the
317 // clear load op isn't supported)
318 if (this->caps()->performColorClearsAsDraws()) {
319 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
320 GrPaint paint;
321 clear_to_grpaint(color, &paint);
322 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400323 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
324 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500325 } else {
Chris Dalton6b982802019-06-27 13:53:46 -0600326 opList->addOp(GrClearOp::Make(fContext, SkIRect::MakeEmpty(), color,
327 /* fullscreen */ true), *this->caps());
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500328 }
329 } else {
330 if (this->caps()->performPartialClearsAsDraws()) {
331 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500332 GrPaint paint;
333 clear_to_grpaint(color, &paint);
334
Michael Ludwig64b28a72019-05-28 12:02:00 -0400335 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400336 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
337 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500338 } else {
339 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
340 this->asSurfaceProxy()));
341 // This version of the clear op factory can return null if the clip doesn't intersect
342 // with the surface proxy's boundary
343 if (!op) {
344 return;
345 }
346 this->getRTOpList()->addOp(std::move(op), *this->caps());
347 }
348 }
349}
350
Brian Osman9a9baae2018-11-05 15:06:26 -0500351void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const SkPMColor4f& color) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500352 ASSERT_SINGLE_OWNER_PRIV
353 RETURN_IF_ABANDONED_PRIV
354 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400355 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
356 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500357
Robert Phillips72152832017-01-25 17:31:35 -0500358 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500359
Brian Salomonbb5711a2017-05-17 13:49:59 -0400360 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
361 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500362
363 if (clearRect) {
364 if (clearRect->contains(rtRect)) {
365 clearRect = nullptr; // full screen
366 } else {
367 if (!rtRect.intersect(*clearRect)) {
368 return;
369 }
370 }
371 }
372
373 // TODO: in a post-MDB world this should be handled at the OpList level.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500374 // This makes sure to always add an op to the list, instead of marking the clear as a load op.
375 // This code follows very similar logic to internalClear() below, but critical differences are
376 // highlighted in line related to absClear()'s unique behavior.
377 if (clearRect) {
378 if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400379 GrPaint paint;
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500380 clear_to_grpaint(color, &paint);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400381
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500382 // Use the disabled clip; the rect geometry already matches the clear rectangle and
383 // if it were added to a scissor, that would be intersected with the logical surface
384 // bounds and not the worst case dimensions required here.
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400385 fRenderTargetContext->addDrawOp(
386 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400387 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
388 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500389 } else {
390 // Must use the ClearOp factory that takes a boolean (false) instead of a surface
391 // proxy. The surface proxy variant would intersect the clip rect with its logical
392 // bounds, which is not desired in this special case.
393 fRenderTargetContext->getRTOpList()->addOp(
394 GrClearOp::Make(fRenderTargetContext->fContext, rtRect, color,
395 /* fullscreen */ false),
396 *fRenderTargetContext->caps());
csmartdalton29df7602016-08-31 11:55:52 -0700397 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500398 } else {
399 // Reset the oplist like in internalClear(), but do not rely on a load op for the clear
Chris Dalton6b982802019-06-27 13:53:46 -0600400 fRenderTargetContext->getRTOpList()->resetForFullscreenClear(
401 fRenderTargetContext->canDiscardPreviousOpsOnFullClear());
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500402 fRenderTargetContext->getRTOpList()->setColorLoadOp(GrLoadOp::kDiscard);
403
404 if (fRenderTargetContext->caps()->performColorClearsAsDraws()) {
405 // This draws a quad covering the worst case dimensions instead of just the logical
406 // width and height like in internalClear().
407 GrPaint paint;
408 clear_to_grpaint(color, &paint);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400409 fRenderTargetContext->addDrawOp(
410 GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400411 GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint),
412 SkMatrix::I(), SkRect::Make(rtRect)));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500413 } else {
414 // Nothing special about this path in absClear compared to internalClear()
415 fRenderTargetContext->getRTOpList()->addOp(
416 GrClearOp::Make(fRenderTargetContext->fContext, SkIRect::MakeEmpty(), color,
417 /* fullscreen */ true),
418 *fRenderTargetContext->caps());
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400419 }
robertphillips9199a9f2016-07-13 07:48:43 -0700420 }
robertphillipsea461502015-05-26 11:38:03 -0700421}
422
Brian Osman11052242016-10-27 14:47:55 -0400423void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500424 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400425 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000426 // Start with the render target, since that is the maximum content we could possibly fill.
427 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400428 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400429 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000430 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
431 // inverse view matrix.
432 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
433 } else {
434 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
435 SkMatrix localMatrix;
436 if (!viewMatrix.invert(&localMatrix)) {
437 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400438 }
Michael Ludwig61328202019-06-19 14:48:58 +0000439 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
440 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700441 }
robertphillipsea461502015-05-26 11:38:03 -0700442}
443
Michael Ludwig61328202019-06-19 14:48:58 +0000444enum class GrRenderTargetContext::QuadOptimization {
445 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
446 kDiscarded,
447 // The rect to draw was converted to some other op and appended to the oplist, so no additional
448 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
449 // a constColor is provided.
450 kSubmitted,
451 // The clip was folded into the device quad, with updated edge flags and local coords, and
452 // caller is responsible for adding an appropriate op.
453 kClipApplied,
454 // No change to clip, but quad updated to better fit clip/render target, and caller is
455 // responsible for adding an appropriate op.
456 kCropped
457};
Michael Ludwig61a16512019-01-15 11:15:13 -0500458
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400459static bool make_vertex_finite(float* value) {
460 if (SkScalarIsNaN(*value)) {
461 return false;
462 }
463
464 if (!SkScalarIsFinite(*value)) {
465 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
466 // left when calculating crops.
467 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
468 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
469 }
470
471 return true;
472}
473
Michael Ludwig61328202019-06-19 14:48:58 +0000474GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400475 const GrClip& clip, const SkPMColor4f* constColor,
476 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
477 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000478 // Optimization requirements:
479 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
480 // 2. kClear applies when constColor and final geom is pixel aligned rect;
481 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
482 // 3. kRRect applies when constColor and rrect clip and quad covers clip
483 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
484 // 5. kCropped applies when rect quad (currently)
485 // 6. kNone always applies
486 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000487
Michael Ludwige08b4432019-06-19 18:00:48 -0400488 SkRect rtRect;
489 if (stencilSettings) {
490 // Must use worst case bounds so that stencil buffer updates on approximately sized render
491 // targets don't get corrupted.
492 rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(),
493 fRenderTargetProxy->worstCaseHeight());
494 } else {
495 // Use the logical size of the render target, which allows for "fullscreen" clears even if
496 // the render target has an approximate backing fit
497 rtRect = SkRect::MakeWH(this->width(), this->height());
498 }
499
Michael Ludwig61328202019-06-19 14:48:58 +0000500 SkRect drawBounds = deviceQuad->bounds();
501 if (constColor) {
502 // Don't bother updating local coordinates when the paint will ignore them anyways
503 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400504 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
505 // will be cropped to the finite 'clip' or render target and there is no local space mapping
506 if (!deviceQuad->isFinite()) {
507 for (int i = 0; i < 4; ++i) {
508 if (!make_vertex_finite(deviceQuad->xs() + i) ||
509 !make_vertex_finite(deviceQuad->ys() + i) ||
510 !make_vertex_finite(deviceQuad->ws() + i)) {
511 // Discard if we see a nan
512 return QuadOptimization::kDiscarded;
513 }
514 }
515 SkASSERT(deviceQuad->isFinite());
516 }
517 } else {
518 // CropToRect requires the quads to be finite. If they are not finite and we have local
519 // coordinates, the mapping from local space to device space is poorly defined so drop it
520 if (!deviceQuad->isFinite()) {
521 return QuadOptimization::kDiscarded;
522 }
Brian Salomon7694b902019-06-18 21:00:21 +0000523 }
524
Michael Ludwig61328202019-06-19 14:48:58 +0000525 // If the quad is entirely off screen, it doesn't matter what the clip does
526 if (!rtRect.intersects(drawBounds)) {
527 return QuadOptimization::kDiscarded;
528 }
Brian Salomon7694b902019-06-18 21:00:21 +0000529
Michael Ludwig61328202019-06-19 14:48:58 +0000530 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
531 // the render target).
532 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400533 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
534 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
535 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
536 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000537 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000538 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000539 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
540 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000541 }
Brian Salomon7694b902019-06-18 21:00:21 +0000542 }
543
Michael Ludwig61328202019-06-19 14:48:58 +0000544 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
545 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400546 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000547 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
548 // of the render target and the clip rect)
549 SkRect clipBounds = rtRect;
550 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
551 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000552 }
553
Michael Ludwig61328202019-06-19 14:48:58 +0000554 if (clipRRect.isRect()) {
555 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
556 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
557 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
558 // Clear optimization is possible
559 drawBounds = deviceQuad->bounds();
560 if (drawBounds.contains(rtRect)) {
561 // Fullscreen clear
562 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
563 return QuadOptimization::kSubmitted;
564 } else if (GrClip::IsPixelAligned(drawBounds) &&
565 drawBounds.width() > 256 && drawBounds.height() > 256) {
566 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
567 SkIRect scissorRect;
568 drawBounds.round(&scissorRect);
569 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
570 return QuadOptimization::kSubmitted;
571 }
572 }
573
574 // Update overall AA setting.
575 *edgeFlags = newFlags;
576 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
577 newFlags != GrQuadAAFlags::kNone) {
578 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
579 // properly reflect the smooth edge of the clip.
580 *aa = GrAA::kYes;
581 }
582 // We intentionally do not downgrade AA here because we don't know if we need to
583 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
584 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
585
586 // deviceQuad is exactly the intersection of original quad and clip, so it can be
587 // drawn with no clip (submitted by caller)
588 return QuadOptimization::kClipApplied;
589 } else {
590 // The quads have been updated to better fit the clip bounds, but can't get rid of
591 // the clip entirely
592 return QuadOptimization::kCropped;
593 }
594 } else if (constColor) {
595 // Rounded corners and constant filled color (limit ourselves to solid colors because
596 // there is no way to use custom local coordinates with drawRRect).
597 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
598 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
599 deviceQuad->bounds().contains(clipBounds)) {
600 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
601 // we can draw the rrect directly and ignore the edge flags
602 GrPaint paint;
603 clear_to_grpaint(*constColor, &paint);
604 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
605 clipRRect, GrStyle::SimpleFill());
606 return QuadOptimization::kSubmitted;
607 } else {
608 // The quad has been updated to better fit clip bounds, but can't remove the clip
609 return QuadOptimization::kCropped;
610 }
Brian Salomon7694b902019-06-18 21:00:21 +0000611 }
Brian Salomon7694b902019-06-18 21:00:21 +0000612 }
613
Michael Ludwig61328202019-06-19 14:48:58 +0000614 // Crop the quad to the conservative bounds of the clip.
615 SkIRect clipDevBounds;
616 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
617 SkRect clipBounds = SkRect::Make(clipDevBounds);
618
619 // One final check for discarding, since we may have gone here directly due to a complex clip
620 if (!clipBounds.intersects(drawBounds)) {
621 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000622 }
623
Michael Ludwig61328202019-06-19 14:48:58 +0000624 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
625 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
626 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
627
628 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000629}
630
Michael Ludwig61328202019-06-19 14:48:58 +0000631void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500632 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500633 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000634 GrQuadAAFlags edgeFlags,
635 const GrQuad& deviceQuad,
636 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400637 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000638 ASSERT_SINGLE_OWNER
639 RETURN_IF_ABANDONED
640 SkDEBUGCODE(this->validate();)
641 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500642
Michael Ludwig61328202019-06-19 14:48:58 +0000643 AutoCheckFlush acf(this->drawingManager());
644
645 SkPMColor4f* constColor = nullptr;
646 SkPMColor4f paintColor;
647 if (!ss && !paint.numCoverageFragmentProcessors() &&
648 paint.isConstantBlendedColor(&paintColor)) {
649 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
650 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500651 }
652
Michael Ludwig61328202019-06-19 14:48:58 +0000653 GrQuad croppedDeviceQuad = deviceQuad;
654 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400655 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
656 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000657 if (opt >= QuadOptimization::kClipApplied) {
658 // These optimizations require caller to add an op themselves
659 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
660 : clip;
661 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
662 : this->chooseAAType(aa);
663 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
664 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700665 }
Michael Ludwig61328202019-06-19 14:48:58 +0000666 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800667}
668
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000669void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
670 sk_sp<GrTextureProxy> proxy,
671 sk_sp<GrColorSpaceXform> textureXform,
672 GrSamplerState::Filter filter,
673 const SkPMColor4f& color,
674 SkBlendMode blendMode,
675 GrAA aa,
676 GrQuadAAFlags edgeFlags,
677 const GrQuad& deviceQuad,
678 const GrQuad& localQuad,
679 const SkRect* domain) {
680 ASSERT_SINGLE_OWNER
681 RETURN_IF_ABANDONED
682 SkDEBUGCODE(this->validate();)
683 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
684
685 AutoCheckFlush acf(this->drawingManager());
686
687 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
688 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
689 GrQuad croppedDeviceQuad = deviceQuad;
690 GrQuad croppedLocalQuad = localQuad;
691 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
692 &croppedDeviceQuad, &croppedLocalQuad);
693
694 SkASSERT(opt != QuadOptimization::kSubmitted);
695 if (opt != QuadOptimization::kDiscarded) {
696 // And the texture op if not discarded
697 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
698 : clip;
699 GrAAType aaType = this->chooseAAType(aa);
700 // Use the provided domain, although hypothetically we could detect that the cropped local
701 // quad is sufficiently inside the domain and the constraint could be dropped.
702 this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy),
703 std::move(textureXform), filter, color,
704 blendMode, aaType, edgeFlags,
705 croppedDeviceQuad, croppedLocalQuad, domain));
706 }
707}
708
Brian Osman11052242016-10-27 14:47:55 -0400709void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500710 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500711 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400712 const SkMatrix& viewMatrix,
713 const SkRect& rect,
714 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700715 if (!style) {
716 style = &GrStyle::SimpleFill();
717 }
joshualitt1de610a2016-01-06 08:26:09 -0800718 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700719 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700720 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400721 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700722
bsalomon6663acf2016-05-10 09:14:17 -0700723 // Path effects should've been devolved to a path in SkGpuDevice
724 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700725
Robert Phillips72152832017-01-25 17:31:35 -0500726 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700727
bsalomon6663acf2016-05-10 09:14:17 -0700728 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400729 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000730 // Fills the rect, using rect as its own local coordinates
731 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500732 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700733 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
734 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
735 if ((!rect.width() || !rect.height()) &&
736 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
737 SkScalar r = stroke.getWidth() / 2;
738 // TODO: Move these stroke->fill fallbacks to GrShape?
739 switch (stroke.getJoin()) {
740 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500741 this->drawRect(
742 clip, std::move(paint), aa, viewMatrix,
743 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
744 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700745 return;
746 case SkPaint::kRound_Join:
747 // Raster draws nothing when both dimensions are empty.
748 if (rect.width() || rect.height()){
749 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500750 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
751 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700752 return;
753 }
754 case SkPaint::kBevel_Join:
755 if (!rect.width()) {
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 - r, rect.fTop, rect.fRight + r, rect.fBottom},
758 &GrStyle::SimpleFill());
759 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500760 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700761 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
762 &GrStyle::SimpleFill());
763 }
764 return;
765 }
766 }
robertphillips44302392016-07-08 14:43:03 -0700767
Brian Salomonbaaf4392017-06-15 09:59:23 -0400768 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700769
Chris Dalton7d6748e2019-03-13 00:34:52 -0600770 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500771 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
772 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
773 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500774 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400775 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700776 return;
robertphillips4bc31812016-03-01 12:22:49 -0800777 }
robertphillips4bc31812016-03-01 12:22:49 -0800778 }
Mike Klein16885072018-12-11 09:54:31 -0500779 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500780 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700781}
782
Michael Ludwig69858532018-11-28 15:34:34 -0500783void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
784 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
785 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600786 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig69858532018-11-28 15:34:34 -0500787 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
788 quads, cnt));
789}
790
Robert Phillipsec2249f2016-11-09 08:54:35 -0500791int GrRenderTargetContextPriv::maxWindowRectangles() const {
792 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400793 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500794}
795
Chris Dalton6b982802019-06-27 13:53:46 -0600796GrRenderTargetOpList::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
797 ) const {
798#if GR_TEST_UTILS
799 if (fPreserveOpsOnFullClear_TestingOnly) {
800 return GrRenderTargetOpList::CanDiscardPreviousOps::kNo;
801 }
802#endif
803 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
804 // would normally be overwritten. The one exception is if the render target context is marked as
805 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
806 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
807 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
808 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Chris Daltoneffee202019-07-01 22:28:03 -0600809 return GrRenderTargetOpList::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600810}
811
Chris Daltoneffee202019-07-01 22:28:03 -0600812void GrRenderTargetContext::setNeedsStencil(bool multisampled) {
813 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600814 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Daltoneffee202019-07-01 22:28:03 -0600815 bool needsStencilClear = !fNumStencilSamples;
Chris Dalton6b982802019-06-27 13:53:46 -0600816
Chris Daltoneffee202019-07-01 22:28:03 -0600817 int numRequiredSamples = this->numSamples();
818 if (multisampled && 1 == numRequiredSamples) {
819 // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use
820 // mixed samples.
821 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
822 numRequiredSamples = this->caps()->internalMultisampleCount(
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400823 this->asSurfaceProxy()->config());
Chris Daltoneffee202019-07-01 22:28:03 -0600824 }
825 SkASSERT(numRequiredSamples > 0);
826
827 if (numRequiredSamples > fNumStencilSamples) {
828 fNumStencilSamples = numRequiredSamples;
829 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
830 }
Chris Dalton6b982802019-06-27 13:53:46 -0600831
832 if (needsStencilClear) {
833 if (this->caps()->performStencilClearsAsDraws()) {
834 // There is a driver bug with clearing stencil. We must use an op to manually clear the
835 // stencil buffer before the op that required 'setNeedsStencil'.
836 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
837 } else {
838 // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush
839 // code note when the instantiated stencil buffer is already clear and skip the clear
840 // altogether. And on tilers, loading the stencil buffer cleared is even faster than
841 // preserving the previous contents.
842 this->getRTOpList()->setStencilLoadOp(GrLoadOp::kClear);
843 }
844 }
845}
846
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000847void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700848 ASSERT_SINGLE_OWNER_PRIV
849 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400850 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400851 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
852 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700853
Robert Phillips72152832017-01-25 17:31:35 -0500854 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400855
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500856 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
857}
858
859void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
860 if (this->caps()->performStencilClearsAsDraws()) {
861 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
862 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
863
864 // Configure the paint to have no impact on the color buffer
865 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400866 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400867 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
868 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500869 } else {
870 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
871 fRenderTargetProxy.get()));
872 if (!op) {
873 return;
874 }
875 this->getRTOpList()->addOp(std::move(op), *this->caps());
Robert Phillipse60ad622016-11-17 10:22:48 -0500876 }
robertphillips976f5f02016-06-03 10:59:20 -0700877}
878
Chris Daltonbbfd5162017-11-07 13:35:22 -0700879void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600880 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400881 const SkMatrix& viewMatrix,
882 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500883 ASSERT_SINGLE_OWNER_PRIV
884 RETURN_IF_ABANDONED_PRIV
885 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400886 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
887 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500888
Brian Salomon467921e2017-03-06 16:17:12 -0500889 // TODO: extract portions of checkDraw that are relevant to path stenciling.
890 SkASSERT(path);
891 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
892
893 // FIXME: Use path bounds instead of this WAR once
894 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
895 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
896
897 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700898 GrAppliedHardClip appliedClip;
899 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
900 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500901 return;
902 }
903
Robert Phillips7c525e62018-06-12 10:11:12 -0400904 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
905 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600906 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500907 path->getFillType(),
908 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500909 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500910 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400911 if (!op) {
912 return;
913 }
Brian Salomon97180af2017-03-14 13:42:58 -0400914 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600915
Chris Daltoneffee202019-07-01 22:28:03 -0600916 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400917 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700918}
919
Brian Salomond7065e72018-10-12 11:42:02 -0400920void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500921 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400922 GrAA aa, SkCanvas::SrcRectConstraint constraint,
923 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500924 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400925 ASSERT_SINGLE_OWNER
926 RETURN_IF_ABANDONED
927 SkDEBUGCODE(this->validate();)
928 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500929
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500930 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500931 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwig22429f92019-06-27 10:44:48 -0400932 // Draw one at a time since the bulk API doesn't support non src-over blending, or the
933 // backend can't support the bulk geometry processor yet.
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500934 SkMatrix ctm;
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500935 for (int i = 0; i < cnt; ++i) {
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500936 float alpha = set[i].fAlpha;
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500937 ctm = viewMatrix;
938 if (set[i].fPreViewMatrix) {
939 ctm.preConcat(*set[i].fPreViewMatrix);
940 }
941
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000942 GrQuad quad, srcQuad;
943 if (set[i].fDstClipQuad) {
944 quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm);
945
946 SkPoint srcPts[4];
947 GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4);
948 srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I());
Michael Ludwigce62dec2019-02-19 11:48:46 -0500949 } else {
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000950 quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm);
951 srcQuad = GrQuad(set[i].fSrcRect);
Michael Ludwigce62dec2019-02-19 11:48:46 -0500952 }
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000953
954 const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint
955 ? &set[i].fSrcRect : nullptr;
956 this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter,
957 {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags,
958 quad, srcQuad, domain);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500959 }
960 } else {
961 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500962 AutoCheckFlush acf(this->drawingManager());
Chris Dalton7d6748e2019-03-13 00:34:52 -0600963 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig31ba7182019-04-03 10:38:06 -0400964 auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix,
Michael Ludwig009b92e2019-02-15 16:03:53 -0500965 std::move(texXform));
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500966 this->addDrawOp(clip, std::move(op));
967 }
Brian Salomond7065e72018-10-12 11:42:02 -0400968}
969
Brian Osman11052242016-10-27 14:47:55 -0400970void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500971 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400972 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400973 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400974 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400975 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400976 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500977 ASSERT_SINGLE_OWNER
978 RETURN_IF_ABANDONED
979 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400980 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500981
982 AutoCheckFlush acf(this->drawingManager());
983
984 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600985 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400986 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400987 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400988 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400989 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700990}
991
992///////////////////////////////////////////////////////////////////////////////
993
Brian Osman4d92b892019-03-24 00:53:23 +0000994void GrRenderTargetContext::drawAtlas(const GrClip& clip,
995 GrPaint&& paint,
996 const SkMatrix& viewMatrix,
997 int spriteCount,
998 const SkRSXform xform[],
999 const SkRect texRect[],
1000 const SkColor colors[]) {
1001 ASSERT_SINGLE_OWNER
1002 RETURN_IF_ABANDONED
1003 SkDEBUGCODE(this->validate();)
1004 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
1005
1006 AutoCheckFlush acf(this->drawingManager());
1007
1008 GrAAType aaType = this->chooseAAType(GrAA::kNo);
1009 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1010 aaType, spriteCount, xform, texRect, colors);
1011 this->addDrawOp(clip, std::move(op));
1012}
1013
1014///////////////////////////////////////////////////////////////////////////////
1015
Brian Osman11052242016-10-27 14:47:55 -04001016void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001017 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001018 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001019 const SkMatrix& viewMatrix,
1020 const SkRRect& rrect,
1021 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001022 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001023 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001024 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001025 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001026
1027 const SkStrokeRec& stroke = style.strokeRec();
1028 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001029 return;
1030 }
1031
bsalomon7f0d9f32016-08-15 14:49:10 -07001032 GrNoClip noclip;
1033 const GrClip* clip = &origClip;
1034#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1035 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001036 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001037 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1038 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -04001039 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
1040 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -07001041 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -04001042 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
1043 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -07001044 clip = &noclip;
1045 }
1046#endif
bsalomon6663acf2016-05-10 09:14:17 -07001047 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001048
Robert Phillips72152832017-01-25 17:31:35 -05001049 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001050
Chris Dalton7d6748e2019-03-13 00:34:52 -06001051 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -05001052
Chris Dalton0dffbab2019-03-27 13:08:50 -06001053 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -04001054 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
1055 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
1056 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
1057 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
1058 // to avoid perf regressions on some platforms.
1059 assert_alive(paint);
1060 op = GrOvalOpFactory::MakeCircularRRectOp(
1061 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
1062 }
1063 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001064 assert_alive(paint);
1065 op = GrFillRRectOp::Make(
1066 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
1067 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001068 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001069 assert_alive(paint);
1070 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +00001071 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001072 }
1073 if (op) {
1074 this->addDrawOp(*clip, std::move(op));
1075 return;
robertphillipsea461502015-05-26 11:38:03 -07001076 }
robertphillipsb56f9272016-02-25 11:03:52 -08001077
Mike Klein16885072018-12-11 09:54:31 -05001078 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001079 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1080 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001081}
1082
Jim Van Verthc5903412016-11-17 15:27:09 -05001083///////////////////////////////////////////////////////////////////////////////
1084
Jim Van Verth3af1af92017-05-18 15:06:54 -04001085static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1086 SkPoint3 result;
1087 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1088 result.fZ = pt.fZ;
1089 return result;
1090}
1091
1092bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001093 const SkMatrix& viewMatrix,
1094 const SkPath& path,
1095 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001096 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001097 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001098 return true;
1099 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001100 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001101 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001102
1103 // check z plane
1104 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1105 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1106 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1107 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1108 return false;
1109 }
1110
1111 SkRRect rrect;
1112 SkRect rect;
1113 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001114 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001115 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1116 if (!isRRect &&
1117 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1118 rect.width() > SK_ScalarNearlyZero) {
1119 rrect.setOval(rect);
1120 isRRect = true;
1121 }
1122 if (!isRRect && path.isRect(&rect)) {
1123 rrect.setRect(rect);
1124 isRRect = true;
1125 }
1126
1127 if (!isRRect) {
1128 return false;
1129 }
1130
Jim Van Verthc5903412016-11-17 15:27:09 -05001131 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001132 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001133 }
1134
Robert Phillips72152832017-01-25 17:31:35 -05001135 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001136
Jim Van Verth3af1af92017-05-18 15:06:54 -04001137 // transform light
1138 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1139
1140 // 1/scale
1141 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1142 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1143 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1144 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1145
1146 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001147 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1148
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001149 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001150 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1151 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1152 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001153
1154 // Outset the shadow rrect to the border of the penumbra
1155 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1156 SkRRect ambientRRect;
1157 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1158 // If the rrect was an oval then its outset will also be one.
1159 // We set it explicitly to avoid errors.
1160 if (rrect.isOval()) {
1161 ambientRRect = SkRRect::MakeOval(outsetRect);
1162 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001163 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001164 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1165 }
1166
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001167 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001168 if (transparent) {
1169 // set a large inset to force a fill
1170 devSpaceInsetWidth = ambientRRect.width();
1171 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001172
Robert Phillips7c525e62018-06-12 10:11:12 -04001173 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1174 ambientColor,
1175 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001176 ambientRRect,
1177 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001178 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001179 if (op) {
1180 this->addDrawOp(clip, std::move(op));
1181 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001182 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001183
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001184 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001185 SkScalar devSpaceSpotBlur;
1186 SkScalar spotScale;
1187 SkVector spotOffset;
1188 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1189 devLightPos.fZ, rec.fLightRadius,
1190 &devSpaceSpotBlur, &spotScale, &spotOffset);
1191 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001192 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1193
Jim Van Verth3af1af92017-05-18 15:06:54 -04001194 // Adjust translate for the effect of the scale.
1195 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1196 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1197 // This offset is in dev space, need to transform it into source space.
1198 SkMatrix ctmInverse;
1199 if (viewMatrix.invert(&ctmInverse)) {
1200 ctmInverse.mapPoints(&spotOffset, 1);
1201 } else {
1202 // Since the matrix is a similarity, this should never happen, but just in case...
1203 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1204 SkASSERT(false);
1205 }
1206
1207 // Compute the transformed shadow rrect
1208 SkRRect spotShadowRRect;
1209 SkMatrix shadowTransform;
1210 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1211 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001212 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001213
1214 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001215 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001216 SkScalar insetWidth = blurOutset;
1217 if (transparent) {
1218 // If transparent, just do a fill
1219 insetWidth += spotShadowRRect.width();
1220 } else {
1221 // For shadows, instead of using a stroke we specify an inset from the penumbra
1222 // border. We want to extend this inset area so that it meets up with the caster
1223 // geometry. The inset geometry will by default already be inset by the blur width.
1224 //
1225 // We compare the min and max corners inset by the radius between the original
1226 // rrect and the shadow rrect. The distance between the two plus the difference
1227 // between the scaled radius and the original radius gives the distance from the
1228 // transformed shadow shape to the original shape in that corner. The max
1229 // of these gives the maximum distance we need to cover.
1230 //
1231 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1232 // that to get the full insetWidth.
1233 SkScalar maxOffset;
1234 if (rrect.isRect()) {
1235 // Manhattan distance works better for rects
1236 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1237 rrect.rect().fLeft),
1238 SkTAbs(spotShadowRRect.rect().fTop -
1239 rrect.rect().fTop)),
1240 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1241 rrect.rect().fRight),
1242 SkTAbs(spotShadowRRect.rect().fBottom -
1243 rrect.rect().fBottom)));
1244 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001245 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001246 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1247 rrect.rect().fLeft + dr,
1248 spotShadowRRect.rect().fTop -
1249 rrect.rect().fTop + dr);
1250 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1251 rrect.rect().fRight - dr,
1252 spotShadowRRect.rect().fBottom -
1253 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001254 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1255 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001256 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001257 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001258 }
1259
1260 // Outset the shadow rrect to the border of the penumbra
1261 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1262 if (spotShadowRRect.isOval()) {
1263 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1264 } else {
1265 SkScalar outsetRad = spotRadius + blurOutset;
1266 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1267 }
1268
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001269 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001270
Robert Phillips7c525e62018-06-12 10:11:12 -04001271 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1272 spotColor,
1273 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001274 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001275 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001276 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001277 if (op) {
1278 this->addDrawOp(clip, std::move(op));
1279 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001280 }
1281
1282 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001283}
1284
1285///////////////////////////////////////////////////////////////////////////////
1286
Brian Osman11052242016-10-27 14:47:55 -04001287bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001288 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001289 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001290 const SkMatrix& viewMatrix,
1291 const SkRRect& origOuter,
1292 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001293 SkASSERT(!origInner.isEmpty());
1294 SkASSERT(!origOuter.isEmpty());
1295
Brian Salomon65749212017-12-01 16:01:47 -05001296 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1297
Chris Dalton7d6748e2019-03-13 00:34:52 -06001298 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001299
1300 if (GrAAType::kMSAA == aaType) {
1301 return false;
1302 }
1303
Greg Daniel2655ede2019-04-10 00:49:28 +00001304 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1305 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001306 auto outerR = outer->width() / 2.f;
1307 auto innerR = inner->width() / 2.f;
1308 auto cx = outer->getBounds().fLeft + outerR;
1309 auto cy = outer->getBounds().fTop + outerR;
1310 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1311 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1312 auto avgR = (innerR + outerR) / 2.f;
1313 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1314 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1315 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001316 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001317 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001318 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001319 if (op) {
1320 this->addDrawOp(clip, std::move(op));
1321 return true;
1322 }
Mike Klein16885072018-12-11 09:54:31 -05001323 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001324 }
1325 }
1326
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001327 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001328 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001329 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1330 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001331 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001332 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1333 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001334 }
robertphillips00095892016-02-29 13:50:40 -08001335
robertphillips00095892016-02-29 13:50:40 -08001336 SkMatrix inverseVM;
1337 if (!viewMatrix.isIdentity()) {
1338 if (!origInner.transform(viewMatrix, inner.writable())) {
1339 return false;
1340 }
1341 if (!origOuter.transform(viewMatrix, outer.writable())) {
1342 return false;
1343 }
1344 if (!viewMatrix.invert(&inverseVM)) {
1345 return false;
1346 }
1347 } else {
1348 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001349 }
robertphillips00095892016-02-29 13:50:40 -08001350
Ethan Nicholaseace9352018-10-15 20:09:54 +00001351 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001352 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001353 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001354 if (!innerEffect) {
1355 return false;
1356 }
1357
Ethan Nicholaseace9352018-10-15 20:09:54 +00001358 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001359 if (!outerEffect) {
1360 return false;
1361 }
1362
Brian Salomon82f44312017-01-11 13:42:54 -05001363 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1364 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001365
1366 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001367 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001368 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1369 }
halcanary9d524f22016-03-29 09:03:52 -07001370
Brian Salomon82f44312017-01-11 13:42:54 -05001371 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1372 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001373 return true;
1374}
1375
Brian Osman11052242016-10-27 14:47:55 -04001376void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001377 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001378 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001379 const SkMatrix& viewMatrix,
1380 const SkRRect& outer,
1381 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001382 ASSERT_SINGLE_OWNER
1383 RETURN_IF_ABANDONED
1384 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001385 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001386
1387 SkASSERT(!outer.isEmpty());
1388 SkASSERT(!inner.isEmpty());
1389
Robert Phillips72152832017-01-25 17:31:35 -05001390 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001391
Brian Salomon82f44312017-01-11 13:42:54 -05001392 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001393 return;
1394 }
Mike Klein16885072018-12-11 09:54:31 -05001395 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001396
1397 SkPath path;
1398 path.setIsVolatile(true);
1399 path.addRRect(inner);
1400 path.addRRect(outer);
1401 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001402 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001403}
1404
robertphillipsea461502015-05-26 11:38:03 -07001405///////////////////////////////////////////////////////////////////////////////
1406
Brian Osman11052242016-10-27 14:47:55 -04001407void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001408 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001409 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001410 const SkMatrix& viewMatrix,
1411 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001412 const GrStyle& style,
1413 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001414 ASSERT_SINGLE_OWNER
1415 RETURN_IF_ABANDONED
1416 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001417 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001418
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001419 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001420 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001421 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001422 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001423 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1424 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001425 aa = GrAA::kNo;
1426 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001427 }
msarettcc319b92016-08-25 18:07:18 -07001428 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001429 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001430 SkPath path;
1431 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001432 path.setIsVolatile(true);
1433
Brian Salomon82f44312017-01-11 13:42:54 -05001434 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001435 }
1436
Chris Dalton7d6748e2019-03-13 00:34:52 -06001437 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001438 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1439 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001440 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001441}
1442
Brian Osman11052242016-10-27 14:47:55 -04001443void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001444 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001445 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001446 const SkMatrix& viewMatrix,
1447 const SkRect& oval,
1448 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001449 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001450 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001451 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001452 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001453
Robert Phillips7484d202018-07-03 09:09:08 -04001454 const SkStrokeRec& stroke = style.strokeRec();
1455
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001456 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001457 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1458 return;
1459 }
1460
1461 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001462 return;
robertphillipsea461502015-05-26 11:38:03 -07001463 }
1464
Robert Phillips72152832017-01-25 17:31:35 -05001465 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001466
Chris Dalton7d6748e2019-03-13 00:34:52 -06001467 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001468
1469 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001470 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1471 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001472 // We don't draw true circles as round rects in coverage mode, because it can
1473 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1474 assert_alive(paint);
1475 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1476 this->caps()->shaderCaps());
1477 }
1478 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001479 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1480 // the arc equation. This same special geometry and fragment branch also turn out to be a
1481 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1482 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1483 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001484 assert_alive(paint);
1485 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1486 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001487 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001488 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001489 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001490 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1491 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001492 }
1493 if (op) {
1494 this->addDrawOp(clip, std::move(op));
1495 return;
robertphillipsea461502015-05-26 11:38:03 -07001496 }
robertphillipsb56f9272016-02-25 11:03:52 -08001497
Mike Klein16885072018-12-11 09:54:31 -05001498 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001499 this->drawShapeUsingPathRenderer(
1500 clip, std::move(paint), aa, viewMatrix,
1501 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001502}
1503
Brian Osman11052242016-10-27 14:47:55 -04001504void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001505 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001506 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001507 const SkMatrix& viewMatrix,
1508 const SkRect& oval,
1509 SkScalar startAngle,
1510 SkScalar sweepAngle,
1511 bool useCenter,
1512 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001513 ASSERT_SINGLE_OWNER
1514 RETURN_IF_ABANDONED
1515 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001516 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001517
1518 AutoCheckFlush acf(this->drawingManager());
1519
Chris Dalton7d6748e2019-03-13 00:34:52 -06001520 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001521 if (GrAAType::kCoverage == aaType) {
1522 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1523 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1524 std::move(paint),
1525 viewMatrix,
1526 oval,
1527 startAngle,
1528 sweepAngle,
1529 useCenter,
1530 style,
1531 shaderCaps);
1532 if (op) {
1533 this->addDrawOp(clip, std::move(op));
1534 return;
1535 }
1536 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001537 }
Brian Salomone4949402018-04-26 15:22:04 -04001538 this->drawShapeUsingPathRenderer(
1539 clip, std::move(paint), aa, viewMatrix,
1540 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001541}
1542
Brian Osman11052242016-10-27 14:47:55 -04001543void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001544 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001545 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001546 sk_sp<GrTextureProxy> image,
1547 sk_sp<GrColorSpaceXform> csxf,
1548 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001549 std::unique_ptr<SkLatticeIter> iter,
1550 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001551 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001552 RETURN_IF_ABANDONED
1553 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001554 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001555
Robert Phillips72152832017-01-25 17:31:35 -05001556 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001557
Brian Salomon2a943df2018-05-04 13:43:19 -04001558 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001559 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1560 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001561 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001562}
1563
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001564void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1565 const SkRect& bounds) {
1566 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1567 SkASSERT(op);
1568 this->getRTOpList()->addOp(std::move(op), *this->caps());
1569}
1570
Brian Salomon031b0ba2019-05-23 11:05:26 -04001571sk_sp<GrRenderTargetContext> GrRenderTargetContext::rescale(const SkImageInfo& info,
1572 const SkIRect& srcRect,
1573 SkSurface::RescaleGamma rescaleGamma,
1574 SkFilterQuality rescaleQuality) {
Brian Salomonab32f652019-05-10 14:24:50 -04001575 auto direct = fContext->priv().asDirectContext();
1576 if (!direct) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001577 return nullptr;
Brian Salomonab32f652019-05-10 14:24:50 -04001578 }
1579 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001580 return nullptr;
1581 }
1582
Brian Salomon4d036892019-07-02 15:10:58 -04001583 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
1584 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001585 return nullptr;
1586 }
1587
1588 int srcW = srcRect.width();
1589 int srcH = srcRect.height();
1590 int srcX = srcRect.fLeft;
1591 int srcY = srcRect.fTop;
Greg Daniel46cfbc62019-06-07 11:43:30 -04001592 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001593 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
Greg Daniel46cfbc62019-06-07 11:43:30 -04001594 if (!texProxy) {
1595 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(), GrMipMapped::kNo,
1596 srcRect, SkBackingFit::kApprox, SkBudgeted::kNo);
1597 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001598 return nullptr;
1599 }
1600 srcX = 0;
1601 srcY = 0;
1602 constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001603 }
1604
1605 float sx = (float)info.width() / srcW;
1606 float sy = (float)info.height() / srcH;
1607
1608 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
1609 int stepsX;
1610 int stepsY;
1611 if (rescaleQuality > kNone_SkFilterQuality) {
1612 stepsX = static_cast<int>((sx > 1.f) ? std::ceil(std::log2f(sx))
1613 : std::floor(std::log2f(sx)));
1614 stepsY = static_cast<int>((sy > 1.f) ? std::ceil(std::log2f(sy))
1615 : std::floor(std::log2f(sy)));
1616 } else {
1617 stepsX = sx != 1.f;
1618 stepsY = sy != 1.f;
1619 }
1620 SkASSERT(stepsX || stepsY);
Brian Salomond6287472019-06-24 15:50:07 -04001621 auto currRTC = sk_ref_sp(this);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001622 // Assume we should ignore the rescale linear request if the surface has no color space since
1623 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4d036892019-07-02 15:10:58 -04001624 if (rescaleGamma == SkSurface::RescaleGamma::kLinear && this->colorSpaceInfo().colorSpace() &&
1625 !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) {
1626 auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma();
Brian Salomon4d036892019-07-02 15:10:58 -04001627 auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1628 this->colorSpaceInfo().alphaType(),
1629 cs.get(), kPremul_SkAlphaType);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001630 // We'll fall back to kRGBA_8888 if half float not supported.
1631 auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001632 SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo,
1633 kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001634 if (!linearRTC) {
1635 return nullptr;
1636 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04001637 linearRTC->drawTexture(GrNoClip(), texProxy,
Brian Salomon031b0ba2019-05-23 11:05:26 -04001638 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc,
1639 SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH),
1640 GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(),
1641 std::move(xform));
Greg Daniel46cfbc62019-06-07 11:43:30 -04001642 texProxy = linearRTC->asTextureProxyRef();
Brian Salomond6287472019-06-24 15:50:07 -04001643 currRTC = std::move(linearRTC);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001644 srcX = 0;
1645 srcY = 0;
1646 constraint = SkCanvas::kFast_SrcRectConstraint;
1647 }
1648 while (stepsX || stepsY) {
1649 int nextW = info.width();
1650 int nextH = info.height();
1651 if (stepsX < 0) {
1652 nextW = info.width() << (-stepsX - 1);
1653 stepsX++;
1654 } else if (stepsX != 0) {
1655 if (stepsX > 1) {
1656 nextW = srcW * 2;
1657 }
1658 --stepsX;
1659 }
1660 if (stepsY < 0) {
1661 nextH = info.height() << (-stepsY - 1);
1662 stepsY++;
1663 } else if (stepsY != 0) {
1664 if (stepsY > 1) {
1665 nextH = srcH * 2;
1666 }
1667 --stepsY;
1668 }
Brian Salomond6287472019-06-24 15:50:07 -04001669 GrColorType colorType = currRTC->colorSpaceInfo().colorType();
Brian Salomon4d036892019-07-02 15:10:58 -04001670 auto cs = currRTC->colorSpaceInfo().refColorSpace();
Brian Salomon900dda82019-06-03 13:12:55 -04001671 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4d036892019-07-02 15:10:58 -04001672 auto prevAlphaType = currRTC->colorSpaceInfo().alphaType();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001673 if (!stepsX && !stepsY) {
1674 // Might as well fold conversion to final info in the last step.
Brian Salomon031b0ba2019-05-23 11:05:26 -04001675 cs = info.refColorSpace();
Brian Salomond6287472019-06-24 15:50:07 -04001676 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4d036892019-07-02 15:10:58 -04001677 xform = GrColorSpaceXform::Make(currRTC->colorSpaceInfo().colorSpace(),
1678 currRTC->colorSpaceInfo().alphaType(),
1679 cs.get(), info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001680 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04001681 currRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001682 SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo,
1683 kTopLeft_GrSurfaceOrigin);
Greg Daniel46cfbc62019-06-07 11:43:30 -04001684 if (!currRTC) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001685 return nullptr;
1686 }
1687 auto dstRect = SkRect::MakeWH(nextW, nextH);
1688 if (rescaleQuality == kHigh_SkFilterQuality) {
1689 SkMatrix matrix;
1690 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
1691 std::unique_ptr<GrFragmentProcessor> fp;
Brian Salomona86fc7a2019-05-28 20:42:58 -04001692 auto dir = GrBicubicEffect::Direction::kXY;
1693 if (nextW == srcW) {
1694 dir = GrBicubicEffect::Direction::kY;
1695 } else if (nextH == srcH) {
1696 dir = GrBicubicEffect::Direction::kX;
1697 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04001698 if (srcW != texProxy->width() || srcH != texProxy->height()) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001699 auto domain = GrTextureDomain::MakeTexelDomain(
1700 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
Brian Salomon4d036892019-07-02 15:10:58 -04001701 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001702 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001703 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001704 }
Brian Salomon900dda82019-06-03 13:12:55 -04001705 if (xform) {
1706 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
1707 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001708 GrPaint paint;
1709 paint.addColorFragmentProcessor(std::move(fp));
1710 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001711 currRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
1712 dstRect, dstRect);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001713 } else {
1714 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
1715 : GrSamplerState::Filter::kBilerp;
1716 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Greg Daniel46cfbc62019-06-07 11:43:30 -04001717 currRTC->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE,
1718 srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint,
1719 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001720 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04001721 texProxy = currRTC->asTextureProxyRef();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001722 srcX = srcY = 0;
1723 srcW = nextW;
1724 srcH = nextH;
1725 constraint = SkCanvas::kFast_SrcRectConstraint;
1726 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04001727 SkASSERT(currRTC);
1728 return currRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001729}
1730
1731void GrRenderTargetContext::asyncRescaleAndReadPixels(
1732 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1733 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1734 auto direct = fContext->priv().asDirectContext();
1735 if (!direct) {
1736 callback(context, nullptr, 0);
1737 return;
1738 }
1739 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1740 callback(context, nullptr, 0);
1741 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001742 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001743 auto dstCT = SkColorTypeToGrColorType(info.colorType());
1744 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001745 auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001746 GrPixelConfig configOfFinalContext = fRenderTargetProxy->config();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001747 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001748 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001749 colorTypeOfFinalContext = dstCT;
Greg Daniel627d0532019-07-08 16:48:14 -04001750 backendFormatOfFinalContext = this->caps()->getBackendFormatFromColorType(dstCT);
Brian Salomonf30b1c12019-06-20 12:25:02 -04001751 configOfFinalContext = this->caps()->getConfigFromBackendFormat(backendFormatOfFinalContext,
Greg Daniel627d0532019-07-08 16:48:14 -04001752 dstCT);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001753 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001754 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
1755 configOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001756 backendFormatOfFinalContext, dstCT);
1757 // Fail if we can't read from the source surface's color type.
1758 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001759 callback(context, nullptr, 0);
1760 return;
1761 }
Brian Salomon624f9062019-07-02 14:23:00 -04001762 // Fail if read color type does not have all of dstCT's color channels and those missing color
1763 // channels are in the src.
1764 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1765 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
1766 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1767 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001768 callback(context, nullptr, 0);
1769 return;
1770 }
1771
1772 sk_sp<GrRenderTargetContext> rtc;
1773 int x = srcRect.fLeft;
1774 int y = srcRect.fTop;
1775 if (needsRescale) {
1776 rtc = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1777 if (!rtc) {
1778 callback(context, nullptr, 0);
1779 return;
1780 }
1781 SkASSERT(SkColorSpace::Equals(rtc->colorSpaceInfo().colorSpace(), info.colorSpace()));
1782 SkASSERT(rtc->origin() == kTopLeft_GrSurfaceOrigin);
1783 x = y = 0;
1784 } else {
1785 sk_sp<GrColorSpaceXform> xform =
Brian Salomon4d036892019-07-02 15:10:58 -04001786 GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(),
1787 this->colorSpaceInfo().alphaType(),
Brian Salomon031b0ba2019-05-23 11:05:26 -04001788 info.colorSpace(), info.alphaType());
1789 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1790 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001791 // We flip or color convert by drawing and we don't currently support drawing to
1792 // kPremul.
1793 if (info.alphaType() == kUnpremul_SkAlphaType) {
1794 callback(context, nullptr, 0);
1795 return;
1796 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001797 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001798 SkRect srcRectToDraw = SkRect::Make(srcRect);
1799 // If the src is not texturable first try to make a copy to a texture.
1800 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001801 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1802 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1803 SkBudgeted::kNo);
1804 if (!texProxy) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001805 callback(context, nullptr, 0);
1806 return;
1807 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001808 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1809 }
1810 rtc = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001811 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
1812 this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
1813 kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001814 if (!rtc) {
1815 callback(context, nullptr, 0);
1816 return;
1817 }
1818 rtc->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
1819 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
1820 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1821 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(),
1822 std::move(xform));
1823 x = y = 0;
1824 } else {
1825 rtc = sk_ref_sp(this);
1826 }
1827 }
Brian Salomon024bd002019-06-11 11:38:16 -04001828 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1829 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001830}
1831
Brian Salomon024bd002019-06-11 11:38:16 -04001832GrRenderTargetContext::PixelTransferResult GrRenderTargetContext::transferPixels(
1833 GrColorType dstCT, const SkIRect& rect) {
1834 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1835 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001836 auto direct = fContext->priv().asDirectContext();
1837 if (!direct) {
Brian Salomon024bd002019-06-11 11:38:16 -04001838 return {};
Brian Salomon031b0ba2019-05-23 11:05:26 -04001839 }
1840 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Brian Salomon024bd002019-06-11 11:38:16 -04001841 return {};
Brian Salomon031b0ba2019-05-23 11:05:26 -04001842 }
Brian Salomonf30b1c12019-06-20 12:25:02 -04001843 auto supportedRead = this->caps()->supportedReadPixelsColorType(
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001844 this->colorSpaceInfo().colorType(), fRenderTargetProxy->config(),
1845 fRenderTargetProxy->backendFormat(), dstCT);
Brian Salomon624f9062019-07-02 14:23:00 -04001846 // Fail if read color type does not have all of dstCT's color channels and those missing color
1847 // channels are in the src.
1848 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1849 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
1850 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType());
1851 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon024bd002019-06-11 11:38:16 -04001852 return {};
Brian Salomonab32f652019-05-10 14:24:50 -04001853 }
1854
Brian Salomon031b0ba2019-05-23 11:05:26 -04001855 if (!this->caps()->transferBufferSupport() ||
Brian Salomonf30b1c12019-06-20 12:25:02 -04001856 !this->caps()->transferFromOffsetAlignment(supportedRead.fColorType)) {
Brian Salomon024bd002019-06-11 11:38:16 -04001857 return {};
1858 }
1859
Brian Salomonf30b1c12019-06-20 12:25:02 -04001860 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
Brian Salomon024bd002019-06-11 11:38:16 -04001861 size_t size = rowBytes * rect.height();
1862 auto buffer = direct->priv().resourceProvider()->createBuffer(
1863 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
1864 if (!buffer) {
1865 return {};
1866 }
Brian Salomonf30b1c12019-06-20 12:25:02 -04001867 auto srcRect = rect;
1868 bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;
1869 if (flip) {
1870 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
1871 this->height() - rect.fTop);
1872 }
1873 auto op = GrTransferFromOp::Make(fContext, srcRect, supportedRead.fColorType, buffer, 0);
1874 this->getRTOpList()->addOp(std::move(op), *this->caps());
Brian Salomon024bd002019-06-11 11:38:16 -04001875 PixelTransferResult result;
1876 result.fTransferBuffer = std::move(buffer);
Brian Salomonf30b1c12019-06-20 12:25:02 -04001877 if (supportedRead.fColorType != dstCT || supportedRead.fSwizzle != GrSwizzle("rgba") || flip) {
1878 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead](
1879 void* dst, const void* src) {
Brian Salomon4d036892019-07-02 15:10:58 -04001880 // We're using kPremul here for src and dst simply because we don't want any alpha type
1881 // conversions.
Brian Salomon1d435302019-07-01 13:05:28 -04001882 GrPixelInfo srcInfo(supportedRead.fColorType, kPremul_SkAlphaType, nullptr, w, h);
1883 GrPixelInfo dstInfo(dstCT, kPremul_SkAlphaType, nullptr, w, h);
1884 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
1885 srcInfo, src, srcInfo.minRowBytes(),
1886 /* flipY = */ false, supportedRead.fSwizzle);
Brian Salomonf30b1c12019-06-20 12:25:02 -04001887 };
Brian Salomon024bd002019-06-11 11:38:16 -04001888 }
1889 return result;
1890}
1891
1892void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1893 ReadPixelsCallback callback,
1894 ReadPixelsContext context) {
1895 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1896 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1897
1898 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1899
1900 if (!transferResult.fTransferBuffer) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001901 SkAutoPixmapStorage pm;
Brian Salomon4d036892019-07-02 15:10:58 -04001902 auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType,
1903 this->colorSpaceInfo().alphaType(),
Brian Salomon024bd002019-06-11 11:38:16 -04001904 this->colorSpaceInfo().refColorSpace());
1905 pm.alloc(ii);
Brian Salomon1d435302019-07-01 13:05:28 -04001906 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon031b0ba2019-05-23 11:05:26 -04001907 callback(context, nullptr, 0);
Brian Salomonab32f652019-05-10 14:24:50 -04001908 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001909 callback(context, pm.addr(), pm.rowBytes());
1910 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001911 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001912
Brian Salomonab32f652019-05-10 14:24:50 -04001913 struct FinishContext {
1914 ReadPixelsCallback* fClientCallback;
1915 ReadPixelsContext fClientContext;
Brian Salomon024bd002019-06-11 11:38:16 -04001916 int fW, fH;
1917 SkColorType fColorType;
1918 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001919 };
1920 // Assumption is that the caller would like to flush. We could take a parameter or require an
1921 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1922 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon024bd002019-06-11 11:38:16 -04001923 auto* finishContext = new FinishContext{callback, context, rect.width(),
1924 rect.height(), colorType, std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001925 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001926 const auto* context = reinterpret_cast<const FinishContext*>(c);
1927 const void* data = context->fTransferResult.fTransferBuffer->map();
Brian Salomoncd734f62019-05-10 16:32:54 -04001928 if (!data) {
1929 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
1930 delete context;
1931 return;
1932 }
Brian Salomon4d036892019-07-02 15:10:58 -04001933 std::unique_ptr<char[]> tmp;
1934 size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType);
Brian Salomon024bd002019-06-11 11:38:16 -04001935 if (context->fTransferResult.fPixelConverter) {
Brian Salomon4d036892019-07-02 15:10:58 -04001936 tmp.reset(new char[rowBytes * context->fH]);
1937 context->fTransferResult.fPixelConverter(tmp.get(), data);
1938 data = tmp.get();
Brian Salomoncd734f62019-05-10 16:32:54 -04001939 }
Brian Salomon4d036892019-07-02 15:10:58 -04001940 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
Brian Salomon024bd002019-06-11 11:38:16 -04001941 delete context;
1942 };
1943 GrFlushInfo flushInfo;
1944 flushInfo.fFinishedContext = finishContext;
1945 flushInfo.fFinishedProc = finishCallback;
1946 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1947}
1948
1949void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(
1950 SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
1951 int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality,
1952 ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) {
1953 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1954 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
1955 SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0));
1956 auto direct = fContext->priv().asDirectContext();
1957 if (!direct) {
1958 callback(context, nullptr, nullptr);
1959 return;
1960 }
1961 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
1962 callback(context, nullptr, nullptr);
1963 return;
1964 }
1965 if (dstW & 0x1) {
1966 return;
1967 }
1968 int x = srcRect.fLeft;
1969 int y = srcRect.fTop;
1970 auto rtc = sk_ref_sp(this);
1971 bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH;
1972 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001973 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon024bd002019-06-11 11:38:16 -04001974 auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1975 dstColorSpace);
1976 // TODO: Incorporate the YUV conversion into last pass of rescaling.
1977 rtc = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1978 if (!rtc) {
1979 callback(context, nullptr, nullptr);
1980 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001981 }
Brian Salomon024bd002019-06-11 11:38:16 -04001982 SkASSERT(SkColorSpace::Equals(rtc->colorSpaceInfo().colorSpace(), info.colorSpace()));
1983 SkASSERT(rtc->origin() == kTopLeft_GrSurfaceOrigin);
1984 x = y = 0;
1985 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001986 // We assume the caller wants kPremul. There is no way to indicate a preference.
1987 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
1988 this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(),
1989 dstColorSpace.get(), kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001990 if (xform) {
1991 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1992 // TODO: Do something if the input is not a texture already.
1993 if (!texProxy) {
1994 callback(context, nullptr, nullptr);
1995 return;
1996 }
Brian Salomon024bd002019-06-11 11:38:16 -04001997 SkRect srcRectToDraw = SkRect::Make(srcRect);
1998 rtc = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001999 SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(),
2000 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002001 if (!rtc) {
2002 callback(context, nullptr, nullptr);
2003 return;
2004 }
2005 rtc->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest,
2006 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
2007 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
2008 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(),
2009 std::move(xform));
2010 x = y = 0;
2011 }
2012 }
2013 auto srcProxy = rtc->asTextureProxyRef();
2014 // TODO: Do something if the input is not a texture already.
2015 if (!srcProxy) {
2016 callback(context, nullptr, nullptr);
2017 return;
2018 }
Brian Salomon27ae52c2019-07-03 11:27:44 -04002019 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
2020 SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1,
2021 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
2022 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
2023 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
2024 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
2025 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
2026 SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1,
2027 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04002028 if (!yRTC || !uRTC || !vRTC) {
2029 callback(context, nullptr, nullptr);
2030 return;
2031 }
2032
2033 static constexpr float kRec601M[] {
2034 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y
2035 -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u
2036 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v
2037 };
2038 static constexpr float kRec709M[] {
2039 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y
2040 -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u
2041 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v
2042 };
2043 static constexpr float kJpegM[] {
2044 0.299f , 0.587f , 0.114f , 0.f / 255, // y
2045 -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u
2046 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v
2047 };
2048 static constexpr float kIM[] {
2049 1.f, 0.f, 0.f, 0.f,
2050 0.f, 1.f, 0.f, 0.f,
2051 0.f, 0.f, 1.f, 0.f,
2052 };
2053 const float* baseM = kIM;
2054 switch (yuvColorSpace) {
2055 case kRec601_SkYUVColorSpace:
2056 baseM = kRec601M;
2057 break;
2058 case kRec709_SkYUVColorSpace:
2059 baseM = kRec709M;
2060 break;
2061 case kJPEG_SkYUVColorSpace:
2062 baseM = kJpegM;
2063 break;
2064 case kIdentity_SkYUVColorSpace:
2065 baseM = kIM;
2066 break;
2067 }
2068 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
2069
2070 auto texMatrix = SkMatrix::MakeTrans(x, y);
2071
Michael Ludwig61328202019-06-19 14:48:58 +00002072 SkRect dstRectY = SkRect::MakeWH(dstW, dstH);
2073 SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2);
2074
Brian Salomon024bd002019-06-11 11:38:16 -04002075 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
2076 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002077 std::fill_n(yM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04002078 yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3];
2079 GrPaint yPaint;
2080 yPaint.addColorTextureProcessor(srcProxy, texMatrix);
2081 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
2082 yPaint.addColorFragmentProcessor(std::move(yFP));
2083 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002084 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
2085 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04002086 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
2087 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
2088 if (!yTransfer.fTransferBuffer) {
2089 callback(context, nullptr, nullptr);
2090 return;
2091 }
2092
2093 texMatrix.preScale(2.f, 2.f);
2094 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
2095 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002096 std::fill_n(uM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04002097 uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7];
2098 GrPaint uPaint;
2099 uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
2100 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
2101 uPaint.addColorFragmentProcessor(std::move(uFP));
2102 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002103 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
2104 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04002105 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
2106 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
2107 if (!uTransfer.fTransferBuffer) {
2108 callback(context, nullptr, nullptr);
2109 return;
2110 }
2111
2112 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
2113 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04002114 std::fill_n(vM, 15, 0.f);
Brian Salomon024bd002019-06-11 11:38:16 -04002115 vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11];
2116 GrPaint vPaint;
2117 vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp());
2118 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
2119 vPaint.addColorFragmentProcessor(std::move(vFP));
2120 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00002121 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
2122 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04002123 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
2124 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
2125 if (!vTransfer.fTransferBuffer) {
2126 callback(context, nullptr, nullptr);
2127 return;
2128 }
2129
2130 struct FinishContext {
2131 ReadPixelsCallbackYUV420* fClientCallback;
2132 ReadPixelsContext fClientContext;
2133 int fW, fH;
2134 PixelTransferResult fYTransfer;
2135 PixelTransferResult fUTransfer;
2136 PixelTransferResult fVTransfer;
2137 };
2138 // Assumption is that the caller would like to flush. We could take a parameter or require an
2139 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
2140 // callback to GrGpu until after the next flush that flushes our op list, though.
2141 auto* finishContext = new FinishContext{callback,
2142 context,
2143 dstW,
2144 dstH,
2145 std::move(yTransfer),
2146 std::move(uTransfer),
2147 std::move(vTransfer)};
2148 auto finishCallback = [](GrGpuFinishedContext c) {
2149 const auto* context = reinterpret_cast<const FinishContext*>(c);
2150 const void* y = context->fYTransfer.fTransferBuffer->map();
2151 const void* u = context->fUTransfer.fTransferBuffer->map();
2152 const void* v = context->fVTransfer.fTransferBuffer->map();
2153 if (!y || !u || !v) {
2154 if (y) {
2155 context->fYTransfer.fTransferBuffer->unmap();
2156 }
2157 if (u) {
2158 context->fUTransfer.fTransferBuffer->unmap();
2159 }
2160 if (v) {
2161 context->fVTransfer.fTransferBuffer->unmap();
2162 }
2163 (*context->fClientCallback)(context->fClientContext, nullptr, 0);
2164 delete context;
2165 return;
2166 }
2167 size_t w = SkToSizeT(context->fW);
2168 size_t h = SkToSizeT(context->fH);
2169 std::unique_ptr<uint8_t[]> yTemp;
2170 if (context->fYTransfer.fPixelConverter) {
2171 yTemp.reset(new uint8_t[w * h]);
2172 context->fYTransfer.fPixelConverter(yTemp.get(), y);
2173 y = yTemp.get();
2174 }
2175 std::unique_ptr<uint8_t[]> uTemp;
2176 if (context->fUTransfer.fPixelConverter) {
2177 uTemp.reset(new uint8_t[w / 2 * h / 2]);
2178 context->fUTransfer.fPixelConverter(uTemp.get(), u);
2179 u = uTemp.get();
2180 }
2181 std::unique_ptr<uint8_t[]> vTemp;
2182 if (context->fVTransfer.fPixelConverter) {
2183 vTemp.reset(new uint8_t[w / 2 * h / 2]);
2184 context->fVTransfer.fPixelConverter(vTemp.get(), v);
2185 v = vTemp.get();
2186 }
2187 const void* data[] = {y, u, v};
2188 size_t rowBytes[] = {w, w / 2, w / 2};
2189 (*context->fClientCallback)(context->fClientContext, data, rowBytes);
2190 context->fYTransfer.fTransferBuffer->unmap();
2191 context->fUTransfer.fTransferBuffer->unmap();
2192 context->fVTransfer.fTransferBuffer->unmap();
Brian Salomonab32f652019-05-10 14:24:50 -04002193 delete context;
2194 };
2195 GrFlushInfo flushInfo;
2196 flushInfo.fFinishedContext = finishContext;
2197 flushInfo.fFinishedProc = finishCallback;
2198 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04002199}
2200
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002201GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
2202 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07002203 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05002204 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05002205 return GrSemaphoresSubmitted::kNo;
2206 }
robertphillips8c523e02016-07-26 07:41:00 -07002207 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04002208 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07002209
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002210 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04002211}
2212
Greg Danielc64ee462017-06-15 16:59:49 -04002213bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002214 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04002215 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04002216 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04002217 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002218 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04002219
2220 AutoCheckFlush acf(this->drawingManager());
2221
Brian Salomon9ff5acb2019-05-08 09:04:47 -04002222 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04002223 return false;
2224 }
2225
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002226 auto direct = fContext->priv().asDirectContext();
2227 if (!direct) {
2228 return false;
2229 }
2230
2231 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05002232
Greg Daniela5cb7812017-06-16 09:45:32 -04002233 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05002234 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05002235 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
2236 kAdopt_GrWrapOwnership);
Robert Phillipsbc4994a2019-02-14 08:36:56 -05002237 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, std::move(sema),
Robert Phillips7c525e62018-06-12 10:11:12 -04002238 fRenderTargetProxy.get()));
Greg Danielcb324152019-02-25 11:36:53 -05002239 this->getRTOpList()->addWaitOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04002240 }
Greg Danielc64ee462017-06-15 16:59:49 -04002241 return true;
robertphillips8c523e02016-07-26 07:41:00 -07002242}
joshualitt33a5fce2015-11-18 13:28:51 -08002243
Robert Phillips65a88fa2017-08-08 08:36:22 -04002244void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04002245 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04002246 this->getRTOpList()->addOp(std::move(op), *this->caps());
2247}
2248
Robert Phillipsbe9aff22019-02-15 11:33:22 -05002249const GrCaps* GrRenderTargetContext::caps() const {
2250 return fContext->priv().caps();
2251}
2252
Brian Osman11052242016-10-27 14:47:55 -04002253void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05002254 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002255 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002256 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05002257 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04002258 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08002259 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002260 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07002261 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04002262 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
2263
Brian Salomon40b77a62017-12-22 11:25:52 -05002264 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04002265
Robert Phillips27927a52018-08-20 13:18:12 -04002266 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002267}
2268
2269void GrRenderTargetContext::drawShape(const GrClip& clip,
2270 GrPaint&& paint,
2271 GrAA aa,
2272 const SkMatrix& viewMatrix,
2273 const GrShape& shape) {
2274 ASSERT_SINGLE_OWNER
2275 RETURN_IF_ABANDONED
2276 SkDEBUGCODE(this->validate();)
2277 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2278
Brian Salomon2fad74a2017-12-20 13:28:55 -05002279 if (shape.isEmpty()) {
2280 if (shape.inverseFilled()) {
2281 this->drawPaint(clip, std::move(paint), viewMatrix);
2282 }
2283 return;
robertphillipsea461502015-05-26 11:38:03 -07002284 }
2285
Robert Phillips72152832017-01-25 17:31:35 -05002286 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002287
Brian Salomon2fad74a2017-12-20 13:28:55 -05002288 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002289 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002290 SkRRect rrect;
2291 // We can ignore the starting point and direction since there is no path effect.
2292 bool inverted;
2293 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2294 if (rrect.isRect()) {
2295 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2296 &shape.style());
2297 return;
2298 } else if (rrect.isOval()) {
2299 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002300 return;
2301 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002302 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2303 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002304 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2305 viewMatrix.rectStaysRect()) {
2306 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2307 // the matrix to all the points individually rather than just to the rect
2308 SkRect rects[2];
2309 if (shape.asNestedRects(rects)) {
2310 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002311 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002312 fContext, std::move(paint), viewMatrix, rects);
2313 if (op) {
2314 this->addDrawOp(clip, std::move(op));
2315 }
2316 // Returning here indicates that there is nothing to draw in this case.
2317 return;
2318 }
robertphillipsea461502015-05-26 11:38:03 -07002319 }
2320 }
robertphillips4bc31812016-03-01 12:22:49 -08002321
Brian Salomon2fad74a2017-12-20 13:28:55 -05002322 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002323}
2324
Chris Daltonbbfd5162017-11-07 13:35:22 -07002325bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002326 const GrUserStencilSettings* ss,
2327 SkRegion::Op op,
2328 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002329 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002330 const SkMatrix& viewMatrix,
2331 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002332 ASSERT_SINGLE_OWNER_PRIV
2333 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002334 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002335 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2336 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002337
2338 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002339 GrPaint paint;
2340 paint.setCoverageSetOpXPFactory(op, invert);
2341 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2342 SkRect::MakeIWH(fRenderTargetContext->width(),
2343 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002344 return true;
2345 }
2346
Robert Phillips72152832017-01-25 17:31:35 -05002347 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002348
2349 // An Assumption here is that path renderer would use some form of tweaking
2350 // the src color (either the input alpha or in the frag shader) to implement
2351 // aa. If we have some future driver-mojo path AA that can do the right
2352 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002353 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002354 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002355
Chris Daltondb91c6e2017-09-08 16:25:08 -06002356 SkIRect clipConservativeBounds;
2357 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2358 &clipConservativeBounds, nullptr);
2359
bsalomon8acedde2016-06-24 10:42:16 -07002360 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002361 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002362 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002363 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002364 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002365 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002366 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002367 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002368 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2369 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002370 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002371
2372 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002373 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002374 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002375 if (!pr) {
2376 return false;
2377 }
2378
2379 GrPaint paint;
2380 paint.setCoverageSetOpXPFactory(op, invert);
2381
Brian Salomonf3569f02017-10-24 12:52:33 -04002382 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2383 std::move(paint),
2384 ss,
2385 fRenderTargetContext,
2386 &clip,
2387 &clipConservativeBounds,
2388 &viewMatrix,
2389 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002390 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002391 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002392 pr->drawPath(args);
2393 return true;
2394}
2395
Brian Osman11052242016-10-27 14:47:55 -04002396SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002397 ASSERT_SINGLE_OWNER_PRIV
2398
Robert Phillips6a6de562019-02-15 15:19:15 -05002399 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002400 return SkBudgeted::kNo;
2401 }
2402
Brian Osman11052242016-10-27 14:47:55 -04002403 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002404
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002405 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002406}
2407
Brian Salomon2fad74a2017-12-20 13:28:55 -05002408void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2409 GrPaint&& paint,
2410 GrAA aa,
2411 const SkMatrix& viewMatrix,
2412 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002413 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002414 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002415 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2416
Jim Van Verthf86073a2018-10-02 13:05:38 -04002417 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2418 return;
2419 }
2420
Chris Daltondb91c6e2017-09-08 16:25:08 -06002421 SkIRect clipConservativeBounds;
2422 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2423
Brian Salomon2fad74a2017-12-20 13:28:55 -05002424 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002425 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002426
robertphillips68737822015-10-29 12:12:21 -07002427 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002428 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002429 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002430 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002431 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002432 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002433 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002434 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002435
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002436 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002437 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002438 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002439 return;
2440 }
2441
Chris Dalton6ce447a2019-06-23 18:07:38 -06002442 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002443
2444 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2445 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2446 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2447
Brian Salomon2fad74a2017-12-20 13:28:55 -05002448 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002449 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002450 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2451 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002452 return;
2453 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002454 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002455 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002456 }
2457 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002458 if (canDrawArgs.fShape->style().applies()) {
2459 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2460 styleScale);
2461 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002462 return;
2463 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002464 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002465 // This time, allow SW renderer
2466 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2467 } else {
2468 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002469 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002470 }
robertphillipsea461502015-05-26 11:38:03 -07002471
bsalomon8acedde2016-06-24 10:42:16 -07002472 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002473#ifdef SK_DEBUG
2474 SkDebugf("Unable to find path renderer compatible with path.\n");
2475#endif
2476 return;
2477 }
2478
Robert Phillips256c37b2017-03-01 14:32:46 -05002479 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002480 std::move(paint),
2481 &GrUserStencilSettings::kUnused,
2482 this,
2483 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002484 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002485 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002486 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002487 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04002488 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002489 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002490}
2491
Brian Salomon467921e2017-03-06 16:17:12 -05002492static void op_bounds(SkRect* bounds, const GrOp* op) {
2493 *bounds = op->bounds();
2494 if (op->hasZeroArea()) {
2495 if (op->hasAABloat()) {
2496 bounds->outset(0.5f, 0.5f);
2497 } else {
2498 // We don't know which way the particular GPU will snap lines or points at integer
2499 // coords. So we ensure that the bounds is large enough for either snap.
2500 SkRect before = *bounds;
2501 bounds->roundOut(bounds);
2502 if (bounds->fLeft == before.fLeft) {
2503 bounds->fLeft -= 1;
2504 }
2505 if (bounds->fTop == before.fTop) {
2506 bounds->fTop -= 1;
2507 }
2508 if (bounds->fRight == before.fRight) {
2509 bounds->fRight += 1;
2510 }
2511 if (bounds->fBottom == before.fBottom) {
2512 bounds->fBottom += 1;
2513 }
2514 }
2515 }
2516}
2517
Brian Salomon348a0372018-10-31 10:42:18 -04002518void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2519 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002520 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002521 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002522 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002523 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002524 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002525 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002526 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002527 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002528
Brian Salomon467921e2017-03-06 16:17:12 -05002529 // Setup clip
2530 SkRect bounds;
2531 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002532 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002533 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002534 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
2535 bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
2536
2537 if (usesStencil) {
Chris Daltoneffee202019-07-01 22:28:03 -06002538 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002539 }
2540
2541 if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002542 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002543 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002544 }
2545
Chris Daltoneffee202019-07-01 22:28:03 -06002546 SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0));
Brian Salomon54d212e2017-03-21 14:22:38 -04002547
Brian Salomonbd3d8d32019-07-02 09:16:28 -04002548 GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType());
Chris Daltoneffee202019-07-01 22:28:03 -06002549 // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip
2550 // here as well.
2551 bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1);
2552#ifdef SK_DEBUG
2553 if (hasMixedSampledCoverage) {
2554 SkASSERT(usesStencil);
2555 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
2556 }
2557#endif
Brian Osman5ced0bf2019-03-15 10:15:29 -04002558 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002559 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2560
2561 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07002562 if (analysis.requiresDstTexture()) {
Brian Salomon09181ef2018-11-14 13:39:51 -05002563 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002564 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002565 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002566 }
2567 }
2568
2569 op->setClippedBounds(bounds);
Brian Salomon348a0372018-10-31 10:42:18 -04002570 auto opList = this->getRTOpList();
2571 if (willAddFn) {
2572 willAddFn(op.get(), opList->uniqueID());
2573 }
Chris Dalton945ee652019-01-23 09:10:36 -07002574 opList->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy, *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002575}
2576
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002577bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomond6287472019-06-24 15:50:07 -04002578 const GrOp& op, GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002579 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2580 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2581 // start and stop the render pass in order to make the copy.
2582 if (rtProxy->wrapsVkSecondaryCB()) {
2583 return false;
2584 }
2585
Brian Salomon467921e2017-03-06 16:17:12 -05002586 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04002587 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002588 // The render target is a texture, so we can read from it directly in the shader. The XP
2589 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002590 dstProxy->setProxy(sk_ref_sp(texProxy));
2591 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002592 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002593 }
2594 }
2595
Robert Phillipsbf25d432017-04-07 10:08:53 -04002596 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002597
Eric Karl74480882017-04-03 14:49:05 -07002598 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002599 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002600 SkRect opBounds = op.bounds();
2601 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2602 // 0.5 pixels.
2603 if (op.hasAABloat() || op.hasZeroArea()) {
2604 opBounds.outset(0.5f, 0.5f);
2605 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2606 // performance we may ignore the clip when the draw is entirely inside the clip is float
2607 // space but will hit pixels just outside the clip when actually rasterizing.
2608 clippedRect.outset(1, 1);
2609 clippedRect.intersect(SkIRect::MakeWH(rtProxy->width(), rtProxy->height()));
2610 }
2611 SkIRect opIBounds;
2612 opBounds.roundOut(&opIBounds);
2613 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002614#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002615 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002616#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002617 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002618 }
2619
2620 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2621 // have per-sample dst values by making the copy multisampled.
Brian Salomonf2c2ba92019-07-17 09:59:59 -04002622 // TODO: We don't really use renderability or GrSurfaceDesc. Remove them from caps function?
Brian Salomon467921e2017-03-06 16:17:12 -05002623 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07002624 bool rectsMustMatch = false;
2625 bool disallowSubrect = false;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04002626 GrRenderable renderable = GrRenderable::kNo;
2627 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &renderable, &rectsMustMatch,
Brian Salomon2a4f9832018-03-03 22:43:43 -05002628 &disallowSubrect)) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -04002629 renderable = GrRenderable::kYes;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002630 desc.fConfig = rtProxy->config();
Brian Salomon467921e2017-03-06 16:17:12 -05002631 }
2632
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002633 desc.fIsProtected = rtProxy->isProtected() ? GrProtected::kYes : GrProtected::kNo;
2634
Eric Karl74480882017-04-03 14:49:05 -07002635 if (!disallowSubrect) {
2636 copyRect = clippedRect;
2637 }
Brian Salomon467921e2017-03-06 16:17:12 -05002638
Robert Phillipsbf25d432017-04-07 10:08:53 -04002639 SkIPoint dstPoint, dstOffset;
2640 SkBackingFit fit;
Greg Daniel46cfbc62019-06-07 11:43:30 -04002641 GrSurfaceProxy::RectsMustMatch matchRects;
Eric Karl74480882017-04-03 14:49:05 -07002642 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04002643 desc.fWidth = rtProxy->width();
2644 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07002645 dstPoint = {copyRect.fLeft, copyRect.fTop};
2646 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002647 fit = SkBackingFit::kExact;
Greg Daniel46cfbc62019-06-07 11:43:30 -04002648 matchRects = GrSurfaceProxy::RectsMustMatch::kYes;
Eric Karl74480882017-04-03 14:49:05 -07002649 } else {
2650 desc.fWidth = copyRect.width();
2651 desc.fHeight = copyRect.height();
2652 dstPoint = {0, 0};
2653 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002654 fit = SkBackingFit::kApprox;
Greg Daniel46cfbc62019-06-07 11:43:30 -04002655 matchRects = GrSurfaceProxy::RectsMustMatch::kNo;
Eric Karl74480882017-04-03 14:49:05 -07002656 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04002657 sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(fContext, rtProxy, GrMipMapped::kNo,
2658 copyRect, fit, SkBudgeted::kYes,
2659 matchRects);
2660 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002661
Greg Daniel46cfbc62019-06-07 11:43:30 -04002662 dstProxy->setProxy(std::move(newProxy));
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002663 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002664 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002665}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002666
2667bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2668 const SkIPoint& dstPoint) {
2669 SkIRect clippedSrcRect;
2670 SkIPoint clippedDstPoint;
2671 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint,
2672 &clippedSrcRect, &clippedDstPoint)) {
2673 return false;
2674 }
2675
2676 GrPaint paint;
2677 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
2678 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()),
2679 SkMatrix::I());
2680 if (!fp) {
2681 return false;
2682 }
2683 paint.addColorFragmentProcessor(std::move(fp));
2684
2685 this->fillRectToRect(
2686 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2687 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2688 clippedSrcRect.height()),
2689 SkRect::Make(clippedSrcRect));
2690 return true;
2691}
2692