blob: 21bfebd94ad15e1dff9e3dee99ec518d1a0edf1f [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 Osman11052242016-10-27 14:47:55 -04008#include "GrRenderTargetContext.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -04009#include "../private/GrAuditTrail.h"
Jim Van Verth3af1af92017-05-18 15:06:54 -040010#include "../private/SkShadowFlags.h"
Brian Salomon467921e2017-03-06 16:17:12 -050011#include "GrAppliedClip.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040013#include "GrBlurUtils.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050014#include "GrColor.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040015#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070016#include "GrDrawingManager.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070017#include "GrFixedClip.h"
robertphillips714712b2016-08-04 06:20:45 -070018#include "GrGpuResourcePriv.h"
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050019#include "GrMemoryPool.h"
Robert Phillips9d6c64f2017-09-14 10:56:45 -040020#include "GrOpList.h"
robertphillipsea461502015-05-26 11:38:03 -070021#include "GrPathRenderer.h"
Brian Salomon57caa662017-10-18 12:21:05 +000022#include "GrQuad.h"
robertphillips2334fb62015-06-17 05:43:33 -070023#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050024#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070025#include "GrResourceProvider.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040026#include "GrShape.h"
Brian Salomon467921e2017-03-06 16:17:12 -050027#include "GrStencilAttachment.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040028#include "GrStyle.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040029#include "GrTracing.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040030#include "SkDrawable.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040031#include "SkDrawShadowInfo.h"
Herb Derby8378dfb2018-08-30 14:50:04 -040032#include "SkGlyphRunPainter.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040033#include "SkGr.h"
Brian Salomon467921e2017-03-06 16:17:12 -050034#include "SkLatticeIter.h"
35#include "SkMatrixPriv.h"
Mike Reed242135a2018-02-22 13:41:39 -050036#include "SkRRectPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040037#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070038#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050039#include "effects/GrRRectEffect.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040040#include "ops/GrAtlasTextOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050041#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040042#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040043#include "ops/GrDebugMarkerOp.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040044#include "ops/GrDrawableOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000045#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040046#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050047#include "ops/GrDrawVerticesOp.h"
Michael Ludwig69858532018-11-28 15:34:34 -050048#include "ops/GrFillRectOp.h"
Chris Dalton133944a2018-11-16 23:30:29 -050049#include "ops/GrAAFillRRectOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050050#include "ops/GrLatticeOp.h"
51#include "ops/GrOp.h"
52#include "ops/GrOvalOpFactory.h"
Brian Salomon89527432016-12-16 09:52:16 -050053#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040054#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050055#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050056#include "ops/GrStencilPathOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050057#include "ops/GrStrokeRectOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040058#include "ops/GrTextureOp.h"
Herb Derby26cbe512018-05-24 14:39:01 -040059#include "text/GrTextContext.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040060#include "text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040061
Herb Derbyc1b482c2018-08-09 15:02:27 -040062class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040063public:
64 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040065 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomonf18b1d82017-10-27 11:30:49 -040066 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040067 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040068 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040069
Robert Phillips7c525e62018-06-12 10:11:12 -040070 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040071 fRenderTargetContext->addDrawOp(clip, std::move(op));
72 }
73
Robert Phillips46a13382018-08-23 13:53:01 -040074 void drawShape(const GrClip& clip, const SkPaint& paint,
75 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040076 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
77 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040078 }
79
80 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040081 GrPaint* grPaint) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040082 GrContext* context = fRenderTargetContext->fContext;
83 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
84 if (kARGB_GrMaskFormat == maskFormat) {
85 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
86 } else {
87 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
88 }
89 }
90
Robert Phillips7c525e62018-06-12 10:11:12 -040091 GrContext* getContext() override {
92 return fRenderTargetContext->fContext;
93 }
94
Herb Derby65956872018-08-21 16:55:04 -040095 SkGlyphRunListPainter* glyphPainter() override {
96 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -040097 }
98
Brian Salomonf18b1d82017-10-27 11:30:49 -040099private:
100 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400101 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400102
Brian Salomonf18b1d82017-10-27 11:30:49 -0400103};
joshualittbc907352016-01-13 06:45:40 -0800104
Robert Phillips72152832017-01-25 17:31:35 -0500105#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800106#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400107 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800108#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400109 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -0500110#define RETURN_IF_ABANDONED if (fContext->abandoned()) { return; }
111#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->abandoned()) { return; }
112#define RETURN_FALSE_IF_ABANDONED if (fContext->abandoned()) { return false; }
113#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->abandoned()) { return false; }
114#define RETURN_NULL_IF_ABANDONED if (fContext->abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700115
Brian Salomone225b562017-06-14 13:00:03 -0400116//////////////////////////////////////////////////////////////////////////////
117
118GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
119 const GrCaps& caps) {
120 if (GrAA::kNo == aa) {
121 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
122 // that.
123 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
124 return GrAAType::kMSAA;
125 }
126 return GrAAType::kNone;
127 }
128 switch (fsaaType) {
129 case GrFSAAType::kNone:
130 return GrAAType::kCoverage;
131 case GrFSAAType::kUnifiedMSAA:
132 return GrAAType::kMSAA;
133 case GrFSAAType::kMixedSamples:
134 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
135 : GrAAType::kCoverage;
136 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400137 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400138 return GrAAType::kNone;
139}
140
141//////////////////////////////////////////////////////////////////////////////
142
robertphillipsea461502015-05-26 11:38:03 -0700143class AutoCheckFlush {
144public:
halcanary9d524f22016-03-29 09:03:52 -0700145 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700146 SkASSERT(fDrawingManager);
147 }
bsalomonb77a9072016-09-07 10:02:04 -0700148 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700149
150private:
robertphillips77a2e522015-10-17 07:43:27 -0700151 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700152};
153
Robert Phillipsf2361d22016-10-25 14:20:06 -0400154// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400155// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400156// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400157// when the renderTargetContext attempts to use it (via getOpList).
158GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
159 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400160 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400161 sk_sp<SkColorSpace> colorSpace,
162 const SkSurfaceProps* surfaceProps,
163 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400164 GrSingleOwner* singleOwner,
165 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400166 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
167 singleOwner)
168 , fRenderTargetProxy(std::move(rtp))
169 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400170 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
171 , fManagedOpList(managedOpList) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500172 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
Robert Phillips4150eea2018-02-07 17:08:21 -0500173 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
174 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
175 // world we need to get the correct opList here so that it, in turn, can grab and hold
176 // its rendertarget.
177 this->getRTOpList();
178 }
179
Brian Salomonf18b1d82017-10-27 11:30:49 -0400180 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700181 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700182}
183
robertphillips2e1e51f2015-10-15 08:01:48 -0700184#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400185void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400186 SkASSERT(fRenderTargetProxy);
187 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700188
Robert Phillipsf2361d22016-10-25 14:20:06 -0400189 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400190 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700191 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700192}
193#endif
194
Brian Osman11052242016-10-27 14:47:55 -0400195GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800196 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700197}
198
Robert Phillipsf200a902017-01-30 13:27:37 -0500199GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000200 return fRenderTargetProxy->asTextureProxy();
201}
202
Greg Daniele252f082017-10-23 16:05:23 -0400203const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
204 return fRenderTargetProxy->asTextureProxy();
205}
206
Robert Phillipsf200a902017-01-30 13:27:37 -0500207sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
208 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
209}
210
Greg Daniele252f082017-10-23 16:05:23 -0400211GrMipMapped GrRenderTargetContext::mipMapped() const {
212 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
213 return proxy->mipMapped();
214 }
215 return GrMipMapped::kNo;
216}
217
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400218GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800219 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700220 SkDEBUGCODE(this->validate();)
221
Robert Phillipsf2361d22016-10-25 14:20:06 -0400222 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400223 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700224 }
225
Robert Phillipsdc83b892017-04-13 12:23:54 -0400226 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700227}
228
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400229GrOpList* GrRenderTargetContext::getOpList() {
230 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700231}
232
Herb Derbycddab252018-07-16 11:19:04 -0400233void GrRenderTargetContext::drawGlyphRunList(
234 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400235 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800236 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700237 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700238 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400239 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700240
Greg Danielbe7fc462019-01-03 16:40:42 -0500241 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
242 // secondary command buffers because it would require stopping and starting a render pass which
243 // we don't have access to.
244 if (this->wrapsVkSecondaryCB()) {
245 return;
246 }
247
Herb Derby26cbe512018-05-24 14:39:01 -0400248 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400249 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400250 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700251}
252
Brian Osman11052242016-10-27 14:47:55 -0400253void GrRenderTargetContext::discard() {
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();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400257 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700258
Robert Phillips72152832017-01-25 17:31:35 -0500259 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400260
Robert Phillips380b90c2017-08-30 07:41:07 -0400261 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700262}
263
Brian Osman11052242016-10-27 14:47:55 -0400264void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500265 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700266 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800267 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700268 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700269 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400270 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700271
Robert Phillips72152832017-01-25 17:31:35 -0500272 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700273 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
274 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700275}
robertphillips9199a9f2016-07-13 07:48:43 -0700276
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500277void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
278 const SkPMColor4f& color,
279 CanClearFullscreen canClearFullscreen) {
280 ASSERT_SINGLE_OWNER_PRIV
281 RETURN_IF_ABANDONED_PRIV
282 SkDEBUGCODE(fRenderTargetContext->validate();)
283 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
284 fRenderTargetContext->fContext);
285
286 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
287 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
288}
289
290static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
291 paint->setColor4f(color);
292 if (color.isOpaque()) {
293 // Can just rely on the src-over blend mode to do the right thing
294 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
295 } else {
296 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
297 // were src blended
298 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
299 }
300}
301
302void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
303 const SkPMColor4f& color,
304 CanClearFullscreen canClearFullscreen) {
305 bool isFull = false;
306 if (!clip.hasWindowRectangles()) {
307 isFull = !clip.scissorEnabled() ||
308 (CanClearFullscreen::kYes == canClearFullscreen &&
309 this->caps()->preferFullscreenClears()) ||
310 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
311 }
312
313 if (isFull) {
314 if (this->getRTOpList()->resetForFullscreenClear() &&
315 !this->caps()->performColorClearsAsDraws()) {
316 // The op list was emptied and native clears are allowed, so just use the load op
317 this->getRTOpList()->setColorLoadOp(GrLoadOp::kClear, color);
318 return;
319 } else {
320 // Will use an op for the clear, reset the load op to discard since the op will
321 // blow away the color buffer contents
322 this->getRTOpList()->setColorLoadOp(GrLoadOp::kDiscard);
323 }
324
325 // Must add an op to the list (either because we couldn't use a load op, or because the
326 // clear load op isn't supported)
327 if (this->caps()->performColorClearsAsDraws()) {
328 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
329 GrPaint paint;
330 clear_to_grpaint(color, &paint);
331 this->addDrawOp(GrFixedClip::Disabled(),
332 GrFillRectOp::Make(fContext, std::move(paint),
333 GrAAType::kNone, SkMatrix::I(), rtRect));
334 } else {
335 this->getRTOpList()->addOp(GrClearOp::Make(fContext, SkIRect::MakeEmpty(), color,
336 /* fullscreen */ true), *this->caps());
337 }
338 } else {
339 if (this->caps()->performPartialClearsAsDraws()) {
340 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
341 SkRect scissor = SkRect::Make(clip.scissorRect());
342 GrPaint paint;
343 clear_to_grpaint(color, &paint);
344
345 this->addDrawOp(clip, GrFillRectOp::Make(fContext, std::move(paint), GrAAType::kNone,
346 SkMatrix::I(), scissor));
347 } else {
348 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
349 this->asSurfaceProxy()));
350 // This version of the clear op factory can return null if the clip doesn't intersect
351 // with the surface proxy's boundary
352 if (!op) {
353 return;
354 }
355 this->getRTOpList()->addOp(std::move(op), *this->caps());
356 }
357 }
358}
359
Brian Osman9a9baae2018-11-05 15:06:26 -0500360void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const SkPMColor4f& color) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500361 ASSERT_SINGLE_OWNER_PRIV
362 RETURN_IF_ABANDONED_PRIV
363 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400364 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
365 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500366
Robert Phillips72152832017-01-25 17:31:35 -0500367 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500368
Brian Salomonbb5711a2017-05-17 13:49:59 -0400369 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
370 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500371
372 if (clearRect) {
373 if (clearRect->contains(rtRect)) {
374 clearRect = nullptr; // full screen
375 } else {
376 if (!rtRect.intersect(*clearRect)) {
377 return;
378 }
379 }
380 }
381
382 // TODO: in a post-MDB world this should be handled at the OpList level.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500383 // This makes sure to always add an op to the list, instead of marking the clear as a load op.
384 // This code follows very similar logic to internalClear() below, but critical differences are
385 // highlighted in line related to absClear()'s unique behavior.
386 if (clearRect) {
387 if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400388 GrPaint paint;
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500389 clear_to_grpaint(color, &paint);
390
391 // Use the disabled clip; the rect geometry already matches the clear rectangle and
392 // if it were added to a scissor, that would be intersected with the logical surface
393 // bounds and not the worst case dimensions required here.
394 fRenderTargetContext->addDrawOp(GrFixedClip::Disabled(),
395 GrFillRectOp::Make(fRenderTargetContext->fContext,
396 std::move(paint),
397 GrAAType::kNone,
398 SkMatrix::I(),
399 SkRect::Make(rtRect)));
400 } else {
401 // Must use the ClearOp factory that takes a boolean (false) instead of a surface
402 // proxy. The surface proxy variant would intersect the clip rect with its logical
403 // bounds, which is not desired in this special case.
404 fRenderTargetContext->getRTOpList()->addOp(
405 GrClearOp::Make(fRenderTargetContext->fContext, rtRect, color,
406 /* fullscreen */ false),
407 *fRenderTargetContext->caps());
csmartdalton29df7602016-08-31 11:55:52 -0700408 }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500409 } else {
410 // Reset the oplist like in internalClear(), but do not rely on a load op for the clear
411 fRenderTargetContext->getRTOpList()->resetForFullscreenClear();
412 fRenderTargetContext->getRTOpList()->setColorLoadOp(GrLoadOp::kDiscard);
413
414 if (fRenderTargetContext->caps()->performColorClearsAsDraws()) {
415 // This draws a quad covering the worst case dimensions instead of just the logical
416 // width and height like in internalClear().
417 GrPaint paint;
418 clear_to_grpaint(color, &paint);
419 fRenderTargetContext->addDrawOp(GrFixedClip::Disabled(),
420 GrFillRectOp::Make(fRenderTargetContext->fContext,
421 std::move(paint),
422 GrAAType::kNone,
423 SkMatrix::I(),
424 SkRect::Make(rtRect)));
425 } else {
426 // Nothing special about this path in absClear compared to internalClear()
427 fRenderTargetContext->getRTOpList()->addOp(
428 GrClearOp::Make(fRenderTargetContext->fContext, SkIRect::MakeEmpty(), color,
429 /* fullscreen */ true),
430 *fRenderTargetContext->caps());
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400431 }
robertphillips9199a9f2016-07-13 07:48:43 -0700432 }
robertphillipsea461502015-05-26 11:38:03 -0700433}
434
Brian Osman11052242016-10-27 14:47:55 -0400435void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500436 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400437 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800438 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700439 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700440 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400441 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700442
robertphillipsea461502015-05-26 11:38:03 -0700443 // set rect to be big enough to fill the space, but not super-huge, so we
444 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700445
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400446 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700447
Michael Ludwig61a16512019-01-15 11:15:13 -0500448 // Check if we can optimize a clipped drawPaint(). We only do the transformation when there are
449 // no fragment processors because they may depend on having correct local coords and this path
450 // draws in device space without a local matrix. It currently handles converting clipRRect()
451 // to drawRRect() and solid colors to screen-filling drawRects() (which are then converted into
452 // clears if possible in drawRect).
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400453 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61a16512019-01-15 11:15:13 -0500454 SkRRect rrect;
455 GrAA aa = GrAA::kNo;
456 if (clip.isRRect(r, &rrect, &aa)) {
457 if (rrect.isRect()) {
458 // Use drawFilledRect() with no clip and the reduced rectangle
459 this->drawFilledRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect.rect());
460 } else {
461 // Use drawRRect() with no clip
462 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
463 GrStyle::SimpleFill());
464 }
465 } else {
466 // Use drawFilledRect() with no view matrix to draw a fullscreen quad, but preserve
467 // the clip. Since the paint has no FPs we can drop the view matrix without worrying
468 // about local coordinates. If the clip is simple, drawFilledRect() will turn this into
469 // a clear or a scissored clear.
470 this->drawFilledRect(clip, std::move(paint), aa, SkMatrix::I(), r);
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400471 }
Michael Ludwig61a16512019-01-15 11:15:13 -0500472 return;
bsalomoncb31e512016-08-26 10:48:19 -0700473 }
474
Michael Ludwig61a16512019-01-15 11:15:13 -0500475 // Since the paint is not trivial, there's no way at this point drawRect() could have converted
476 // this drawPaint() into an optimized clear. drawRect() would then use GrFillRectOp without
477 // a local matrix, so we can simplify things and use the local matrix variant to draw a screen
478 // filling rect with the inverse view matrix for local coords, which works for all matrix
479 // conditions.
480 SkMatrix localMatrix;
481 if (!viewMatrix.invert(&localMatrix)) {
482 return;
robertphillipsea461502015-05-26 11:38:03 -0700483 }
Michael Ludwig61a16512019-01-15 11:15:13 -0500484
485 AutoCheckFlush acf(this->drawingManager());
486 std::unique_ptr<GrDrawOp> op = GrFillRectOp::MakeWithLocalMatrix(
487 fContext, std::move(paint), GrAAType::kNone, SkMatrix::I(), localMatrix, r);
488 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700489}
490
robertphillipsea461502015-05-26 11:38:03 -0700491static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
492 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
493 point.fY >= rect.fTop && point.fY <= rect.fBottom;
494}
495
csmartdalton97f6cd52016-07-13 13:37:08 -0700496// Attempts to crop a rect and optional local rect to the clip boundaries.
497// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700498static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700499 const SkMatrix& viewMatrix, SkRect* rect,
500 SkRect* localRect = nullptr) {
501 if (!viewMatrix.rectStaysRect()) {
502 return true;
503 }
504
csmartdalton97f6cd52016-07-13 13:37:08 -0700505 SkIRect clipDevBounds;
506 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700507
robertphillips13a7eee2016-08-31 15:06:24 -0700508 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700509 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
510 return false;
511 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700512
513 if (localRect) {
514 if (!rect->intersects(clipBounds)) {
515 return false;
516 }
517 const SkScalar dx = localRect->width() / rect->width();
518 const SkScalar dy = localRect->height() / rect->height();
519 if (clipBounds.fLeft > rect->fLeft) {
520 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
521 rect->fLeft = clipBounds.fLeft;
522 }
523 if (clipBounds.fTop > rect->fTop) {
524 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
525 rect->fTop = clipBounds.fTop;
526 }
527 if (clipBounds.fRight < rect->fRight) {
528 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
529 rect->fRight = clipBounds.fRight;
530 }
531 if (clipBounds.fBottom < rect->fBottom) {
532 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
533 rect->fBottom = clipBounds.fBottom;
534 }
535 return true;
536 }
537
538 return rect->intersect(clipBounds);
539}
540
Michael Ludwig61a16512019-01-15 11:15:13 -0500541bool GrRenderTargetContext::drawFilledRectAsClear(const GrClip& clip, GrPaint&& paint, GrAA aa,
542 const SkMatrix& viewMatrix, const SkRect& rect) {
543 // Rules for a filled rect to become a clear [+scissor]:
544 // 1. The paint is a constant blend color with no other FPs
545 // 2. The view matrix maps rectangles to rectangles, or the transformed quad fully covers
546 // the render target (or clear region in #3).
547 // 3. The clip is an intersection of rectangles, so the clear region will be the
548 // intersection of the clip and the provided rect.
549 // 4. The clear region aligns with pixel bounds
550 // 5. There are no user stencil settings (and since the clip was IOR, the clip won't need
551 // to use the stencil either).
552 // If all conditions pass, the filled rect can either be a fullscreen clear (if it's big
553 // enough), or the rectangle geometry will be used as the scissor clip on the clear.
554 // If everything passes but rule #4, this submits a simplified fill rect op instead so that the
555 // rounding differences between clip and draws don't fight each other.
556 // NOTE: we route draws into clear() regardless of performColorClearsAsDraws() since the
557 // clear call is allowed to reset the oplist even when it also happens to use a GrFillRectOp.
558
559 SkPMColor4f clearColor;
560 if (paint.numCoverageFragmentProcessors() > 0 || !paint.isConstantBlendedColor(&clearColor)) {
561 return false;
562 }
563
564 const SkRect rtRect = fRenderTargetProxy->getBoundsRect();
565 // Will be the intersection of render target, clip, and quad
566 SkRect combinedRect = rtRect;
567
568 SkRRect clipRRect;
569 GrAA clipAA;
570 if (!clip.quickContains(rtRect)) {
571 // If the clip is an rrect with no rounding, then it can replace the full RT bounds as the
572 // limiting region, although we will have to worry about AA. If the clip is anything
573 // more complicated, just punt to the regular fill rect op.
574 if (!clip.isRRect(rtRect, &clipRRect, &clipAA) || !clipRRect.isRect()) {
575 return false;
576 }
577 combinedRect = clipRRect.rect();
578 } else {
579 // The clip is outside the render target, so the clip can be ignored
580 clipAA = GrAA::kNo;
581 }
582
583 if (viewMatrix.rectStaysRect()) {
584 // Skip the extra overhead of inverting the view matrix to see if rtRect is contained in the
585 // drawn rectangle, and instead just intersect rtRect with the transformed rect. It will be
586 // the new clear region.
587 if (!combinedRect.intersect(viewMatrix.mapRect(rect))) {
588 // No intersection means nothing should be drawn, so return true but don't add an op
589 return true;
590 }
591 } else {
592 // If the transformed rectangle does not contain the combined rt and clip, the draw is too
593 // complex to be implemented as a clear
594 SkMatrix invM;
595 if (!viewMatrix.invert(&invM)) {
596 return false;
597 }
598 // The clip region in the rect's local space, so the test becomes the local rect containing
599 // the quad's points.
600 GrQuad quad(rtRect, invM);
601 if (!rect_contains_inclusive(rect, quad.point(0)) ||
602 !rect_contains_inclusive(rect, quad.point(1)) ||
603 !rect_contains_inclusive(rect, quad.point(2)) ||
604 !rect_contains_inclusive(rect, quad.point(3))) {
605 // No containment, so rtRect can't be filled by a solid color
606 return false;
607 }
608 // combinedRect can be filled by a solid color but doesn't need to be modified since it's
609 // inside the quad to be drawn.
610 }
611
612 // Almost every condition is met; now it requires that the combined rect align with pixel
613 // boundaries in order for it to become a scissor-clear. Ignore the AA status in this case
614 // since non-AA with partial-pixel coordinates can be rounded differently on the GPU,
615 // leading to unexpected differences between a scissor test and a rasterized quad.
616 // Also skip very small rectangles since the scissor+clear doesn't by us much then.
617 if (combinedRect.contains(rtRect)) {
618 // Full screen clear
619 this->clear(nullptr, clearColor, CanClearFullscreen::kYes);
620 return true;
621 } else if (GrClip::IsPixelAligned(combinedRect) &&
622 combinedRect.width() > 256 && combinedRect.height() > 256) {
623 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
624 SkIRect scissorRect;
625 combinedRect.round(&scissorRect);
626 this->clear(&scissorRect, clearColor, CanClearFullscreen::kNo);
627 return true;
628 }
629
630 // If we got here, we can't use a scissor + clear, but combinedRect represents the correct
631 // geometry combination of quad + clip so we can perform a simplified fill rect op. We do this
632 // mostly to avoid mismatches in rounding logic on the CPU vs. the GPU, which frequently appears
633 // when drawing and clipping something to the same non-AA rect that never-the-less has
634 // non-integer coordinates.
635
636 // For AA, use non-AA only when both clip and draw are non-AA.
637 if (clipAA == GrAA::kYes) {
638 aa = GrAA::kYes;
639 }
640 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
641 this->addDrawOp(GrFixedClip::Disabled(),
642 GrFillRectOp::Make(fContext, std::move(paint), aaType, SkMatrix::I(),
643 combinedRect));
644 return true;
645}
646
647void GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500648 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500649 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400650 const SkMatrix& viewMatrix,
651 const SkRect& rect,
652 const GrUserStencilSettings* ss) {
Michael Ludwig61a16512019-01-15 11:15:13 -0500653
654 if (!ss) {
655 if (this->drawFilledRectAsClear(clip, std::move(paint), aa, viewMatrix, rect)) {
656 return;
657 }
658 // Fall through to fill rect op
659 assert_alive(paint);
660 }
661
csmartdalton97f6cd52016-07-13 13:37:08 -0700662 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500663 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
Michael Ludwig61a16512019-01-15 11:15:13 -0500664 // The rectangle would not be drawn, so no need to add a draw op to the list
665 return;
csmartdalton97f6cd52016-07-13 13:37:08 -0700666 }
robertphillips44302392016-07-08 14:43:03 -0700667
Brian Salomon7c8460e2017-05-12 11:36:10 -0400668 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500669 this->addDrawOp(clip, GrFillRectOp::Make(fContext, std::move(paint), aaType, viewMatrix,
670 croppedRect, ss));
robertphillips391395d2016-03-02 09:26:36 -0800671}
672
Brian Osman11052242016-10-27 14:47:55 -0400673void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500674 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500675 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400676 const SkMatrix& viewMatrix,
677 const SkRect& rect,
678 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700679 if (!style) {
680 style = &GrStyle::SimpleFill();
681 }
joshualitt1de610a2016-01-06 08:26:09 -0800682 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700683 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700684 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400685 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700686
bsalomon6663acf2016-05-10 09:14:17 -0700687 // Path effects should've been devolved to a path in SkGpuDevice
688 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700689
Robert Phillips72152832017-01-25 17:31:35 -0500690 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700691
bsalomon6663acf2016-05-10 09:14:17 -0700692 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400693 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61a16512019-01-15 11:15:13 -0500694 this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect);
695 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700696 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
697 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
698 if ((!rect.width() || !rect.height()) &&
699 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
700 SkScalar r = stroke.getWidth() / 2;
701 // TODO: Move these stroke->fill fallbacks to GrShape?
702 switch (stroke.getJoin()) {
703 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500704 this->drawRect(
705 clip, std::move(paint), aa, viewMatrix,
706 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
707 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700708 return;
709 case SkPaint::kRound_Join:
710 // Raster draws nothing when both dimensions are empty.
711 if (rect.width() || rect.height()){
712 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500713 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
714 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700715 return;
716 }
717 case SkPaint::kBevel_Join:
718 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500719 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700720 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
721 &GrStyle::SimpleFill());
722 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500723 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700724 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
725 &GrStyle::SimpleFill());
726 }
727 return;
728 }
729 }
robertphillips44302392016-07-08 14:43:03 -0700730
Brian Salomonbaaf4392017-06-15 09:59:23 -0400731 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700732
Brian Salomon7c8460e2017-05-12 11:36:10 -0400733 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500734 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
735 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
736 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500737 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400738 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700739 return;
robertphillips4bc31812016-03-01 12:22:49 -0800740 }
robertphillips4bc31812016-03-01 12:22:49 -0800741 }
Mike Klein16885072018-12-11 09:54:31 -0500742 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500743 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700744}
745
Michael Ludwig69858532018-11-28 15:34:34 -0500746void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
747 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
748 int cnt) {
749 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
750 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
751 quads, cnt));
752}
753
Robert Phillipsec2249f2016-11-09 08:54:35 -0500754int GrRenderTargetContextPriv::maxWindowRectangles() const {
755 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400756 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500757}
758
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000759void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700760 ASSERT_SINGLE_OWNER_PRIV
761 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400762 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400763 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
764 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700765
Robert Phillips72152832017-01-25 17:31:35 -0500766 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400767
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500768 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
769}
770
771void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
772 if (this->caps()->performStencilClearsAsDraws()) {
773 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
774 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
775
776 // Configure the paint to have no impact on the color buffer
777 GrPaint paint;
778 paint.setColor4f({0.f, 0.f, 0.f, 0.f});
779 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
780
781 // Mark stencil usage here before addDrawOp() so that it doesn't try to re-call
782 // internalStencilClear() just because the op has stencil settings.
783 this->setNeedsStencil();
784 this->addDrawOp(clip, GrFillRectOp::Make(fContext, std::move(paint),
785 GrAAType::kNone, SkMatrix::I(), rtRect, ss));
786 } else {
787 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
788 fRenderTargetProxy.get()));
789 if (!op) {
790 return;
791 }
792 this->getRTOpList()->addOp(std::move(op), *this->caps());
Robert Phillipse60ad622016-11-17 10:22:48 -0500793 }
robertphillips976f5f02016-06-03 10:59:20 -0700794}
795
Chris Daltonbbfd5162017-11-07 13:35:22 -0700796void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500797 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400798 const SkMatrix& viewMatrix,
799 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500800 ASSERT_SINGLE_OWNER_PRIV
801 RETURN_IF_ABANDONED_PRIV
802 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400803 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
804 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500805
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500806 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500807
808 bool useHWAA = GrAATypeIsHW(aaType);
809 // TODO: extract portions of checkDraw that are relevant to path stenciling.
810 SkASSERT(path);
811 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
812
813 // FIXME: Use path bounds instead of this WAR once
814 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
815 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
816
817 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700818 GrAppliedHardClip appliedClip;
819 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
820 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500821 return;
822 }
823
Robert Phillips65048132017-08-10 08:44:49 -0400824 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500825
Robert Phillips7c525e62018-06-12 10:11:12 -0400826 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
827 viewMatrix,
Brian Salomon467921e2017-03-06 16:17:12 -0500828 useHWAA,
829 path->getFillType(),
830 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500831 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500832 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400833 if (!op) {
834 return;
835 }
Brian Salomon97180af2017-03-14 13:42:58 -0400836 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400837 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700838}
839
Chris Daltonbbfd5162017-11-07 13:35:22 -0700840void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400841 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500842 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400843 const SkMatrix& viewMatrix,
844 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700845 ASSERT_SINGLE_OWNER_PRIV
846 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400847 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400848 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
849 fRenderTargetContext->fContext);
850
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500851 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500852 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700853
854 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500855 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwig72ab3462018-12-10 12:43:36 -0500856 std::unique_ptr<GrDrawOp> op = GrFillRectOp::Make(
857 fRenderTargetContext->fContext, std::move(paint), aaType, viewMatrix, rect, ss);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400858 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700859}
860
Chris Daltonbbfd5162017-11-07 13:35:22 -0700861bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400862 const GrUserStencilSettings* ss,
863 SkRegion::Op op,
864 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500865 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400866 const SkMatrix& viewMatrix,
867 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800868 ASSERT_SINGLE_OWNER_PRIV
869 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400870 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400871 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
872 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800873
Robert Phillips72152832017-01-25 17:31:35 -0500874 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800875
876 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800877 paint.setCoverageSetOpXPFactory(op, invert);
878
Michael Ludwig61a16512019-01-15 11:15:13 -0500879 // This will always succeed to draw a rectangle
880 fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss);
881 return true;
robertphillips391395d2016-03-02 09:26:36 -0800882}
883
Brian Osman11052242016-10-27 14:47:55 -0400884void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500885 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500886 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400887 const SkMatrix& viewMatrix,
888 const SkRect& rectToDraw,
889 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800890 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700891 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700892 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400893 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700894
csmartdalton97f6cd52016-07-13 13:37:08 -0700895 SkRect croppedRect = rectToDraw;
896 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700897 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
898 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700899 return;
900 }
901
Robert Phillips72152832017-01-25 17:31:35 -0500902 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700903
Brian Salomon7c8460e2017-05-12 11:36:10 -0400904 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500905 this->addDrawOp(clip, GrFillRectOp::MakeWithLocalRect(fContext, std::move(paint), aaType,
906 viewMatrix, croppedRect, croppedLocalRect));
joshualittb6b513b2015-08-21 10:25:18 -0700907}
908
Michael Ludwig75451902019-01-23 11:14:29 -0500909void GrRenderTargetContext::fillRectWithEdgeAA(const GrClip& clip, GrPaint&& paint,
910 GrQuadAAFlags edgeAA, const SkMatrix& viewMatrix,
911 const SkRect& rect) {
912 ASSERT_SINGLE_OWNER
913 RETURN_IF_ABANDONED
914 SkDEBUGCODE(this->validate();)
915 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithEdgeAA", fContext);
916
917 // If aaType turns into MSAA, make sure to keep quads with no AA edges as MSAA. Sending those
918 // to drawFilledRect() would have it turn off MSAA in that case, which breaks seaming with
919 // any partial AA edges that kept MSAA.
920 GrAAType aaType = this->chooseAAType(GrAA::kYes, GrAllowMixedSamples::kNo);
921 if (aaType != GrAAType::kMSAA &&
922 (edgeAA == GrQuadAAFlags::kNone || edgeAA == GrQuadAAFlags::kAll)) {
923 // This is equivalent to a regular filled rect draw, so route through there to take
924 // advantage of draw->clear optimizations
925 this->drawFilledRect(clip, std::move(paint), GrAA(edgeAA == GrQuadAAFlags::kAll),
926 viewMatrix, rect);
927 return;
928 }
929
930 SkRect croppedRect = rect;
931 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
932 return;
933 }
934
935 AutoCheckFlush acf(this->drawingManager());
936 this->addDrawOp(clip, GrFillRectOp::MakePerEdge(fContext, std::move(paint), aaType, edgeAA,
937 viewMatrix, croppedRect));
938}
939
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400940void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
Brian Osman3d139a42018-11-19 10:42:10 -0500941 GrSamplerState::Filter filter, const SkPMColor4f& color,
Brian Salomon2213ee92018-10-02 10:44:21 -0400942 const SkRect& srcRect, const SkRect& dstRect,
943 GrQuadAAFlags aaFlags,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400944 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400945 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500946 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400947 ASSERT_SINGLE_OWNER
948 RETURN_IF_ABANDONED
949 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400950 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomonf1709042018-10-03 11:57:00 -0400951 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
952 srcRect.contains(proxy->getWorstCaseBoundsRect())) {
953 constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon34169692017-08-28 15:32:01 -0400954 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400955 GrAAType aaType =
956 this->chooseAAType(GrAA(aaFlags != GrQuadAAFlags::kNone), GrAllowMixedSamples::kNo);
Brian Salomonff9d6d32017-08-30 10:27:49 -0400957 SkRect clippedDstRect = dstRect;
958 SkRect clippedSrcRect = srcRect;
959 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
960 &clippedSrcRect)) {
961 return;
962 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400963 auto op = GrTextureOp::Make(fContext, std::move(proxy), filter, color, clippedSrcRect,
964 clippedDstRect, aaType, aaFlags, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500965 std::move(textureColorSpaceXform));
Brian Salomon2213ee92018-10-02 10:44:21 -0400966 this->addDrawOp(clip, std::move(op));
Brian Salomon34169692017-08-28 15:32:01 -0400967}
968
Brian Salomond7065e72018-10-12 11:42:02 -0400969void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500970 GrSamplerState::Filter filter, SkBlendMode mode,
Brian Salomond7065e72018-10-12 11:42:02 -0400971 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500972 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400973 ASSERT_SINGLE_OWNER
974 RETURN_IF_ABANDONED
975 SkDEBUGCODE(this->validate();)
976 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500977
978 AutoCheckFlush acf(this->drawingManager());
979
Brian Salomond7065e72018-10-12 11:42:02 -0400980 GrAAType aaType = this->chooseAAType(GrAA::kYes, GrAllowMixedSamples::kNo);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500981 if (mode != SkBlendMode::kSrcOver ||
Robert Phillips9da87e02019-02-04 13:26:26 -0500982 !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) {
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500983 // Draw one at a time with GrFillRectOp and a GrPaint that emulates what GrTextureOp does
984 const GrXPFactory* xpFactory = SkBlendMode_AsXPFactory(mode);
985 for (int i = 0; i < cnt; ++i) {
986 GrPaint paint;
987 paint.setColor4f({set[i].fAlpha, set[i].fAlpha, set[i].fAlpha, set[i].fAlpha});
988 paint.setXPFactory(xpFactory);
989
990 // See if we can disable bilerp filtering when the src and dst rects line up
991 if (filter != GrSamplerState::Filter::kNearest &&
992 !GrTextureOp::GetFilterHasEffect(viewMatrix, set[i].fSrcRect, set[i].fDstRect)) {
993 filter = GrSamplerState::Filter::kNearest;
994 }
995
996 auto fp = GrSimpleTextureEffect::Make(set[i].fProxy, SkMatrix::I(), filter);
997 fp = GrColorSpaceXformEffect::Make(std::move(fp), texXform);
998 paint.addColorFragmentProcessor(std::move(fp));
999
1000 auto op = GrFillRectOp::MakePerEdgeWithLocalRect(fContext, std::move(paint), aaType,
1001 set[i].fAAFlags, viewMatrix,
1002 set[i].fDstRect, set[i].fSrcRect);
1003 this->addDrawOp(clip, std::move(op));
1004 }
1005 } else {
1006 // Can use a single op, avoiding GrPaint creation, and can batch across proxies
1007 auto op = GrTextureOp::Make(fContext, set, cnt, filter, aaType, viewMatrix,
1008 std::move(texXform));
1009 this->addDrawOp(clip, std::move(op));
1010 }
Brian Salomond7065e72018-10-12 11:42:02 -04001011}
1012
Brian Osman11052242016-10-27 14:47:55 -04001013void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001014 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001015 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001016 const SkMatrix& viewMatrix,
1017 const SkRect& rectToDraw,
1018 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -08001019 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -07001020 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001021 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001022 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -07001023
csmartdalton97f6cd52016-07-13 13:37:08 -07001024 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -07001025 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -07001026 return;
1027 }
1028
Robert Phillips72152832017-01-25 17:31:35 -05001029 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001030
Brian Salomon7c8460e2017-05-12 11:36:10 -04001031 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -05001032 this->addDrawOp(clip, GrFillRectOp::MakeWithLocalMatrix(fContext, std::move(paint), aaType,
1033 viewMatrix, localMatrix, croppedRect));
robertphillipsea461502015-05-26 11:38:03 -07001034}
1035
Brian Osman11052242016-10-27 14:47:55 -04001036void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001037 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001038 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001039 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001040 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001041 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -04001042 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -05001043 ASSERT_SINGLE_OWNER
1044 RETURN_IF_ABANDONED
1045 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001046 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -05001047
1048 AutoCheckFlush acf(this->drawingManager());
1049
1050 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -04001051 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -04001052 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -04001053 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -04001054 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -04001055 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -07001056}
1057
1058///////////////////////////////////////////////////////////////////////////////
1059
Brian Osman11052242016-10-27 14:47:55 -04001060void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001061 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001062 const SkMatrix& viewMatrix,
1063 int spriteCount,
1064 const SkRSXform xform[],
1065 const SkRect texRect[],
1066 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -08001067 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -07001068 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001069 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001070 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001071
Robert Phillips72152832017-01-25 17:31:35 -05001072 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -07001073
Brian Salomon0088f942017-07-12 11:51:27 -04001074 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001075 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
1076 aaType, spriteCount, xform, texRect, colors);
Brian Salomon0088f942017-07-12 11:51:27 -04001077 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -07001078}
1079
1080///////////////////////////////////////////////////////////////////////////////
1081
Brian Osman11052242016-10-27 14:47:55 -04001082void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -05001083 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001084 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001085 const SkMatrix& viewMatrix,
1086 const SkRRect& rrect,
1087 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001088 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001089 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001090 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001091 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -04001092
1093 const SkStrokeRec& stroke = style.strokeRec();
1094 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001095 return;
1096 }
1097
bsalomon7f0d9f32016-08-15 14:49:10 -07001098 GrNoClip noclip;
1099 const GrClip* clip = &origClip;
1100#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1101 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -05001102 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -07001103 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1104 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1105 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
1106 SkRRect devRRect;
1107 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1108 clip = &noclip;
1109 }
1110#endif
bsalomon6663acf2016-05-10 09:14:17 -07001111 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -07001112
Robert Phillips72152832017-01-25 17:31:35 -05001113 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001114
Brian Salomonea26d6b2018-01-23 20:33:21 +00001115 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001116 if (GrAAType::kCoverage == aaType) {
Chris Dalton133944a2018-11-16 23:30:29 -05001117 std::unique_ptr<GrDrawOp> op;
1118 if (style.isSimpleFill()) {
1119 op = GrAAFillRRectOp::Make(fContext, viewMatrix, rrect, *this->caps(),
1120 std::move(paint));
1121 }
1122 if (!op) {
Mike Klein16885072018-12-11 09:54:31 -05001123 assert_alive(paint);
Chris Dalton133944a2018-11-16 23:30:29 -05001124 op = GrOvalOpFactory::MakeRRectOp(fContext, std::move(paint), viewMatrix, rrect, stroke,
1125 this->caps()->shaderCaps());
1126 }
1127
Brian Salomon42521e82016-12-07 16:44:58 -05001128 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001129 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001130 return;
1131 }
robertphillipsea461502015-05-26 11:38:03 -07001132 }
robertphillipsb56f9272016-02-25 11:03:52 -08001133
Mike Klein16885072018-12-11 09:54:31 -05001134 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001135 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
1136 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -07001137}
1138
Jim Van Verthc5903412016-11-17 15:27:09 -05001139///////////////////////////////////////////////////////////////////////////////
1140
Jim Van Verth3af1af92017-05-18 15:06:54 -04001141static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1142 SkPoint3 result;
1143 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1144 result.fZ = pt.fZ;
1145 return result;
1146}
1147
1148bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001149 const SkMatrix& viewMatrix,
1150 const SkPath& path,
1151 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001152 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -05001153 if (fContext->abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001154 return true;
1155 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001156 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001157 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001158
1159 // check z plane
1160 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1161 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1162 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1163 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1164 return false;
1165 }
1166
1167 SkRRect rrect;
1168 SkRect rect;
1169 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001170 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001171 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1172 if (!isRRect &&
1173 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1174 rect.width() > SK_ScalarNearlyZero) {
1175 rrect.setOval(rect);
1176 isRRect = true;
1177 }
1178 if (!isRRect && path.isRect(&rect)) {
1179 rrect.setRect(rect);
1180 isRRect = true;
1181 }
1182
1183 if (!isRRect) {
1184 return false;
1185 }
1186
Jim Van Verthc5903412016-11-17 15:27:09 -05001187 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001188 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001189 }
1190
Robert Phillips72152832017-01-25 17:31:35 -05001191 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001192
Jim Van Verth3af1af92017-05-18 15:06:54 -04001193 // transform light
1194 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1195
1196 // 1/scale
1197 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1198 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1199 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1200 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1201
1202 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001203 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1204
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001205 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001206 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1207 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1208 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001209
1210 // Outset the shadow rrect to the border of the penumbra
1211 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1212 SkRRect ambientRRect;
1213 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1214 // If the rrect was an oval then its outset will also be one.
1215 // We set it explicitly to avoid errors.
1216 if (rrect.isOval()) {
1217 ambientRRect = SkRRect::MakeOval(outsetRect);
1218 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001219 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001220 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1221 }
1222
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001223 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001224 if (transparent) {
1225 // set a large inset to force a fill
1226 devSpaceInsetWidth = ambientRRect.width();
1227 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001228
Robert Phillips7c525e62018-06-12 10:11:12 -04001229 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1230 ambientColor,
1231 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001232 ambientRRect,
1233 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001234 devSpaceInsetWidth);
Brian Salomon05969092017-07-13 11:20:51 -04001235 SkASSERT(op);
1236 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001237 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001238
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001239 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001240 SkScalar devSpaceSpotBlur;
1241 SkScalar spotScale;
1242 SkVector spotOffset;
1243 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1244 devLightPos.fZ, rec.fLightRadius,
1245 &devSpaceSpotBlur, &spotScale, &spotOffset);
1246 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001247 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1248
Jim Van Verth3af1af92017-05-18 15:06:54 -04001249 // Adjust translate for the effect of the scale.
1250 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1251 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1252 // This offset is in dev space, need to transform it into source space.
1253 SkMatrix ctmInverse;
1254 if (viewMatrix.invert(&ctmInverse)) {
1255 ctmInverse.mapPoints(&spotOffset, 1);
1256 } else {
1257 // Since the matrix is a similarity, this should never happen, but just in case...
1258 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1259 SkASSERT(false);
1260 }
1261
1262 // Compute the transformed shadow rrect
1263 SkRRect spotShadowRRect;
1264 SkMatrix shadowTransform;
1265 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1266 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001267 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001268
1269 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001270 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001271 SkScalar insetWidth = blurOutset;
1272 if (transparent) {
1273 // If transparent, just do a fill
1274 insetWidth += spotShadowRRect.width();
1275 } else {
1276 // For shadows, instead of using a stroke we specify an inset from the penumbra
1277 // border. We want to extend this inset area so that it meets up with the caster
1278 // geometry. The inset geometry will by default already be inset by the blur width.
1279 //
1280 // We compare the min and max corners inset by the radius between the original
1281 // rrect and the shadow rrect. The distance between the two plus the difference
1282 // between the scaled radius and the original radius gives the distance from the
1283 // transformed shadow shape to the original shape in that corner. The max
1284 // of these gives the maximum distance we need to cover.
1285 //
1286 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1287 // that to get the full insetWidth.
1288 SkScalar maxOffset;
1289 if (rrect.isRect()) {
1290 // Manhattan distance works better for rects
1291 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1292 rrect.rect().fLeft),
1293 SkTAbs(spotShadowRRect.rect().fTop -
1294 rrect.rect().fTop)),
1295 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1296 rrect.rect().fRight),
1297 SkTAbs(spotShadowRRect.rect().fBottom -
1298 rrect.rect().fBottom)));
1299 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001300 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001301 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1302 rrect.rect().fLeft + dr,
1303 spotShadowRRect.rect().fTop -
1304 rrect.rect().fTop + dr);
1305 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1306 rrect.rect().fRight - dr,
1307 spotShadowRRect.rect().fBottom -
1308 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001309 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1310 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001311 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001312 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001313 }
1314
1315 // Outset the shadow rrect to the border of the penumbra
1316 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1317 if (spotShadowRRect.isOval()) {
1318 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1319 } else {
1320 SkScalar outsetRad = spotRadius + blurOutset;
1321 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1322 }
1323
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001324 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001325
Robert Phillips7c525e62018-06-12 10:11:12 -04001326 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1327 spotColor,
1328 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001329 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001330 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001331 insetWidth);
1332 SkASSERT(op);
1333 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001334 }
1335
1336 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001337}
1338
1339///////////////////////////////////////////////////////////////////////////////
1340
Brian Osman11052242016-10-27 14:47:55 -04001341bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001342 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001343 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001344 const SkMatrix& viewMatrix,
1345 const SkRRect& origOuter,
1346 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001347 SkASSERT(!origInner.isEmpty());
1348 SkASSERT(!origOuter.isEmpty());
1349
Brian Salomon65749212017-12-01 16:01:47 -05001350 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1351
Brian Salomon45839f92017-12-04 09:02:35 -05001352 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1353
1354 if (GrAAType::kMSAA == aaType) {
1355 return false;
1356 }
1357
Mike Reed242135a2018-02-22 13:41:39 -05001358 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1359 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001360 auto outerR = outer->width() / 2.f;
1361 auto innerR = inner->width() / 2.f;
1362 auto cx = outer->getBounds().fLeft + outerR;
1363 auto cy = outer->getBounds().fTop + outerR;
1364 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1365 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1366 auto avgR = (innerR + outerR) / 2.f;
1367 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1368 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1369 stroke.setStrokeStyle(outerR - innerR);
Robert Phillips7c525e62018-06-12 10:11:12 -04001370 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
1371 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001372 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001373 if (op) {
1374 this->addDrawOp(clip, std::move(op));
1375 return true;
1376 }
Mike Klein16885072018-12-11 09:54:31 -05001377 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001378 }
1379 }
1380
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001381 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001382 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001383 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1384 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001385 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001386 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1387 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001388 }
robertphillips00095892016-02-29 13:50:40 -08001389
robertphillips00095892016-02-29 13:50:40 -08001390 SkMatrix inverseVM;
1391 if (!viewMatrix.isIdentity()) {
1392 if (!origInner.transform(viewMatrix, inner.writable())) {
1393 return false;
1394 }
1395 if (!origOuter.transform(viewMatrix, outer.writable())) {
1396 return false;
1397 }
1398 if (!viewMatrix.invert(&inverseVM)) {
1399 return false;
1400 }
1401 } else {
1402 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001403 }
robertphillips00095892016-02-29 13:50:40 -08001404
Ethan Nicholaseace9352018-10-15 20:09:54 +00001405 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001406 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001407 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001408 if (!innerEffect) {
1409 return false;
1410 }
1411
Ethan Nicholaseace9352018-10-15 20:09:54 +00001412 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001413 if (!outerEffect) {
1414 return false;
1415 }
1416
Brian Salomon82f44312017-01-11 13:42:54 -05001417 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1418 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001419
1420 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001421 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001422 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1423 }
halcanary9d524f22016-03-29 09:03:52 -07001424
Brian Salomon82f44312017-01-11 13:42:54 -05001425 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1426 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001427 return true;
1428}
1429
Brian Osman11052242016-10-27 14:47:55 -04001430void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001431 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001432 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001433 const SkMatrix& viewMatrix,
1434 const SkRRect& outer,
1435 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001436 ASSERT_SINGLE_OWNER
1437 RETURN_IF_ABANDONED
1438 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001439 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001440
1441 SkASSERT(!outer.isEmpty());
1442 SkASSERT(!inner.isEmpty());
1443
Robert Phillips72152832017-01-25 17:31:35 -05001444 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001445
Brian Salomon82f44312017-01-11 13:42:54 -05001446 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001447 return;
1448 }
Mike Klein16885072018-12-11 09:54:31 -05001449 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001450
1451 SkPath path;
1452 path.setIsVolatile(true);
1453 path.addRRect(inner);
1454 path.addRRect(outer);
1455 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001456 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001457}
1458
robertphillipsea461502015-05-26 11:38:03 -07001459///////////////////////////////////////////////////////////////////////////////
1460
Brian Osman11052242016-10-27 14:47:55 -04001461void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001462 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001463 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001464 const SkMatrix& viewMatrix,
1465 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001466 const GrStyle& style,
1467 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001468 ASSERT_SINGLE_OWNER
1469 RETURN_IF_ABANDONED
1470 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001471 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001472
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001473 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001474 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001475 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001476 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001477 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1478 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001479 aa = GrAA::kNo;
1480 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001481 }
msarettcc319b92016-08-25 18:07:18 -07001482 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001483 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001484 SkPath path;
1485 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001486 path.setIsVolatile(true);
1487
Brian Salomon82f44312017-01-11 13:42:54 -05001488 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001489 }
1490
Brian Salomonf0366322017-07-11 15:53:05 -04001491 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001492 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1493 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001494 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001495}
1496
Brian Osman11052242016-10-27 14:47:55 -04001497void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001498 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001499 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001500 const SkMatrix& viewMatrix,
1501 const SkRect& oval,
1502 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001503 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001504 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001505 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001506 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001507
Robert Phillips7484d202018-07-03 09:09:08 -04001508 const SkStrokeRec& stroke = style.strokeRec();
1509
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001510 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001511 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1512 return;
1513 }
1514
1515 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001516 return;
robertphillipsea461502015-05-26 11:38:03 -07001517 }
1518
Robert Phillips72152832017-01-25 17:31:35 -05001519 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001520
Brian Salomonea26d6b2018-01-23 20:33:21 +00001521 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001522 if (GrAAType::kCoverage == aaType) {
Chris Daltonebc38c92018-11-28 16:58:09 -07001523 std::unique_ptr<GrDrawOp> op;
1524 // GrAAFillRRectOp has special geometry and a fragment-shader branch to conditionally
1525 // evaluate the arc equation. This same special geometry and fragment branch also turn out
1526 // to be a substantial optimization for drawing ovals (namely, by not evaluating the arc
1527 // equation inside the oval's inner diamond). Given these optimizations, it's a clear win to
1528 // draw ovals the exact same way we do round rects.
1529 //
1530 // However, we still don't draw true circles as round rects, because it can cause perf
1531 // regressions on some platforms as compared to the dedicated circle Op.
1532 if (style.isSimpleFill() && oval.height() != oval.width()) {
1533 op = GrAAFillRRectOp::Make(fContext, viewMatrix, SkRRect::MakeOval(oval), *this->caps(),
1534 std::move(paint));
1535 }
1536 if (!op) {
Mike Klein16885072018-12-11 09:54:31 -05001537 assert_alive(paint);
Chris Daltonebc38c92018-11-28 16:58:09 -07001538 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1539 this->caps()->shaderCaps());
1540 }
1541 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001542 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001543 return;
1544 }
robertphillipsea461502015-05-26 11:38:03 -07001545 }
robertphillipsb56f9272016-02-25 11:03:52 -08001546
Mike Klein16885072018-12-11 09:54:31 -05001547 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001548 this->drawShapeUsingPathRenderer(
1549 clip, std::move(paint), aa, viewMatrix,
1550 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001551}
1552
Brian Osman11052242016-10-27 14:47:55 -04001553void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001554 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001555 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001556 const SkMatrix& viewMatrix,
1557 const SkRect& oval,
1558 SkScalar startAngle,
1559 SkScalar sweepAngle,
1560 bool useCenter,
1561 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001562 ASSERT_SINGLE_OWNER
1563 RETURN_IF_ABANDONED
1564 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001565 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001566
1567 AutoCheckFlush acf(this->drawingManager());
1568
Brian Salomonea26d6b2018-01-23 20:33:21 +00001569 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001570 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001571 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001572 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1573 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +00001574 viewMatrix,
1575 oval,
1576 startAngle,
1577 sweepAngle,
1578 useCenter,
1579 style,
1580 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001581 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001582 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001583 return;
1584 }
Mike Klein16885072018-12-11 09:54:31 -05001585 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001586 }
Brian Salomone4949402018-04-26 15:22:04 -04001587 this->drawShapeUsingPathRenderer(
1588 clip, std::move(paint), aa, viewMatrix,
1589 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001590}
1591
Brian Osman11052242016-10-27 14:47:55 -04001592void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001593 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001594 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001595 sk_sp<GrTextureProxy> image,
1596 sk_sp<GrColorSpaceXform> csxf,
1597 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001598 std::unique_ptr<SkLatticeIter> iter,
1599 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001600 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001601 RETURN_IF_ABANDONED
1602 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001603 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001604
Robert Phillips72152832017-01-25 17:31:35 -05001605 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001606
Brian Salomon2a943df2018-05-04 13:43:19 -04001607 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001608 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1609 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001610 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001611}
1612
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001613void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1614 const SkRect& bounds) {
1615 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1616 SkASSERT(op);
1617 this->getRTOpList()->addOp(std::move(op), *this->caps());
1618}
1619
Greg Daniel51316782017-08-02 15:10:09 +00001620GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1621 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001622 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -05001623 if (fContext->abandoned()) {
1624 return GrSemaphoresSubmitted::kNo;
1625 }
robertphillips8c523e02016-07-26 07:41:00 -07001626 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001627 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001628
Greg Daniel51316782017-08-02 15:10:09 +00001629 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1630 numSemaphores,
1631 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001632}
1633
Greg Danielc64ee462017-06-15 16:59:49 -04001634bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001635 const GrBackendSemaphore* waitSemaphores) {
1636 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001637 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001638 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001639 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001640
1641 AutoCheckFlush acf(this->drawingManager());
1642
Greg Danielc64ee462017-06-15 16:59:49 -04001643 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1644 return false;
1645 }
1646
Robert Phillips9da87e02019-02-04 13:26:26 -05001647 auto resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001648
Greg Daniela5cb7812017-06-16 09:45:32 -04001649 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1650 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001651 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001652 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1653 kAdopt_GrWrapOwnership);
Robert Phillips7c525e62018-06-12 10:11:12 -04001654 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, sema,
1655 fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001656 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001657 }
Greg Danielc64ee462017-06-15 16:59:49 -04001658 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001659}
joshualitt33a5fce2015-11-18 13:28:51 -08001660
Robert Phillips65a88fa2017-08-08 08:36:22 -04001661void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001662 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001663 this->getRTOpList()->addOp(std::move(op), *this->caps());
1664}
1665
Brian Osman11052242016-10-27 14:47:55 -04001666void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001667 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001668 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001669 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001670 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001671 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001672 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001673 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001674 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001675 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1676
Brian Salomon40b77a62017-12-22 11:25:52 -05001677 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001678
Robert Phillips27927a52018-08-20 13:18:12 -04001679 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001680}
1681
1682void GrRenderTargetContext::drawShape(const GrClip& clip,
1683 GrPaint&& paint,
1684 GrAA aa,
1685 const SkMatrix& viewMatrix,
1686 const GrShape& shape) {
1687 ASSERT_SINGLE_OWNER
1688 RETURN_IF_ABANDONED
1689 SkDEBUGCODE(this->validate();)
1690 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1691
Brian Salomon2fad74a2017-12-20 13:28:55 -05001692 if (shape.isEmpty()) {
1693 if (shape.inverseFilled()) {
1694 this->drawPaint(clip, std::move(paint), viewMatrix);
1695 }
1696 return;
robertphillipsea461502015-05-26 11:38:03 -07001697 }
1698
Robert Phillips72152832017-01-25 17:31:35 -05001699 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001700
Brian Salomon2fad74a2017-12-20 13:28:55 -05001701 if (!shape.style().hasPathEffect()) {
Robert Phillips73653b42018-08-22 12:42:42 -04001702 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001703 SkRRect rrect;
1704 // We can ignore the starting point and direction since there is no path effect.
1705 bool inverted;
1706 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1707 if (rrect.isRect()) {
1708 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1709 &shape.style());
1710 return;
1711 } else if (rrect.isOval()) {
1712 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001713 return;
1714 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001715 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1716 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001717 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1718 viewMatrix.rectStaysRect()) {
1719 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1720 // the matrix to all the points individually rather than just to the rect
1721 SkRect rects[2];
1722 if (shape.asNestedRects(rects)) {
1723 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05001724 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04001725 fContext, std::move(paint), viewMatrix, rects);
1726 if (op) {
1727 this->addDrawOp(clip, std::move(op));
1728 }
1729 // Returning here indicates that there is nothing to draw in this case.
1730 return;
1731 }
robertphillipsea461502015-05-26 11:38:03 -07001732 }
1733 }
robertphillips4bc31812016-03-01 12:22:49 -08001734
Brian Salomon2fad74a2017-12-20 13:28:55 -05001735 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001736}
1737
Chris Daltonbbfd5162017-11-07 13:35:22 -07001738bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001739 const GrUserStencilSettings* ss,
1740 SkRegion::Op op,
1741 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001742 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001743 const SkMatrix& viewMatrix,
1744 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001745 ASSERT_SINGLE_OWNER_PRIV
1746 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001747 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001748 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1749 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001750
1751 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001752 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001753 SkRect::MakeIWH(fRenderTargetContext->width(),
1754 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001755 return true;
1756 }
1757
Robert Phillips72152832017-01-25 17:31:35 -05001758 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001759
1760 // An Assumption here is that path renderer would use some form of tweaking
1761 // the src color (either the input alpha or in the frag shader) to implement
1762 // aa. If we have some future driver-mojo path AA that can do the right
1763 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001764 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001765 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001766
Chris Daltondb91c6e2017-09-08 16:25:08 -06001767 SkIRect clipConservativeBounds;
1768 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1769 &clipConservativeBounds, nullptr);
1770
bsalomon8acedde2016-06-24 10:42:16 -07001771 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001772 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001773 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001774 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001775 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001776 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001777 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05001778 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
1779 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001780 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001781
1782 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001783 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001784 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001785 if (!pr) {
1786 return false;
1787 }
1788
1789 GrPaint paint;
1790 paint.setCoverageSetOpXPFactory(op, invert);
1791
Brian Salomonf3569f02017-10-24 12:52:33 -04001792 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1793 std::move(paint),
1794 ss,
1795 fRenderTargetContext,
1796 &clip,
1797 &clipConservativeBounds,
1798 &viewMatrix,
1799 &shape,
1800 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001801 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001802 pr->drawPath(args);
1803 return true;
1804}
1805
Brian Osman11052242016-10-27 14:47:55 -04001806SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001807 ASSERT_SINGLE_OWNER_PRIV
1808
Robert Phillipsa9162df2019-02-11 14:12:03 -05001809 if (fRenderTargetContext->fContext->abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001810 return SkBudgeted::kNo;
1811 }
1812
Brian Osman11052242016-10-27 14:47:55 -04001813 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001814
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001815 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001816}
1817
Brian Salomon2fad74a2017-12-20 13:28:55 -05001818void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1819 GrPaint&& paint,
1820 GrAA aa,
1821 const SkMatrix& viewMatrix,
1822 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001823 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001824 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001825 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1826
Jim Van Verthf86073a2018-10-02 13:05:38 -04001827 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1828 return;
1829 }
1830
Chris Daltondb91c6e2017-09-08 16:25:08 -06001831 SkIRect clipConservativeBounds;
1832 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1833
Brian Salomon2fad74a2017-12-20 13:28:55 -05001834 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001835 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1836 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1837 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001838 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1839 ? GrAllowMixedSamples::kNo
1840 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001841 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001842 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001843 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001844 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001845 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001846 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001847 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001848 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001849
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001850 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001851 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001852 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001853 return;
1854 }
1855
1856 canDrawArgs.fAAType = aaType;
1857
1858 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1859 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1860 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1861
Brian Salomon2fad74a2017-12-20 13:28:55 -05001862 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001863 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001864 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1865 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001866 return;
1867 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001868 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001869 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001870 }
1871 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001872 if (canDrawArgs.fShape->style().applies()) {
1873 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1874 styleScale);
1875 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001876 return;
1877 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001878 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001879 // This time, allow SW renderer
1880 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1881 } else {
1882 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001883 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001884 }
robertphillipsea461502015-05-26 11:38:03 -07001885
bsalomon8acedde2016-06-24 10:42:16 -07001886 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001887#ifdef SK_DEBUG
1888 SkDebugf("Unable to find path renderer compatible with path.\n");
1889#endif
1890 return;
1891 }
1892
Robert Phillips256c37b2017-03-01 14:32:46 -05001893 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001894 std::move(paint),
1895 &GrUserStencilSettings::kUnused,
1896 this,
1897 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001898 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001899 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001900 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001901 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001902 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001903 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001904}
1905
Brian Salomon467921e2017-03-06 16:17:12 -05001906static void op_bounds(SkRect* bounds, const GrOp* op) {
1907 *bounds = op->bounds();
1908 if (op->hasZeroArea()) {
1909 if (op->hasAABloat()) {
1910 bounds->outset(0.5f, 0.5f);
1911 } else {
1912 // We don't know which way the particular GPU will snap lines or points at integer
1913 // coords. So we ensure that the bounds is large enough for either snap.
1914 SkRect before = *bounds;
1915 bounds->roundOut(bounds);
1916 if (bounds->fLeft == before.fLeft) {
1917 bounds->fLeft -= 1;
1918 }
1919 if (bounds->fTop == before.fTop) {
1920 bounds->fTop -= 1;
1921 }
1922 if (bounds->fRight == before.fRight) {
1923 bounds->fRight += 1;
1924 }
1925 if (bounds->fBottom == before.fBottom) {
1926 bounds->fBottom += 1;
1927 }
1928 }
1929 }
1930}
1931
Brian Salomon348a0372018-10-31 10:42:18 -04001932void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
1933 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001934 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -05001935 if (fContext->abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001936 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001937 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001938 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001939 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04001940 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001941 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001942
Brian Salomon467921e2017-03-06 16:17:12 -05001943 // Setup clip
1944 SkRect bounds;
1945 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001946 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001947 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1948 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1949 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1950 &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001951 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001952 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001953 }
1954
Brian Salomon54d212e2017-03-21 14:22:38 -04001955 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1956 appliedClip.hasStencilClip()) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001957 if (this->caps()->performStencilClearsAsDraws()) {
1958 // Must use an op to perform the clear of the stencil buffer before this op, but only
1959 // have to clear the first time any draw needs it (this also ensures we don't loop
1960 // forever when the internal stencil clear adds a draw op that has stencil settings).
1961 if (!fRenderTargetProxy->needsStencil()) {
1962 // Send false so that the stencil buffer is fully cleared to 0
1963 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
1964 }
1965 } else {
1966 // Just make sure the stencil buffer is cleared before the draw op, easy to do it as
1967 // a load at the start
1968 this->getRTOpList()->setStencilLoadOp(GrLoadOp::kClear);
1969 }
Robert Phillips95214472017-08-08 18:00:03 -04001970
Robert Phillips65048132017-08-10 08:44:49 -04001971 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001972 }
1973
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001974 GrXferProcessor::DstProxy dstProxy;
Chris Dalton945ee652019-01-23 09:10:36 -07001975 GrProcessorSet::Analysis analysis = op->finalize(*this->caps(), &appliedClip);
1976 if (analysis.requiresDstTexture()) {
Brian Salomon09181ef2018-11-14 13:39:51 -05001977 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, *op, &dstProxy)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05001978 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001979 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001980 }
1981 }
1982
1983 op->setClippedBounds(bounds);
Brian Salomon348a0372018-10-31 10:42:18 -04001984 auto opList = this->getRTOpList();
1985 if (willAddFn) {
1986 willAddFn(op.get(), opList->uniqueID());
1987 }
Chris Dalton945ee652019-01-23 09:10:36 -07001988 opList->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy, *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04001989}
1990
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001991bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon09181ef2018-11-14 13:39:51 -05001992 const GrOp& op,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001993 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001994 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
1995 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
1996 // start and stop the render pass in order to make the copy.
1997 if (rtProxy->wrapsVkSecondaryCB()) {
1998 return false;
1999 }
2000
Brian Salomon467921e2017-03-06 16:17:12 -05002001 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04002002 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002003 // The render target is a texture, so we can read from it directly in the shader. The XP
2004 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002005 dstProxy->setProxy(sk_ref_sp(texProxy));
2006 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002007 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002008 }
2009 }
2010
Robert Phillipsbf25d432017-04-07 10:08:53 -04002011 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05002012
Eric Karl74480882017-04-03 14:49:05 -07002013 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002014 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002015 SkRect opBounds = op.bounds();
2016 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2017 // 0.5 pixels.
2018 if (op.hasAABloat() || op.hasZeroArea()) {
2019 opBounds.outset(0.5f, 0.5f);
2020 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2021 // performance we may ignore the clip when the draw is entirely inside the clip is float
2022 // space but will hit pixels just outside the clip when actually rasterizing.
2023 clippedRect.outset(1, 1);
2024 clippedRect.intersect(SkIRect::MakeWH(rtProxy->width(), rtProxy->height()));
2025 }
2026 SkIRect opIBounds;
2027 opBounds.roundOut(&opIBounds);
2028 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002029#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002030 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002031#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002032 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002033 }
2034
2035 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2036 // have per-sample dst values by making the copy multisampled.
2037 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07002038 bool rectsMustMatch = false;
2039 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05002040 GrSurfaceOrigin origin;
2041 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
2042 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002043 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002044 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04002045 origin = rtProxy->origin();
Brian Salomon467921e2017-03-06 16:17:12 -05002046 }
2047
Eric Karl74480882017-04-03 14:49:05 -07002048 if (!disallowSubrect) {
2049 copyRect = clippedRect;
2050 }
Brian Salomon467921e2017-03-06 16:17:12 -05002051
Robert Phillipsbf25d432017-04-07 10:08:53 -04002052 SkIPoint dstPoint, dstOffset;
2053 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07002054 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04002055 desc.fWidth = rtProxy->width();
2056 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07002057 dstPoint = {copyRect.fLeft, copyRect.fTop};
2058 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002059 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002060 } else {
2061 desc.fWidth = copyRect.width();
2062 desc.fHeight = copyRect.height();
2063 dstPoint = {0, 0};
2064 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002065 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002066 }
Brian Salomon467921e2017-03-06 16:17:12 -05002067
Greg Daniel4065d452018-11-16 15:43:41 -05002068 SkASSERT(rtProxy->backendFormat().textureType() == GrTextureType::k2D);
2069 const GrBackendFormat& format = rtProxy->backendFormat();
Robert Phillips9da87e02019-02-04 13:26:26 -05002070 sk_sp<GrSurfaceContext> sContext = fContext->priv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -05002071 format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05002072 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04002073 if (!sContext) {
2074 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
2075 return false;
2076 }
2077
2078 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
2079 SkDebugf("setupDstTexture: copy failed.\n");
2080 return false;
2081 }
2082
Robert Phillipsbb581ce2017-05-29 15:05:15 -04002083 dstProxy->setProxy(sContext->asTextureProxyRef());
2084 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002085 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002086}