blob: c9d44cc673324dcac9011f27a93e3e69bbe6e09b [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 Phillips9d6c64f2017-09-14 10:56:45 -040019#include "GrOpList.h"
robertphillipsea461502015-05-26 11:38:03 -070020#include "GrPathRenderer.h"
Brian Salomon57caa662017-10-18 12:21:05 +000021#include "GrQuad.h"
robertphillips2334fb62015-06-17 05:43:33 -070022#include "GrRenderTarget.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050023#include "GrRenderTargetContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070024#include "GrResourceProvider.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040025#include "GrShape.h"
Brian Salomon467921e2017-03-06 16:17:12 -050026#include "GrStencilAttachment.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040027#include "GrStyle.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040028#include "GrTracing.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040029#include "SkDrawShadowInfo.h"
Herb Derby8378dfb2018-08-30 14:50:04 -040030#include "SkGlyphRunPainter.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040031#include "SkGr.h"
Brian Salomon467921e2017-03-06 16:17:12 -050032#include "SkLatticeIter.h"
33#include "SkMatrixPriv.h"
Mike Reed242135a2018-02-22 13:41:39 -050034#include "SkRRectPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040035#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070036#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050037#include "effects/GrRRectEffect.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040038#include "ops/GrAtlasTextOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050039#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040040#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040041#include "ops/GrDebugMarkerOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000042#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040043#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050044#include "ops/GrDrawVerticesOp.h"
45#include "ops/GrLatticeOp.h"
46#include "ops/GrOp.h"
47#include "ops/GrOvalOpFactory.h"
48#include "ops/GrRectOpFactory.h"
49#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040050#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050051#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050052#include "ops/GrStencilPathOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040053#include "ops/GrTextureOp.h"
Herb Derby26cbe512018-05-24 14:39:01 -040054#include "text/GrTextContext.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040055#include "text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040056
Herb Derbyc1b482c2018-08-09 15:02:27 -040057class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040058public:
59 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040060 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomonf18b1d82017-10-27 11:30:49 -040061 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040062 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040063 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040064
Robert Phillips7c525e62018-06-12 10:11:12 -040065 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040066 fRenderTargetContext->addDrawOp(clip, std::move(op));
67 }
68
Robert Phillips46a13382018-08-23 13:53:01 -040069 void drawShape(const GrClip& clip, const SkPaint& paint,
70 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040071 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
72 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040073 }
74
75 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040076 GrPaint* grPaint) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040077 GrContext* context = fRenderTargetContext->fContext;
78 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
79 if (kARGB_GrMaskFormat == maskFormat) {
80 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
81 } else {
82 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
83 }
84 }
85
Robert Phillips7c525e62018-06-12 10:11:12 -040086 GrContext* getContext() override {
87 return fRenderTargetContext->fContext;
88 }
89
Herb Derby65956872018-08-21 16:55:04 -040090 SkGlyphRunListPainter* glyphPainter() override {
91 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -040092 }
93
Brian Salomonf18b1d82017-10-27 11:30:49 -040094private:
95 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -040096 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -040097
Brian Salomonf18b1d82017-10-27 11:30:49 -040098};
joshualittbc907352016-01-13 06:45:40 -080099
Robert Phillips72152832017-01-25 17:31:35 -0500100#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800101#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400102 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800103#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400104 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -0500105#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
106#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
107#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
108#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
109#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700110
Brian Salomone225b562017-06-14 13:00:03 -0400111//////////////////////////////////////////////////////////////////////////////
112
113GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
114 const GrCaps& caps) {
115 if (GrAA::kNo == aa) {
116 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
117 // that.
118 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
119 return GrAAType::kMSAA;
120 }
121 return GrAAType::kNone;
122 }
123 switch (fsaaType) {
124 case GrFSAAType::kNone:
125 return GrAAType::kCoverage;
126 case GrFSAAType::kUnifiedMSAA:
127 return GrAAType::kMSAA;
128 case GrFSAAType::kMixedSamples:
129 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
130 : GrAAType::kCoverage;
131 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400132 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400133 return GrAAType::kNone;
134}
135
136//////////////////////////////////////////////////////////////////////////////
137
robertphillipsea461502015-05-26 11:38:03 -0700138class AutoCheckFlush {
139public:
halcanary9d524f22016-03-29 09:03:52 -0700140 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700141 SkASSERT(fDrawingManager);
142 }
bsalomonb77a9072016-09-07 10:02:04 -0700143 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700144
145private:
robertphillips77a2e522015-10-17 07:43:27 -0700146 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700147};
148
Brian Osman11052242016-10-27 14:47:55 -0400149bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500150 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700151}
152
Robert Phillipsf2361d22016-10-25 14:20:06 -0400153// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400154// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400155// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400156// when the renderTargetContext attempts to use it (via getOpList).
157GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
158 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400159 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400160 sk_sp<SkColorSpace> colorSpace,
161 const SkSurfaceProps* surfaceProps,
162 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400163 GrSingleOwner* singleOwner,
164 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400165 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
166 singleOwner)
167 , fRenderTargetProxy(std::move(rtp))
168 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400169 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
170 , fManagedOpList(managedOpList) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500171 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
172 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
173 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
174 // world we need to get the correct opList here so that it, in turn, can grab and hold
175 // its rendertarget.
176 this->getRTOpList();
177 }
178
Brian Salomonf18b1d82017-10-27 11:30:49 -0400179 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700180 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700181}
182
robertphillips2e1e51f2015-10-15 08:01:48 -0700183#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400184void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400185 SkASSERT(fRenderTargetProxy);
186 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700187
Robert Phillipsf2361d22016-10-25 14:20:06 -0400188 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400189 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700190 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700191}
192#endif
193
Brian Osman11052242016-10-27 14:47:55 -0400194GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800195 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700196}
197
Robert Phillipsf200a902017-01-30 13:27:37 -0500198GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000199 return fRenderTargetProxy->asTextureProxy();
200}
201
Greg Daniele252f082017-10-23 16:05:23 -0400202const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
203 return fRenderTargetProxy->asTextureProxy();
204}
205
Robert Phillipsf200a902017-01-30 13:27:37 -0500206sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
207 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
208}
209
Greg Daniele252f082017-10-23 16:05:23 -0400210GrMipMapped GrRenderTargetContext::mipMapped() const {
211 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
212 return proxy->mipMapped();
213 }
214 return GrMipMapped::kNo;
215}
216
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400217GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800218 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700219 SkDEBUGCODE(this->validate();)
220
Robert Phillipsf2361d22016-10-25 14:20:06 -0400221 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400222 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700223 }
224
Robert Phillipsdc83b892017-04-13 12:23:54 -0400225 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700226}
227
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400228GrOpList* GrRenderTargetContext::getOpList() {
229 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700230}
231
Herb Derbycddab252018-07-16 11:19:04 -0400232void GrRenderTargetContext::drawGlyphRunList(
233 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400234 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800235 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700236 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700237 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400238 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700239
Herb Derby26cbe512018-05-24 14:39:01 -0400240 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400241 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400242 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700243}
244
Brian Osman11052242016-10-27 14:47:55 -0400245void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800246 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700247 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700248 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400249 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700250
Robert Phillips72152832017-01-25 17:31:35 -0500251 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400252
Robert Phillips380b90c2017-08-30 07:41:07 -0400253 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700254}
255
Brian Osman11052242016-10-27 14:47:55 -0400256void GrRenderTargetContext::clear(const SkIRect* rect,
257 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700258 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800259 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700260 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700261 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400262 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700263
Robert Phillips72152832017-01-25 17:31:35 -0500264 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700265 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
266 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700267}
robertphillips9199a9f2016-07-13 07:48:43 -0700268
Robert Phillips784b7bf2016-12-09 13:35:02 -0500269void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
270 ASSERT_SINGLE_OWNER_PRIV
271 RETURN_IF_ABANDONED_PRIV
272 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400273 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
274 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500275
Robert Phillips72152832017-01-25 17:31:35 -0500276 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500277
Brian Salomonbb5711a2017-05-17 13:49:59 -0400278 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
279 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500280
281 if (clearRect) {
282 if (clearRect->contains(rtRect)) {
283 clearRect = nullptr; // full screen
284 } else {
285 if (!rtRect.intersect(*clearRect)) {
286 return;
287 }
288 }
289 }
290
291 // TODO: in a post-MDB world this should be handled at the OpList level.
292 // An op-list that is initially cleared and has no other ops should receive an
293 // extra draw.
Brian Salomon43f8bf02017-10-18 08:33:29 -0400294 // This path doesn't handle coalescing of full screen clears b.c. it
295 // has to clear the entire render target - not just the content area.
296 // It could be done but will take more finagling.
Robert Phillips7c525e62018-06-12 10:11:12 -0400297 std::unique_ptr<GrOp> op(GrClearOp::Make(fRenderTargetContext->fContext, rtRect,
298 color, !clearRect));
Brian Salomon43f8bf02017-10-18 08:33:29 -0400299 if (!op) {
300 return;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500301 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400302 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500303}
304
Brian Osman11052242016-10-27 14:47:55 -0400305void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
306 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700307 CanClearFullscreen canClearFullscreen) {
csmartdalton29df7602016-08-31 11:55:52 -0700308 ASSERT_SINGLE_OWNER_PRIV
309 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400310 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400311 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
312 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700313
Robert Phillips72152832017-01-25 17:31:35 -0500314 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700315 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700316}
317
Brian Osman11052242016-10-27 14:47:55 -0400318void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
319 const GrColor color,
Chris Dalton344e9032017-12-11 15:42:09 -0700320 CanClearFullscreen canClearFullscreen) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700321 bool isFull = false;
322 if (!clip.hasWindowRectangles()) {
Brian Salomond818ebf2018-07-02 14:08:49 +0000323 isFull = !clip.scissorEnabled() ||
Chris Dalton344e9032017-12-11 15:42:09 -0700324 (CanClearFullscreen::kYes == canClearFullscreen &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400325 this->caps()->preferFullscreenClears()) ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700326 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
327 }
robertphillips9199a9f2016-07-13 07:48:43 -0700328
Brian Salomon43f8bf02017-10-18 08:33:29 -0400329 if (isFull) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400330 this->getRTOpList()->fullClear(fContext, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700331 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400332 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color, this->asSurfaceProxy()));
Brian Salomon42521e82016-12-07 16:44:58 -0500333 if (!op) {
csmartdalton29df7602016-08-31 11:55:52 -0700334 return;
335 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400336 this->getRTOpList()->addOp(std::move(op), *this->caps());
robertphillips9199a9f2016-07-13 07:48:43 -0700337 }
robertphillipsea461502015-05-26 11:38:03 -0700338}
339
Brian Osman11052242016-10-27 14:47:55 -0400340void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500341 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400342 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800343 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700344 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700345 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400346 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700347
robertphillipsea461502015-05-26 11:38:03 -0700348 // set rect to be big enough to fill the space, but not super-huge, so we
349 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700350
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400351 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700352
bsalomoncb31e512016-08-26 10:48:19 -0700353 SkRRect rrect;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400354 GrAA aa = GrAA::kNo;
355
356 // rrect is calculated in one of two places, don't do it twice
357 enum RRectState {
358 kUnknown, kValid, kNotValid
359 };
360 RRectState rrectState = kUnknown;
361
362 // Check if the paint is a constant color, which is the first criterion to being able to turn
363 // the drawPaint() into a clear(). More expensive geometry checks can happen after that.
364 GrColor clearColor;
365 if (paint.isConstantBlendedColor(&clearColor)) {
366 // Regardless of the actual clip geometry, if it completely covers the device bounds it can
367 // be turned into a fullscreen clear.
368 if (clip.quickContains(r)) {
369 // Fill the device with the constant color
370 this->clear(nullptr, clearColor, CanClearFullscreen::kYes);
371 return;
372 }
373 // If the clip intersection with the device is an aligned rectangle, it can be a clear
374 // limited by the scissor test.
375 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
376 if (rrectState == kValid && aa == GrAA::kNo && rrect.isRect()) {
377 SkIRect scissorRect = GrClip::GetPixelIBounds(rrect.getBounds());
378 this->clear(&scissorRect, clearColor, CanClearFullscreen::kNo);
379 return;
380 }
381 }
382
bsalomoncb31e512016-08-26 10:48:19 -0700383 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
384 // transformation for non-rect rrects. Rects caused a performance regression on an Android
385 // test that needs investigation. We also skip cases where there are fragment processors
386 // because they may depend on having correct local coords and this path draws in device space
387 // without a local matrix.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400388 if (!paint.numTotalFragmentProcessors()) {
389 if (rrectState == kUnknown) {
390 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
391 }
392 if (rrectState == kValid && !rrect.isRect()) {
393 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
394 GrStyle::SimpleFill());
395 return;
396 }
bsalomoncb31e512016-08-26 10:48:19 -0700397 }
398
robertphillipsea461502015-05-26 11:38:03 -0700399 bool isPerspective = viewMatrix.hasPerspective();
400
401 // We attempt to map r by the inverse matrix and draw that. mapRect will
402 // map the four corners and bound them with a new rect. This will not
403 // produce a correct result for some perspective matrices.
404 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700405 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700406 return;
407 }
Brian Salomon82f44312017-01-11 13:42:54 -0500408 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700409 } else {
410 SkMatrix localMatrix;
411 if (!viewMatrix.invert(&localMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700412 return;
413 }
414
Robert Phillips72152832017-01-25 17:31:35 -0500415 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700416
Brian Salomonbaaf4392017-06-15 09:59:23 -0400417 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400418 fContext, std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400419 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700420 }
421}
422
robertphillipsea461502015-05-26 11:38:03 -0700423static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
424 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
425 point.fY >= rect.fTop && point.fY <= rect.fBottom;
426}
427
csmartdalton97f6cd52016-07-13 13:37:08 -0700428// Attempts to crop a rect and optional local rect to the clip boundaries.
429// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700430static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700431 const SkMatrix& viewMatrix, SkRect* rect,
432 SkRect* localRect = nullptr) {
433 if (!viewMatrix.rectStaysRect()) {
434 return true;
435 }
436
csmartdalton97f6cd52016-07-13 13:37:08 -0700437 SkIRect clipDevBounds;
438 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700439
robertphillips13a7eee2016-08-31 15:06:24 -0700440 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700441 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
442 return false;
443 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700444
445 if (localRect) {
446 if (!rect->intersects(clipBounds)) {
447 return false;
448 }
449 const SkScalar dx = localRect->width() / rect->width();
450 const SkScalar dy = localRect->height() / rect->height();
451 if (clipBounds.fLeft > rect->fLeft) {
452 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
453 rect->fLeft = clipBounds.fLeft;
454 }
455 if (clipBounds.fTop > rect->fTop) {
456 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
457 rect->fTop = clipBounds.fTop;
458 }
459 if (clipBounds.fRight < rect->fRight) {
460 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
461 rect->fRight = clipBounds.fRight;
462 }
463 if (clipBounds.fBottom < rect->fBottom) {
464 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
465 rect->fBottom = clipBounds.fBottom;
466 }
467 return true;
468 }
469
470 return rect->intersect(clipBounds);
471}
472
Brian Osman11052242016-10-27 14:47:55 -0400473bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500474 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400476 const SkMatrix& viewMatrix,
477 const SkRect& rect,
478 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700479 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500480 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700481 return true;
482 }
robertphillips44302392016-07-08 14:43:03 -0700483
Brian Salomon7c8460e2017-05-12 11:36:10 -0400484 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400485 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 if (GrAAType::kCoverage == aaType) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400487 op = GrRectOpFactory::MakeAAFill(fContext, std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800488 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400489 op = GrRectOpFactory::MakeNonAAFill(fContext, std::move(paint), viewMatrix, croppedRect,
490 aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800491 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400492 if (!op) {
493 return false;
494 }
495 this->addDrawOp(clip, std::move(op));
496 return true;
robertphillips391395d2016-03-02 09:26:36 -0800497}
498
Brian Osman11052242016-10-27 14:47:55 -0400499void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500500 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500501 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400502 const SkMatrix& viewMatrix,
503 const SkRect& rect,
504 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700505 if (!style) {
506 style = &GrStyle::SimpleFill();
507 }
joshualitt1de610a2016-01-06 08:26:09 -0800508 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700509 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700510 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400511 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700512
bsalomon6663acf2016-05-10 09:14:17 -0700513 // Path effects should've been devolved to a path in SkGpuDevice
514 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700515
Robert Phillips72152832017-01-25 17:31:35 -0500516 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700517
bsalomon6663acf2016-05-10 09:14:17 -0700518 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400519 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400520 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
521 // checking cases where the RT is fully inside a stroke.
522 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
523 // Does the clip contain the entire RT?
Robert Phillips8c8b0462018-08-24 16:18:03 -0400524 if (clip.quickContains(rtRect) && !paint.numCoverageFragmentProcessors()) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400525 SkMatrix invM;
526 if (!viewMatrix.invert(&invM)) {
527 return;
528 }
529 // Does the rect bound the RT?
Brian Salomona33b67c2018-05-17 10:42:14 -0400530 GrQuad quad(rtRect, invM);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400531 if (rect_contains_inclusive(rect, quad.point(0)) &&
532 rect_contains_inclusive(rect, quad.point(1)) &&
533 rect_contains_inclusive(rect, quad.point(2)) &&
534 rect_contains_inclusive(rect, quad.point(3))) {
535 // Will it blend?
536 GrColor clearColor;
537 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700538 this->clear(nullptr, clearColor,
539 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700540 return;
541 }
542 }
543 }
robertphillips44302392016-07-08 14:43:03 -0700544
Brian Salomon82f44312017-01-11 13:42:54 -0500545 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700546 return;
547 }
bsalomona7d85ba2016-07-06 11:54:59 -0700548 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
549 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
550 if ((!rect.width() || !rect.height()) &&
551 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
552 SkScalar r = stroke.getWidth() / 2;
553 // TODO: Move these stroke->fill fallbacks to GrShape?
554 switch (stroke.getJoin()) {
555 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500556 this->drawRect(
557 clip, std::move(paint), aa, viewMatrix,
558 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
559 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700560 return;
561 case SkPaint::kRound_Join:
562 // Raster draws nothing when both dimensions are empty.
563 if (rect.width() || rect.height()){
564 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500565 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
566 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700567 return;
568 }
569 case SkPaint::kBevel_Join:
570 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500571 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700572 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
573 &GrStyle::SimpleFill());
574 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500575 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700576 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
577 &GrStyle::SimpleFill());
578 }
579 return;
580 }
581 }
robertphillips44302392016-07-08 14:43:03 -0700582
Brian Salomonbaaf4392017-06-15 09:59:23 -0400583 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700584
Brian Salomon7c8460e2017-05-12 11:36:10 -0400585 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500586 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800587 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
588 if (viewMatrix.rectStaysRect()) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400589 op = GrRectOpFactory::MakeAAStroke(fContext, std::move(paint), viewMatrix, rect,
590 stroke);
cdaltonbb539482016-01-04 09:48:25 -0800591 }
robertphillipsea461502015-05-26 11:38:03 -0700592 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400593 op = GrRectOpFactory::MakeNonAAStroke(fContext, std::move(paint), viewMatrix, rect,
594 stroke, aaType);
robertphillips391395d2016-03-02 09:26:36 -0800595 }
robertphillips4bc31812016-03-01 12:22:49 -0800596
Brian Salomon42521e82016-12-07 16:44:58 -0500597 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400598 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700599 return;
robertphillips4bc31812016-03-01 12:22:49 -0800600 }
robertphillips4bc31812016-03-01 12:22:49 -0800601 }
Brian Salomon2fad74a2017-12-20 13:28:55 -0500602 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700603}
604
Robert Phillipsec2249f2016-11-09 08:54:35 -0500605int GrRenderTargetContextPriv::maxWindowRectangles() const {
606 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400607 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500608}
609
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000610void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700611 ASSERT_SINGLE_OWNER_PRIV
612 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400613 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400614 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
615 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700616
Robert Phillips72152832017-01-25 17:31:35 -0500617 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400618
Robert Phillips7c525e62018-06-12 10:11:12 -0400619 GrRenderTargetProxy* rtProxy = fRenderTargetContext->fRenderTargetProxy.get();
620 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fRenderTargetContext->fContext,
621 clip, insideStencilMask, rtProxy));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400622 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500623 return;
624 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400625 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700626}
627
Chris Daltonbbfd5162017-11-07 13:35:22 -0700628void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500629 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400630 const SkMatrix& viewMatrix,
631 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500632 ASSERT_SINGLE_OWNER_PRIV
633 RETURN_IF_ABANDONED_PRIV
634 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400635 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
636 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500637
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500638 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500639
640 bool useHWAA = GrAATypeIsHW(aaType);
641 // TODO: extract portions of checkDraw that are relevant to path stenciling.
642 SkASSERT(path);
643 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
644
645 // FIXME: Use path bounds instead of this WAR once
646 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
647 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
648
649 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700650 GrAppliedHardClip appliedClip;
651 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
652 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500653 return;
654 }
655
Robert Phillips65048132017-08-10 08:44:49 -0400656 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500657
Robert Phillips7c525e62018-06-12 10:11:12 -0400658 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
659 viewMatrix,
Brian Salomon467921e2017-03-06 16:17:12 -0500660 useHWAA,
661 path->getFillType(),
662 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500663 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500664 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400665 if (!op) {
666 return;
667 }
Brian Salomon97180af2017-03-14 13:42:58 -0400668 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400669 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700670}
671
Chris Daltonbbfd5162017-11-07 13:35:22 -0700672void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400673 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500674 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400675 const SkMatrix& viewMatrix,
676 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700677 ASSERT_SINGLE_OWNER_PRIV
678 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400679 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400680 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
681 fRenderTargetContext->fContext);
682
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500683 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500684 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700685
686 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500687 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400688 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(fRenderTargetContext->fContext,
689 std::move(paint), viewMatrix,
690 rect, aaType, ss);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400691 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700692}
693
Chris Daltonbbfd5162017-11-07 13:35:22 -0700694bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400695 const GrUserStencilSettings* ss,
696 SkRegion::Op op,
697 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500698 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400699 const SkMatrix& viewMatrix,
700 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800701 ASSERT_SINGLE_OWNER_PRIV
702 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400703 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400704 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
705 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800706
Robert Phillips72152832017-01-25 17:31:35 -0500707 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800708
709 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800710 paint.setCoverageSetOpXPFactory(op, invert);
711
Brian Salomon82f44312017-01-11 13:42:54 -0500712 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800713 return true;
714 }
robertphillips391395d2016-03-02 09:26:36 -0800715 SkPath path;
716 path.setIsVolatile(true);
717 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500718 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800719}
720
Brian Osman11052242016-10-27 14:47:55 -0400721void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500722 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500723 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400724 const SkMatrix& viewMatrix,
725 const SkRect& rectToDraw,
726 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800727 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700728 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700729 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400730 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700731
csmartdalton97f6cd52016-07-13 13:37:08 -0700732 SkRect croppedRect = rectToDraw;
733 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700734 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
735 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700736 return;
737 }
738
Robert Phillips72152832017-01-25 17:31:35 -0500739 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700740
Brian Salomon7c8460e2017-05-12 11:36:10 -0400741 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500742 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400743 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400744 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400745 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700746 return;
joshualitt04194f32016-01-13 10:08:27 -0800747 }
bsalomonbb243832016-07-22 07:10:19 -0700748
Brian Salomonbaaf4392017-06-15 09:59:23 -0400749 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400750 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400751 if (op) {
752 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700753 return;
754 }
755
756 SkMatrix viewAndUnLocalMatrix;
757 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
758 SkDebugf("fillRectToRect called with empty local matrix.\n");
759 return;
760 }
761 viewAndUnLocalMatrix.postConcat(viewMatrix);
762
Brian Salomon2fad74a2017-12-20 13:28:55 -0500763 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
764 GrShape(localRect));
joshualittb6b513b2015-08-21 10:25:18 -0700765}
766
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400767void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
768 GrSamplerState::Filter filter, GrColor color,
Brian Salomon2213ee92018-10-02 10:44:21 -0400769 const SkRect& srcRect, const SkRect& dstRect,
770 GrQuadAAFlags aaFlags,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400771 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400772 const SkMatrix& viewMatrix,
Brian Osman3ebd3542018-07-30 14:36:53 -0400773 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
774 sk_sp<GrColorSpaceXform> paintColorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400775 ASSERT_SINGLE_OWNER
776 RETURN_IF_ABANDONED
777 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400778 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomonf1709042018-10-03 11:57:00 -0400779 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
780 srcRect.contains(proxy->getWorstCaseBoundsRect())) {
781 constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon34169692017-08-28 15:32:01 -0400782 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400783 GrAAType aaType =
784 this->chooseAAType(GrAA(aaFlags != GrQuadAAFlags::kNone), GrAllowMixedSamples::kNo);
Brian Salomonff9d6d32017-08-30 10:27:49 -0400785 SkRect clippedDstRect = dstRect;
786 SkRect clippedSrcRect = srcRect;
787 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
788 &clippedSrcRect)) {
789 return;
790 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400791 auto op = GrTextureOp::Make(fContext, std::move(proxy), filter, color, clippedSrcRect,
792 clippedDstRect, aaType, aaFlags, constraint, viewMatrix,
793 std::move(textureColorSpaceXform), std::move(paintColorSpaceXform));
794 this->addDrawOp(clip, std::move(op));
Brian Salomon34169692017-08-28 15:32:01 -0400795}
796
Brian Osman11052242016-10-27 14:47:55 -0400797void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500798 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500799 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400800 const SkMatrix& viewMatrix,
801 const SkRect& rectToDraw,
802 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800803 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700804 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700805 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400806 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700807
csmartdalton97f6cd52016-07-13 13:37:08 -0700808 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700809 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700810 return;
811 }
812
Robert Phillips72152832017-01-25 17:31:35 -0500813 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700814
Brian Salomon7c8460e2017-05-12 11:36:10 -0400815 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500816 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400817 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400818 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400819 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700820 return;
bsalomonc55271f2015-11-09 11:55:57 -0800821 }
robertphillips4bc31812016-03-01 12:22:49 -0800822
Brian Salomonbaaf4392017-06-15 09:59:23 -0400823 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400824 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400825 if (op) {
826 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700827 return;
828 }
829
830 SkMatrix viewAndUnLocalMatrix;
831 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
832 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
833 return;
834 }
835 viewAndUnLocalMatrix.postConcat(viewMatrix);
836
837 SkPath path;
838 path.setIsVolatile(true);
839 path.addRect(rectToDraw);
840 path.transform(localMatrix);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500841 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
842 GrShape(path));
robertphillipsea461502015-05-26 11:38:03 -0700843}
844
Brian Osman11052242016-10-27 14:47:55 -0400845void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500846 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400847 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400848 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400849 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400850 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400851 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500852 ASSERT_SINGLE_OWNER
853 RETURN_IF_ABANDONED
854 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400855 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500856
857 AutoCheckFlush acf(this->drawingManager());
858
859 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400860 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400861 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400862 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400863 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400864 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700865}
866
867///////////////////////////////////////////////////////////////////////////////
868
Brian Osman11052242016-10-27 14:47:55 -0400869void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500870 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400871 const SkMatrix& viewMatrix,
872 int spriteCount,
873 const SkRSXform xform[],
874 const SkRect texRect[],
875 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800876 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700877 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700878 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400879 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700880
Robert Phillips72152832017-01-25 17:31:35 -0500881 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700882
Brian Salomon0088f942017-07-12 11:51:27 -0400883 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -0400884 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
885 aaType, spriteCount, xform, texRect, colors);
Brian Salomon0088f942017-07-12 11:51:27 -0400886 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700887}
888
889///////////////////////////////////////////////////////////////////////////////
890
Brian Osman11052242016-10-27 14:47:55 -0400891void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500892 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500893 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400894 const SkMatrix& viewMatrix,
895 const SkRRect& rrect,
896 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800897 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700898 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700899 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400900 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400901
902 const SkStrokeRec& stroke = style.strokeRec();
903 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700904 return;
905 }
906
bsalomon7f0d9f32016-08-15 14:49:10 -0700907 GrNoClip noclip;
908 const GrClip* clip = &origClip;
909#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
910 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500911 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700912 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
913 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
914 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
915 SkRRect devRRect;
916 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
917 clip = &noclip;
918 }
919#endif
bsalomon6663acf2016-05-10 09:14:17 -0700920 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700921
Robert Phillips72152832017-01-25 17:31:35 -0500922 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700923
Brian Salomonea26d6b2018-01-23 20:33:21 +0000924 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500925 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400926 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -0400927 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(fContext,
928 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +0000929 viewMatrix,
930 rrect,
931 stroke,
932 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500933 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400934 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800935 return;
936 }
robertphillipsea461502015-05-26 11:38:03 -0700937 }
robertphillipsb56f9272016-02-25 11:03:52 -0800938
Brian Salomon2fad74a2017-12-20 13:28:55 -0500939 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
940 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700941}
942
Jim Van Verthc5903412016-11-17 15:27:09 -0500943///////////////////////////////////////////////////////////////////////////////
944
Jim Van Verth3af1af92017-05-18 15:06:54 -0400945static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
946 SkPoint3 result;
947 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
948 result.fZ = pt.fZ;
949 return result;
950}
951
952bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400953 const SkMatrix& viewMatrix,
954 const SkPath& path,
955 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500956 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400957 if (this->drawingManager()->wasAbandoned()) {
958 return true;
959 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500960 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400961 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400962
963 // check z plane
964 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
965 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
966 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
967 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
968 return false;
969 }
970
971 SkRRect rrect;
972 SkRect rect;
973 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500974 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400975 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
976 if (!isRRect &&
977 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
978 rect.width() > SK_ScalarNearlyZero) {
979 rrect.setOval(rect);
980 isRRect = true;
981 }
982 if (!isRRect && path.isRect(&rect)) {
983 rrect.setRect(rect);
984 isRRect = true;
985 }
986
987 if (!isRRect) {
988 return false;
989 }
990
Jim Van Verthc5903412016-11-17 15:27:09 -0500991 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400992 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500993 }
994
Robert Phillips72152832017-01-25 17:31:35 -0500995 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -0500996
Jim Van Verth3af1af92017-05-18 15:06:54 -0400997 // transform light
998 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
999
1000 // 1/scale
1001 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1002 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1003 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1004 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1005
1006 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001007 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1008
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001009 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001010 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1011 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1012 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001013
1014 // Outset the shadow rrect to the border of the penumbra
1015 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1016 SkRRect ambientRRect;
1017 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1018 // If the rrect was an oval then its outset will also be one.
1019 // We set it explicitly to avoid errors.
1020 if (rrect.isOval()) {
1021 ambientRRect = SkRRect::MakeOval(outsetRect);
1022 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001023 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001024 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1025 }
1026
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001027 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001028 if (transparent) {
1029 // set a large inset to force a fill
1030 devSpaceInsetWidth = ambientRRect.width();
1031 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001032
Robert Phillips7c525e62018-06-12 10:11:12 -04001033 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1034 ambientColor,
1035 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001036 ambientRRect,
1037 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001038 devSpaceInsetWidth);
Brian Salomon05969092017-07-13 11:20:51 -04001039 SkASSERT(op);
1040 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001041 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001042
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001043 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001044 SkScalar devSpaceSpotBlur;
1045 SkScalar spotScale;
1046 SkVector spotOffset;
1047 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1048 devLightPos.fZ, rec.fLightRadius,
1049 &devSpaceSpotBlur, &spotScale, &spotOffset);
1050 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001051 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1052
Jim Van Verth3af1af92017-05-18 15:06:54 -04001053 // Adjust translate for the effect of the scale.
1054 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1055 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1056 // This offset is in dev space, need to transform it into source space.
1057 SkMatrix ctmInverse;
1058 if (viewMatrix.invert(&ctmInverse)) {
1059 ctmInverse.mapPoints(&spotOffset, 1);
1060 } else {
1061 // Since the matrix is a similarity, this should never happen, but just in case...
1062 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1063 SkASSERT(false);
1064 }
1065
1066 // Compute the transformed shadow rrect
1067 SkRRect spotShadowRRect;
1068 SkMatrix shadowTransform;
1069 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1070 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001071 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001072
1073 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001074 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001075 SkScalar insetWidth = blurOutset;
1076 if (transparent) {
1077 // If transparent, just do a fill
1078 insetWidth += spotShadowRRect.width();
1079 } else {
1080 // For shadows, instead of using a stroke we specify an inset from the penumbra
1081 // border. We want to extend this inset area so that it meets up with the caster
1082 // geometry. The inset geometry will by default already be inset by the blur width.
1083 //
1084 // We compare the min and max corners inset by the radius between the original
1085 // rrect and the shadow rrect. The distance between the two plus the difference
1086 // between the scaled radius and the original radius gives the distance from the
1087 // transformed shadow shape to the original shape in that corner. The max
1088 // of these gives the maximum distance we need to cover.
1089 //
1090 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1091 // that to get the full insetWidth.
1092 SkScalar maxOffset;
1093 if (rrect.isRect()) {
1094 // Manhattan distance works better for rects
1095 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1096 rrect.rect().fLeft),
1097 SkTAbs(spotShadowRRect.rect().fTop -
1098 rrect.rect().fTop)),
1099 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1100 rrect.rect().fRight),
1101 SkTAbs(spotShadowRRect.rect().fBottom -
1102 rrect.rect().fBottom)));
1103 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001104 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001105 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1106 rrect.rect().fLeft + dr,
1107 spotShadowRRect.rect().fTop -
1108 rrect.rect().fTop + dr);
1109 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1110 rrect.rect().fRight - dr,
1111 spotShadowRRect.rect().fBottom -
1112 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001113 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1114 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001115 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001116 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001117 }
1118
1119 // Outset the shadow rrect to the border of the penumbra
1120 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1121 if (spotShadowRRect.isOval()) {
1122 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1123 } else {
1124 SkScalar outsetRad = spotRadius + blurOutset;
1125 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1126 }
1127
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001128 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001129
Robert Phillips7c525e62018-06-12 10:11:12 -04001130 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1131 spotColor,
1132 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001133 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001134 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001135 insetWidth);
1136 SkASSERT(op);
1137 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001138 }
1139
1140 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001141}
1142
1143///////////////////////////////////////////////////////////////////////////////
1144
Brian Osman11052242016-10-27 14:47:55 -04001145bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001146 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001147 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001148 const SkMatrix& viewMatrix,
1149 const SkRRect& origOuter,
1150 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001151 SkASSERT(!origInner.isEmpty());
1152 SkASSERT(!origOuter.isEmpty());
1153
Brian Salomon65749212017-12-01 16:01:47 -05001154 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1155
Brian Salomon45839f92017-12-04 09:02:35 -05001156 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1157
1158 if (GrAAType::kMSAA == aaType) {
1159 return false;
1160 }
1161
Mike Reed242135a2018-02-22 13:41:39 -05001162 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1163 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001164 auto outerR = outer->width() / 2.f;
1165 auto innerR = inner->width() / 2.f;
1166 auto cx = outer->getBounds().fLeft + outerR;
1167 auto cy = outer->getBounds().fTop + outerR;
1168 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1169 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1170 auto avgR = (innerR + outerR) / 2.f;
1171 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1172 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1173 stroke.setStrokeStyle(outerR - innerR);
Robert Phillips7c525e62018-06-12 10:11:12 -04001174 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
1175 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001176 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001177 if (op) {
1178 this->addDrawOp(clip, std::move(op));
1179 return true;
1180 }
1181 }
1182 }
1183
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001184 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001185 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001186 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1187 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001188 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001189 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1190 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001191 }
robertphillips00095892016-02-29 13:50:40 -08001192
robertphillips00095892016-02-29 13:50:40 -08001193 SkMatrix inverseVM;
1194 if (!viewMatrix.isIdentity()) {
1195 if (!origInner.transform(viewMatrix, inner.writable())) {
1196 return false;
1197 }
1198 if (!origOuter.transform(viewMatrix, outer.writable())) {
1199 return false;
1200 }
1201 if (!viewMatrix.invert(&inverseVM)) {
1202 return false;
1203 }
1204 } else {
1205 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001206 }
robertphillips00095892016-02-29 13:50:40 -08001207
Brian Salomon14471772017-12-05 10:35:15 -05001208 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001209 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001210 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001211 if (!innerEffect) {
1212 return false;
1213 }
1214
Brian Salomon14471772017-12-05 10:35:15 -05001215 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001216 if (!outerEffect) {
1217 return false;
1218 }
1219
Brian Salomon82f44312017-01-11 13:42:54 -05001220 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1221 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001222
1223 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001224 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001225 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1226 }
halcanary9d524f22016-03-29 09:03:52 -07001227
Brian Salomon82f44312017-01-11 13:42:54 -05001228 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1229 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001230 return true;
1231}
1232
Brian Osman11052242016-10-27 14:47:55 -04001233void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001234 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001235 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001236 const SkMatrix& viewMatrix,
1237 const SkRRect& outer,
1238 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001239 ASSERT_SINGLE_OWNER
1240 RETURN_IF_ABANDONED
1241 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001242 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001243
1244 SkASSERT(!outer.isEmpty());
1245 SkASSERT(!inner.isEmpty());
1246
Robert Phillips72152832017-01-25 17:31:35 -05001247 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001248
Brian Salomon82f44312017-01-11 13:42:54 -05001249 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001250 return;
1251 }
1252
1253 SkPath path;
1254 path.setIsVolatile(true);
1255 path.addRRect(inner);
1256 path.addRRect(outer);
1257 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001258 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001259}
1260
robertphillipsea461502015-05-26 11:38:03 -07001261///////////////////////////////////////////////////////////////////////////////
1262
Brian Osman11052242016-10-27 14:47:55 -04001263void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001264 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001265 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001266 const SkMatrix& viewMatrix,
1267 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001268 const GrStyle& style,
1269 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001270 ASSERT_SINGLE_OWNER
1271 RETURN_IF_ABANDONED
1272 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001273 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001274
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001275 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001276 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001277 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001278 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001279 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1280 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001281 aa = GrAA::kNo;
1282 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001283 }
msarettcc319b92016-08-25 18:07:18 -07001284 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001285 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001286 SkPath path;
1287 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001288 path.setIsVolatile(true);
1289
Brian Salomon82f44312017-01-11 13:42:54 -05001290 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001291 }
1292
Brian Salomonf0366322017-07-11 15:53:05 -04001293 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001294 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1295 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001296 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001297}
1298
Brian Osman11052242016-10-27 14:47:55 -04001299void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001300 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001301 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001302 const SkMatrix& viewMatrix,
1303 const SkRect& oval,
1304 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001305 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001306 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001307 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001308 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001309
Robert Phillips7484d202018-07-03 09:09:08 -04001310 const SkStrokeRec& stroke = style.strokeRec();
1311
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001312 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001313 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1314 return;
1315 }
1316
1317 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001318 return;
robertphillipsea461502015-05-26 11:38:03 -07001319 }
1320
Robert Phillips72152832017-01-25 17:31:35 -05001321 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001322
Brian Salomonea26d6b2018-01-23 20:33:21 +00001323 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001324 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001325 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001326 if (auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval,
1327 style, shaderCaps)) {
Brian Salomon05441c42017-05-15 16:45:49 -04001328 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001329 return;
1330 }
robertphillipsea461502015-05-26 11:38:03 -07001331 }
robertphillipsb56f9272016-02-25 11:03:52 -08001332
Brian Salomon5209d7f2018-04-20 16:52:42 -04001333 this->drawShapeUsingPathRenderer(
1334 clip, std::move(paint), aa, viewMatrix,
1335 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001336}
1337
Brian Osman11052242016-10-27 14:47:55 -04001338void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001339 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001340 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001341 const SkMatrix& viewMatrix,
1342 const SkRect& oval,
1343 SkScalar startAngle,
1344 SkScalar sweepAngle,
1345 bool useCenter,
1346 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001347 ASSERT_SINGLE_OWNER
1348 RETURN_IF_ABANDONED
1349 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001350 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001351
1352 AutoCheckFlush acf(this->drawingManager());
1353
Brian Salomonea26d6b2018-01-23 20:33:21 +00001354 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001355 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001356 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001357 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1358 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +00001359 viewMatrix,
1360 oval,
1361 startAngle,
1362 sweepAngle,
1363 useCenter,
1364 style,
1365 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001366 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001367 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001368 return;
1369 }
1370 }
Brian Salomone4949402018-04-26 15:22:04 -04001371 this->drawShapeUsingPathRenderer(
1372 clip, std::move(paint), aa, viewMatrix,
1373 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001374}
1375
Brian Osman11052242016-10-27 14:47:55 -04001376void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001377 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001378 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001379 sk_sp<GrTextureProxy> image,
1380 sk_sp<GrColorSpaceXform> csxf,
1381 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001382 std::unique_ptr<SkLatticeIter> iter,
1383 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001384 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001385 RETURN_IF_ABANDONED
1386 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001387 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001388
Robert Phillips72152832017-01-25 17:31:35 -05001389 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001390
Brian Salomon2a943df2018-05-04 13:43:19 -04001391 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001392 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1393 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001394 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001395}
1396
Greg Daniel51316782017-08-02 15:10:09 +00001397GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1398 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001399 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001400 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001401 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001402 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001403
Greg Daniel51316782017-08-02 15:10:09 +00001404 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1405 numSemaphores,
1406 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001407}
1408
Greg Danielc64ee462017-06-15 16:59:49 -04001409bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001410 const GrBackendSemaphore* waitSemaphores) {
1411 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001412 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001413 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001414 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001415
1416 AutoCheckFlush acf(this->drawingManager());
1417
Greg Danielc64ee462017-06-15 16:59:49 -04001418 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1419 return false;
1420 }
1421
Robert Phillips6be756b2018-01-16 15:07:54 -05001422 auto resourceProvider = fContext->contextPriv().resourceProvider();
1423
Greg Daniela5cb7812017-06-16 09:45:32 -04001424 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1425 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001426 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001427 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1428 kAdopt_GrWrapOwnership);
Robert Phillips7c525e62018-06-12 10:11:12 -04001429 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, sema,
1430 fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001431 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001432 }
Greg Danielc64ee462017-06-15 16:59:49 -04001433 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001434}
joshualitt33a5fce2015-11-18 13:28:51 -08001435
Robert Phillips65a88fa2017-08-08 08:36:22 -04001436void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001437 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001438 this->getRTOpList()->addOp(std::move(op), *this->caps());
1439}
1440
Brian Osman11052242016-10-27 14:47:55 -04001441void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001442 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001443 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001444 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001445 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001446 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001447 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001448 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001449 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001450 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1451
Brian Salomon40b77a62017-12-22 11:25:52 -05001452 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001453
Robert Phillips27927a52018-08-20 13:18:12 -04001454 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001455}
1456
1457void GrRenderTargetContext::drawShape(const GrClip& clip,
1458 GrPaint&& paint,
1459 GrAA aa,
1460 const SkMatrix& viewMatrix,
1461 const GrShape& shape) {
1462 ASSERT_SINGLE_OWNER
1463 RETURN_IF_ABANDONED
1464 SkDEBUGCODE(this->validate();)
1465 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1466
Brian Salomon2fad74a2017-12-20 13:28:55 -05001467 if (shape.isEmpty()) {
1468 if (shape.inverseFilled()) {
1469 this->drawPaint(clip, std::move(paint), viewMatrix);
1470 }
1471 return;
robertphillipsea461502015-05-26 11:38:03 -07001472 }
1473
Robert Phillips72152832017-01-25 17:31:35 -05001474 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001475
Brian Salomon2fad74a2017-12-20 13:28:55 -05001476 if (!shape.style().hasPathEffect()) {
Robert Phillips73653b42018-08-22 12:42:42 -04001477 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001478 SkRRect rrect;
1479 // We can ignore the starting point and direction since there is no path effect.
1480 bool inverted;
1481 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1482 if (rrect.isRect()) {
1483 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1484 &shape.style());
1485 return;
1486 } else if (rrect.isOval()) {
1487 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001488 return;
1489 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001490 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1491 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001492 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1493 viewMatrix.rectStaysRect()) {
1494 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1495 // the matrix to all the points individually rather than just to the rect
1496 SkRect rects[2];
1497 if (shape.asNestedRects(rects)) {
1498 // Concave AA paths are expensive - try to avoid them for special cases
1499 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillNestedRects(
1500 fContext, std::move(paint), viewMatrix, rects);
1501 if (op) {
1502 this->addDrawOp(clip, std::move(op));
1503 }
1504 // Returning here indicates that there is nothing to draw in this case.
1505 return;
1506 }
robertphillipsea461502015-05-26 11:38:03 -07001507 }
1508 }
robertphillips4bc31812016-03-01 12:22:49 -08001509
Brian Salomon2fad74a2017-12-20 13:28:55 -05001510 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001511}
1512
Chris Daltonbbfd5162017-11-07 13:35:22 -07001513bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001514 const GrUserStencilSettings* ss,
1515 SkRegion::Op op,
1516 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001517 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001518 const SkMatrix& viewMatrix,
1519 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001520 ASSERT_SINGLE_OWNER_PRIV
1521 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001522 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001523 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1524 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001525
1526 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001527 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001528 SkRect::MakeIWH(fRenderTargetContext->width(),
1529 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001530 return true;
1531 }
1532
Robert Phillips72152832017-01-25 17:31:35 -05001533 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001534
1535 // An Assumption here is that path renderer would use some form of tweaking
1536 // the src color (either the input alpha or in the frag shader) to implement
1537 // aa. If we have some future driver-mojo path AA that can do the right
1538 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001539 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001540 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001541
Chris Daltondb91c6e2017-09-08 16:25:08 -06001542 SkIRect clipConservativeBounds;
1543 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1544 &clipConservativeBounds, nullptr);
1545
bsalomon8acedde2016-06-24 10:42:16 -07001546 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001547 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001548 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001549 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001550 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001551 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001552 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001553 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001554
1555 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001556 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001557 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001558 if (!pr) {
1559 return false;
1560 }
1561
1562 GrPaint paint;
1563 paint.setCoverageSetOpXPFactory(op, invert);
1564
Brian Salomonf3569f02017-10-24 12:52:33 -04001565 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1566 std::move(paint),
1567 ss,
1568 fRenderTargetContext,
1569 &clip,
1570 &clipConservativeBounds,
1571 &viewMatrix,
1572 &shape,
1573 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001574 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001575 pr->drawPath(args);
1576 return true;
1577}
1578
Brian Osman11052242016-10-27 14:47:55 -04001579SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001580 ASSERT_SINGLE_OWNER_PRIV
1581
Brian Osman11052242016-10-27 14:47:55 -04001582 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001583 return SkBudgeted::kNo;
1584 }
1585
Brian Osman11052242016-10-27 14:47:55 -04001586 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001587
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001588 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001589}
1590
Brian Salomon2fad74a2017-12-20 13:28:55 -05001591void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1592 GrPaint&& paint,
1593 GrAA aa,
1594 const SkMatrix& viewMatrix,
1595 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001596 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001597 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001598 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1599
Jim Van Verthf86073a2018-10-02 13:05:38 -04001600 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1601 return;
1602 }
1603
Chris Daltondb91c6e2017-09-08 16:25:08 -06001604 SkIRect clipConservativeBounds;
1605 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1606
Brian Salomon2fad74a2017-12-20 13:28:55 -05001607 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001608 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1609 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1610 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001611 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1612 ? GrAllowMixedSamples::kNo
1613 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001614 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001615 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001616 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001617 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001618 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001619 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001620 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001621
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001622 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001623 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001624 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001625 return;
1626 }
1627
1628 canDrawArgs.fAAType = aaType;
1629
1630 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1631 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1632 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1633
Brian Salomon2fad74a2017-12-20 13:28:55 -05001634 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001635 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001636 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1637 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001638 return;
1639 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001640 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001641 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001642 }
1643 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001644 if (canDrawArgs.fShape->style().applies()) {
1645 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1646 styleScale);
1647 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001648 return;
1649 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001650 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001651 // This time, allow SW renderer
1652 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1653 } else {
1654 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001655 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001656 }
robertphillipsea461502015-05-26 11:38:03 -07001657
bsalomon8acedde2016-06-24 10:42:16 -07001658 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001659#ifdef SK_DEBUG
1660 SkDebugf("Unable to find path renderer compatible with path.\n");
1661#endif
1662 return;
1663 }
1664
Robert Phillips256c37b2017-03-01 14:32:46 -05001665 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001666 std::move(paint),
1667 &GrUserStencilSettings::kUnused,
1668 this,
1669 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001670 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001671 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001672 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001673 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001674 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001675 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001676}
1677
Brian Salomon467921e2017-03-06 16:17:12 -05001678static void op_bounds(SkRect* bounds, const GrOp* op) {
1679 *bounds = op->bounds();
1680 if (op->hasZeroArea()) {
1681 if (op->hasAABloat()) {
1682 bounds->outset(0.5f, 0.5f);
1683 } else {
1684 // We don't know which way the particular GPU will snap lines or points at integer
1685 // coords. So we ensure that the bounds is large enough for either snap.
1686 SkRect before = *bounds;
1687 bounds->roundOut(bounds);
1688 if (bounds->fLeft == before.fLeft) {
1689 bounds->fLeft -= 1;
1690 }
1691 if (bounds->fTop == before.fTop) {
1692 bounds->fTop -= 1;
1693 }
1694 if (bounds->fRight == before.fRight) {
1695 bounds->fRight += 1;
1696 }
1697 if (bounds->fBottom == before.fBottom) {
1698 bounds->fBottom += 1;
1699 }
1700 }
1701 }
1702}
1703
Brian Salomon54d212e2017-03-21 14:22:38 -04001704uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001705 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001706 if (this->drawingManager()->wasAbandoned()) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001707 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Robert Phillipsc0138922017-03-08 11:50:55 -05001708 return SK_InvalidUniqueID;
1709 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001710 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001711 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001712
Brian Salomon467921e2017-03-06 16:17:12 -05001713 // Setup clip
1714 SkRect bounds;
1715 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001716 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001717 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1718 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1719 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1720 &bounds)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001721 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001722 return SK_InvalidUniqueID;
1723 }
1724
Brian Salomon54d212e2017-03-21 14:22:38 -04001725 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1726 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001727 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001728
Robert Phillips65048132017-08-10 08:44:49 -04001729 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001730 }
1731
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001732 GrXferProcessor::DstProxy dstProxy;
Brian Osman532b3f92018-07-11 10:02:07 -04001733 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001734 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001735 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001736 return SK_InvalidUniqueID;
1737 }
1738 }
1739
1740 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001741 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1742 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001743}
1744
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001745bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001746 const SkRect& opBounds,
1747 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001748 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001749 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001750 // The render target is a texture, so we can read from it directly in the shader. The XP
1751 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001752 dstProxy->setProxy(sk_ref_sp(texProxy));
1753 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001754 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001755 }
1756 }
1757
Robert Phillipsbf25d432017-04-07 10:08:53 -04001758 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001759
Eric Karl74480882017-04-03 14:49:05 -07001760 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001761 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001762 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001763 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001764 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1765 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001766 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001767#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001768 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001769#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001770 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001771 }
1772
1773 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1774 // have per-sample dst values by making the copy multisampled.
1775 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001776 bool rectsMustMatch = false;
1777 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001778 GrSurfaceOrigin origin;
1779 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
1780 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001781 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001782 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04001783 origin = rtProxy->origin();
Brian Salomon467921e2017-03-06 16:17:12 -05001784 }
1785
Eric Karl74480882017-04-03 14:49:05 -07001786 if (!disallowSubrect) {
1787 copyRect = clippedRect;
1788 }
Brian Salomon467921e2017-03-06 16:17:12 -05001789
Robert Phillipsbf25d432017-04-07 10:08:53 -04001790 SkIPoint dstPoint, dstOffset;
1791 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001792 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001793 desc.fWidth = rtProxy->width();
1794 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001795 dstPoint = {copyRect.fLeft, copyRect.fTop};
1796 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001797 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001798 } else {
1799 desc.fWidth = copyRect.width();
1800 desc.fHeight = copyRect.height();
1801 dstPoint = {0, 0};
1802 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001803 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001804 }
Brian Salomon467921e2017-03-06 16:17:12 -05001805
Robert Phillipsbf25d432017-04-07 10:08:53 -04001806 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -05001807 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05001808 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001809 if (!sContext) {
1810 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1811 return false;
1812 }
1813
1814 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1815 SkDebugf("setupDstTexture: copy failed.\n");
1816 return false;
1817 }
1818
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001819 dstProxy->setProxy(sContext->asTextureProxyRef());
1820 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001821 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001822}