blob: 2fbf7db57ec4e536e668989cbbc82988d13a1fb7 [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 }
Michael Ludwig43a54972018-10-09 18:02:19 -0400373 // If the clip intersection with the device is a non-rounded rectangle, it could be a
374 // implemented faster as a clear limited by the scissor test. Not all rectangular clips can
375 // be converted into a simple clear (see GrReducedClip), but for non-AA and "AA rects that
376 // line up with pixel boundaries", we can map it to a clear using the rounded rect.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400377 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
Michael Ludwig43a54972018-10-09 18:02:19 -0400378 if (rrectState == kValid && rrect.isRect() && (aa == GrAA::kNo ||
379 GrClip::IsPixelAligned(rrect.getBounds()))) {
380 SkIRect scissorRect;
381 rrect.getBounds().round(&scissorRect);
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400382 this->clear(&scissorRect, clearColor, CanClearFullscreen::kNo);
383 return;
384 }
385 }
386
bsalomoncb31e512016-08-26 10:48:19 -0700387 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
388 // transformation for non-rect rrects. Rects caused a performance regression on an Android
389 // test that needs investigation. We also skip cases where there are fragment processors
390 // because they may depend on having correct local coords and this path draws in device space
391 // without a local matrix.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400392 if (!paint.numTotalFragmentProcessors()) {
393 if (rrectState == kUnknown) {
394 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
395 }
396 if (rrectState == kValid && !rrect.isRect()) {
397 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
398 GrStyle::SimpleFill());
399 return;
400 }
bsalomoncb31e512016-08-26 10:48:19 -0700401 }
402
robertphillipsea461502015-05-26 11:38:03 -0700403 bool isPerspective = viewMatrix.hasPerspective();
404
405 // We attempt to map r by the inverse matrix and draw that. mapRect will
406 // map the four corners and bound them with a new rect. This will not
407 // produce a correct result for some perspective matrices.
408 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700409 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700410 return;
411 }
Brian Salomon82f44312017-01-11 13:42:54 -0500412 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700413 } else {
414 SkMatrix localMatrix;
415 if (!viewMatrix.invert(&localMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700416 return;
417 }
418
Robert Phillips72152832017-01-25 17:31:35 -0500419 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700420
Brian Salomonbaaf4392017-06-15 09:59:23 -0400421 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400422 fContext, std::move(paint), SkMatrix::I(), localMatrix, r, GrAAType::kNone);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400423 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700424 }
425}
426
robertphillipsea461502015-05-26 11:38:03 -0700427static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
428 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
429 point.fY >= rect.fTop && point.fY <= rect.fBottom;
430}
431
csmartdalton97f6cd52016-07-13 13:37:08 -0700432// Attempts to crop a rect and optional local rect to the clip boundaries.
433// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700434static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700435 const SkMatrix& viewMatrix, SkRect* rect,
436 SkRect* localRect = nullptr) {
437 if (!viewMatrix.rectStaysRect()) {
438 return true;
439 }
440
csmartdalton97f6cd52016-07-13 13:37:08 -0700441 SkIRect clipDevBounds;
442 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700443
robertphillips13a7eee2016-08-31 15:06:24 -0700444 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700445 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
446 return false;
447 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700448
449 if (localRect) {
450 if (!rect->intersects(clipBounds)) {
451 return false;
452 }
453 const SkScalar dx = localRect->width() / rect->width();
454 const SkScalar dy = localRect->height() / rect->height();
455 if (clipBounds.fLeft > rect->fLeft) {
456 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
457 rect->fLeft = clipBounds.fLeft;
458 }
459 if (clipBounds.fTop > rect->fTop) {
460 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
461 rect->fTop = clipBounds.fTop;
462 }
463 if (clipBounds.fRight < rect->fRight) {
464 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
465 rect->fRight = clipBounds.fRight;
466 }
467 if (clipBounds.fBottom < rect->fBottom) {
468 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
469 rect->fBottom = clipBounds.fBottom;
470 }
471 return true;
472 }
473
474 return rect->intersect(clipBounds);
475}
476
Brian Osman11052242016-10-27 14:47:55 -0400477bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500478 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500479 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400480 const SkMatrix& viewMatrix,
481 const SkRect& rect,
482 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700483 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500484 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700485 return true;
486 }
robertphillips44302392016-07-08 14:43:03 -0700487
Brian Salomon7c8460e2017-05-12 11:36:10 -0400488 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400489 std::unique_ptr<GrDrawOp> op;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500490 if (GrAAType::kCoverage == aaType) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400491 op = GrRectOpFactory::MakeAAFill(fContext, std::move(paint), viewMatrix, croppedRect, ss);
robertphillips391395d2016-03-02 09:26:36 -0800492 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400493 op = GrRectOpFactory::MakeNonAAFill(fContext, std::move(paint), viewMatrix, croppedRect,
494 aaType, ss);
robertphillips391395d2016-03-02 09:26:36 -0800495 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400496 if (!op) {
497 return false;
498 }
499 this->addDrawOp(clip, std::move(op));
500 return true;
robertphillips391395d2016-03-02 09:26:36 -0800501}
502
Brian Osman11052242016-10-27 14:47:55 -0400503void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500504 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500505 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400506 const SkMatrix& viewMatrix,
507 const SkRect& rect,
508 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700509 if (!style) {
510 style = &GrStyle::SimpleFill();
511 }
joshualitt1de610a2016-01-06 08:26:09 -0800512 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700513 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700514 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400515 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700516
bsalomon6663acf2016-05-10 09:14:17 -0700517 // Path effects should've been devolved to a path in SkGpuDevice
518 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700519
Robert Phillips72152832017-01-25 17:31:35 -0500520 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700521
bsalomon6663acf2016-05-10 09:14:17 -0700522 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400523 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400524 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
525 // checking cases where the RT is fully inside a stroke.
526 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
527 // Does the clip contain the entire RT?
Robert Phillips8c8b0462018-08-24 16:18:03 -0400528 if (clip.quickContains(rtRect) && !paint.numCoverageFragmentProcessors()) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400529 SkMatrix invM;
530 if (!viewMatrix.invert(&invM)) {
531 return;
532 }
533 // Does the rect bound the RT?
Brian Salomona33b67c2018-05-17 10:42:14 -0400534 GrQuad quad(rtRect, invM);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400535 if (rect_contains_inclusive(rect, quad.point(0)) &&
536 rect_contains_inclusive(rect, quad.point(1)) &&
537 rect_contains_inclusive(rect, quad.point(2)) &&
538 rect_contains_inclusive(rect, quad.point(3))) {
539 // Will it blend?
540 GrColor clearColor;
541 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700542 this->clear(nullptr, clearColor,
543 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700544 return;
545 }
546 }
547 }
robertphillips44302392016-07-08 14:43:03 -0700548
Brian Salomon82f44312017-01-11 13:42:54 -0500549 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700550 return;
551 }
bsalomona7d85ba2016-07-06 11:54:59 -0700552 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
553 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
554 if ((!rect.width() || !rect.height()) &&
555 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
556 SkScalar r = stroke.getWidth() / 2;
557 // TODO: Move these stroke->fill fallbacks to GrShape?
558 switch (stroke.getJoin()) {
559 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500560 this->drawRect(
561 clip, std::move(paint), aa, viewMatrix,
562 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
563 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700564 return;
565 case SkPaint::kRound_Join:
566 // Raster draws nothing when both dimensions are empty.
567 if (rect.width() || rect.height()){
568 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500569 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
570 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700571 return;
572 }
573 case SkPaint::kBevel_Join:
574 if (!rect.width()) {
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 - r, rect.fTop, rect.fRight + r, rect.fBottom},
577 &GrStyle::SimpleFill());
578 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500579 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700580 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
581 &GrStyle::SimpleFill());
582 }
583 return;
584 }
585 }
robertphillips44302392016-07-08 14:43:03 -0700586
Brian Salomonbaaf4392017-06-15 09:59:23 -0400587 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700588
Brian Salomon7c8460e2017-05-12 11:36:10 -0400589 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500590 if (GrAAType::kCoverage == aaType) {
cdaltonbb539482016-01-04 09:48:25 -0800591 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
592 if (viewMatrix.rectStaysRect()) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400593 op = GrRectOpFactory::MakeAAStroke(fContext, std::move(paint), viewMatrix, rect,
594 stroke);
cdaltonbb539482016-01-04 09:48:25 -0800595 }
robertphillipsea461502015-05-26 11:38:03 -0700596 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400597 op = GrRectOpFactory::MakeNonAAStroke(fContext, std::move(paint), viewMatrix, rect,
598 stroke, aaType);
robertphillips391395d2016-03-02 09:26:36 -0800599 }
robertphillips4bc31812016-03-01 12:22:49 -0800600
Brian Salomon42521e82016-12-07 16:44:58 -0500601 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400602 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700603 return;
robertphillips4bc31812016-03-01 12:22:49 -0800604 }
robertphillips4bc31812016-03-01 12:22:49 -0800605 }
Brian Salomon2fad74a2017-12-20 13:28:55 -0500606 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700607}
608
Robert Phillipsec2249f2016-11-09 08:54:35 -0500609int GrRenderTargetContextPriv::maxWindowRectangles() const {
610 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400611 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500612}
613
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000614void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700615 ASSERT_SINGLE_OWNER_PRIV
616 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400617 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400618 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
619 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700620
Robert Phillips72152832017-01-25 17:31:35 -0500621 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400622
Robert Phillips7c525e62018-06-12 10:11:12 -0400623 GrRenderTargetProxy* rtProxy = fRenderTargetContext->fRenderTargetProxy.get();
624 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fRenderTargetContext->fContext,
625 clip, insideStencilMask, rtProxy));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400626 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500627 return;
628 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400629 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700630}
631
Chris Daltonbbfd5162017-11-07 13:35:22 -0700632void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500633 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400634 const SkMatrix& viewMatrix,
635 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500636 ASSERT_SINGLE_OWNER_PRIV
637 RETURN_IF_ABANDONED_PRIV
638 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400639 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
640 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500641
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500642 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500643
644 bool useHWAA = GrAATypeIsHW(aaType);
645 // TODO: extract portions of checkDraw that are relevant to path stenciling.
646 SkASSERT(path);
647 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
648
649 // FIXME: Use path bounds instead of this WAR once
650 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
651 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
652
653 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700654 GrAppliedHardClip appliedClip;
655 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
656 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500657 return;
658 }
659
Robert Phillips65048132017-08-10 08:44:49 -0400660 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500661
Robert Phillips7c525e62018-06-12 10:11:12 -0400662 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
663 viewMatrix,
Brian Salomon467921e2017-03-06 16:17:12 -0500664 useHWAA,
665 path->getFillType(),
666 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500667 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500668 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400669 if (!op) {
670 return;
671 }
Brian Salomon97180af2017-03-14 13:42:58 -0400672 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400673 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700674}
675
Chris Daltonbbfd5162017-11-07 13:35:22 -0700676void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400677 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500678 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400679 const SkMatrix& viewMatrix,
680 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700681 ASSERT_SINGLE_OWNER_PRIV
682 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400683 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400684 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
685 fRenderTargetContext->fContext);
686
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500687 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500688 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700689
690 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500691 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400692 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(fRenderTargetContext->fContext,
693 std::move(paint), viewMatrix,
694 rect, aaType, ss);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400695 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700696}
697
Chris Daltonbbfd5162017-11-07 13:35:22 -0700698bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400699 const GrUserStencilSettings* ss,
700 SkRegion::Op op,
701 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500702 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400703 const SkMatrix& viewMatrix,
704 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800705 ASSERT_SINGLE_OWNER_PRIV
706 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400707 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400708 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
709 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800710
Robert Phillips72152832017-01-25 17:31:35 -0500711 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800712
713 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800714 paint.setCoverageSetOpXPFactory(op, invert);
715
Brian Salomon82f44312017-01-11 13:42:54 -0500716 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800717 return true;
718 }
robertphillips391395d2016-03-02 09:26:36 -0800719 SkPath path;
720 path.setIsVolatile(true);
721 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500722 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800723}
724
Brian Osman11052242016-10-27 14:47:55 -0400725void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500726 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500727 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400728 const SkMatrix& viewMatrix,
729 const SkRect& rectToDraw,
730 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800731 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700732 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700733 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400734 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700735
csmartdalton97f6cd52016-07-13 13:37:08 -0700736 SkRect croppedRect = rectToDraw;
737 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700738 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
739 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700740 return;
741 }
742
Robert Phillips72152832017-01-25 17:31:35 -0500743 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700744
Brian Salomon7c8460e2017-05-12 11:36:10 -0400745 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500746 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400747 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400748 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400749 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700750 return;
joshualitt04194f32016-01-13 10:08:27 -0800751 }
bsalomonbb243832016-07-22 07:10:19 -0700752
Brian Salomonbaaf4392017-06-15 09:59:23 -0400753 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalRect(
Robert Phillips7c525e62018-06-12 10:11:12 -0400754 fContext, std::move(paint), viewMatrix, croppedRect, croppedLocalRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400755 if (op) {
756 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700757 return;
758 }
759
760 SkMatrix viewAndUnLocalMatrix;
761 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
762 SkDebugf("fillRectToRect called with empty local matrix.\n");
763 return;
764 }
765 viewAndUnLocalMatrix.postConcat(viewMatrix);
766
Brian Salomon2fad74a2017-12-20 13:28:55 -0500767 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
768 GrShape(localRect));
joshualittb6b513b2015-08-21 10:25:18 -0700769}
770
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400771void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
772 GrSamplerState::Filter filter, GrColor color,
Brian Salomon2213ee92018-10-02 10:44:21 -0400773 const SkRect& srcRect, const SkRect& dstRect,
774 GrQuadAAFlags aaFlags,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400775 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400776 const SkMatrix& viewMatrix,
Brian Osman3ebd3542018-07-30 14:36:53 -0400777 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
778 sk_sp<GrColorSpaceXform> paintColorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400779 ASSERT_SINGLE_OWNER
780 RETURN_IF_ABANDONED
781 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400782 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomonf1709042018-10-03 11:57:00 -0400783 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
784 srcRect.contains(proxy->getWorstCaseBoundsRect())) {
785 constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon34169692017-08-28 15:32:01 -0400786 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400787 GrAAType aaType =
788 this->chooseAAType(GrAA(aaFlags != GrQuadAAFlags::kNone), GrAllowMixedSamples::kNo);
Brian Salomonff9d6d32017-08-30 10:27:49 -0400789 SkRect clippedDstRect = dstRect;
790 SkRect clippedSrcRect = srcRect;
791 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
792 &clippedSrcRect)) {
793 return;
794 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400795 auto op = GrTextureOp::Make(fContext, std::move(proxy), filter, color, clippedSrcRect,
796 clippedDstRect, aaType, aaFlags, constraint, viewMatrix,
797 std::move(textureColorSpaceXform), std::move(paintColorSpaceXform));
798 this->addDrawOp(clip, std::move(op));
Brian Salomon34169692017-08-28 15:32:01 -0400799}
800
Brian Osman11052242016-10-27 14:47:55 -0400801void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500802 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500803 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400804 const SkMatrix& viewMatrix,
805 const SkRect& rectToDraw,
806 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800807 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700808 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700809 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400810 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700811
csmartdalton97f6cd52016-07-13 13:37:08 -0700812 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700813 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700814 return;
815 }
816
Robert Phillips72152832017-01-25 17:31:35 -0500817 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700818
Brian Salomon7c8460e2017-05-12 11:36:10 -0400819 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500820 if (GrAAType::kCoverage != aaType) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400821 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400822 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect, aaType);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400823 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700824 return;
bsalomonc55271f2015-11-09 11:55:57 -0800825 }
robertphillips4bc31812016-03-01 12:22:49 -0800826
Brian Salomonbaaf4392017-06-15 09:59:23 -0400827 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400828 fContext, std::move(paint), viewMatrix, localMatrix, croppedRect);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400829 if (op) {
830 this->addDrawOp(clip, std::move(op));
csmartdaltonfc49d562016-07-26 17:05:47 -0700831 return;
832 }
833
834 SkMatrix viewAndUnLocalMatrix;
835 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
836 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
837 return;
838 }
839 viewAndUnLocalMatrix.postConcat(viewMatrix);
840
841 SkPath path;
842 path.setIsVolatile(true);
843 path.addRect(rectToDraw);
844 path.transform(localMatrix);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500845 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewAndUnLocalMatrix,
846 GrShape(path));
robertphillipsea461502015-05-26 11:38:03 -0700847}
848
Brian Osman11052242016-10-27 14:47:55 -0400849void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500850 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400851 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400852 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400853 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400854 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400855 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500856 ASSERT_SINGLE_OWNER
857 RETURN_IF_ABANDONED
858 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400859 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500860
861 AutoCheckFlush acf(this->drawingManager());
862
863 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400864 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400865 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400866 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400867 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400868 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700869}
870
871///////////////////////////////////////////////////////////////////////////////
872
Brian Osman11052242016-10-27 14:47:55 -0400873void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500874 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400875 const SkMatrix& viewMatrix,
876 int spriteCount,
877 const SkRSXform xform[],
878 const SkRect texRect[],
879 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800880 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700881 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700882 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400883 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700884
Robert Phillips72152832017-01-25 17:31:35 -0500885 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700886
Brian Salomon0088f942017-07-12 11:51:27 -0400887 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -0400888 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
889 aaType, spriteCount, xform, texRect, colors);
Brian Salomon0088f942017-07-12 11:51:27 -0400890 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700891}
892
893///////////////////////////////////////////////////////////////////////////////
894
Brian Osman11052242016-10-27 14:47:55 -0400895void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500896 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500897 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400898 const SkMatrix& viewMatrix,
899 const SkRRect& rrect,
900 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800901 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700902 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700903 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400904 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400905
906 const SkStrokeRec& stroke = style.strokeRec();
907 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700908 return;
909 }
910
bsalomon7f0d9f32016-08-15 14:49:10 -0700911 GrNoClip noclip;
912 const GrClip* clip = &origClip;
913#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
914 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500915 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700916 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
917 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
918 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
919 SkRRect devRRect;
920 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
921 clip = &noclip;
922 }
923#endif
bsalomon6663acf2016-05-10 09:14:17 -0700924 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700925
Robert Phillips72152832017-01-25 17:31:35 -0500926 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700927
Brian Salomonea26d6b2018-01-23 20:33:21 +0000928 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500929 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400930 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -0400931 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeRRectOp(fContext,
932 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +0000933 viewMatrix,
934 rrect,
935 stroke,
936 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -0500937 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400938 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800939 return;
940 }
robertphillipsea461502015-05-26 11:38:03 -0700941 }
robertphillipsb56f9272016-02-25 11:03:52 -0800942
Brian Salomon2fad74a2017-12-20 13:28:55 -0500943 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
944 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700945}
946
Jim Van Verthc5903412016-11-17 15:27:09 -0500947///////////////////////////////////////////////////////////////////////////////
948
Jim Van Verth3af1af92017-05-18 15:06:54 -0400949static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
950 SkPoint3 result;
951 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
952 result.fZ = pt.fZ;
953 return result;
954}
955
956bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400957 const SkMatrix& viewMatrix,
958 const SkPath& path,
959 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500960 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400961 if (this->drawingManager()->wasAbandoned()) {
962 return true;
963 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500964 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400965 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400966
967 // check z plane
968 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
969 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
970 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
971 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
972 return false;
973 }
974
975 SkRRect rrect;
976 SkRect rect;
977 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500978 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400979 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
980 if (!isRRect &&
981 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
982 rect.width() > SK_ScalarNearlyZero) {
983 rrect.setOval(rect);
984 isRRect = true;
985 }
986 if (!isRRect && path.isRect(&rect)) {
987 rrect.setRect(rect);
988 isRRect = true;
989 }
990
991 if (!isRRect) {
992 return false;
993 }
994
Jim Van Verthc5903412016-11-17 15:27:09 -0500995 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400996 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -0500997 }
998
Robert Phillips72152832017-01-25 17:31:35 -0500999 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001000
Jim Van Verth3af1af92017-05-18 15:06:54 -04001001 // transform light
1002 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1003
1004 // 1/scale
1005 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1006 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1007 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1008 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1009
1010 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001011 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1012
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001013 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001014 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1015 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1016 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001017
1018 // Outset the shadow rrect to the border of the penumbra
1019 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1020 SkRRect ambientRRect;
1021 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1022 // If the rrect was an oval then its outset will also be one.
1023 // We set it explicitly to avoid errors.
1024 if (rrect.isOval()) {
1025 ambientRRect = SkRRect::MakeOval(outsetRect);
1026 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001027 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001028 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1029 }
1030
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001031 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001032 if (transparent) {
1033 // set a large inset to force a fill
1034 devSpaceInsetWidth = ambientRRect.width();
1035 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001036
Robert Phillips7c525e62018-06-12 10:11:12 -04001037 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1038 ambientColor,
1039 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001040 ambientRRect,
1041 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001042 devSpaceInsetWidth);
Brian Salomon05969092017-07-13 11:20:51 -04001043 SkASSERT(op);
1044 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001045 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001046
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001047 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001048 SkScalar devSpaceSpotBlur;
1049 SkScalar spotScale;
1050 SkVector spotOffset;
1051 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1052 devLightPos.fZ, rec.fLightRadius,
1053 &devSpaceSpotBlur, &spotScale, &spotOffset);
1054 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001055 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1056
Jim Van Verth3af1af92017-05-18 15:06:54 -04001057 // Adjust translate for the effect of the scale.
1058 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1059 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1060 // This offset is in dev space, need to transform it into source space.
1061 SkMatrix ctmInverse;
1062 if (viewMatrix.invert(&ctmInverse)) {
1063 ctmInverse.mapPoints(&spotOffset, 1);
1064 } else {
1065 // Since the matrix is a similarity, this should never happen, but just in case...
1066 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1067 SkASSERT(false);
1068 }
1069
1070 // Compute the transformed shadow rrect
1071 SkRRect spotShadowRRect;
1072 SkMatrix shadowTransform;
1073 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1074 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001075 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001076
1077 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001078 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001079 SkScalar insetWidth = blurOutset;
1080 if (transparent) {
1081 // If transparent, just do a fill
1082 insetWidth += spotShadowRRect.width();
1083 } else {
1084 // For shadows, instead of using a stroke we specify an inset from the penumbra
1085 // border. We want to extend this inset area so that it meets up with the caster
1086 // geometry. The inset geometry will by default already be inset by the blur width.
1087 //
1088 // We compare the min and max corners inset by the radius between the original
1089 // rrect and the shadow rrect. The distance between the two plus the difference
1090 // between the scaled radius and the original radius gives the distance from the
1091 // transformed shadow shape to the original shape in that corner. The max
1092 // of these gives the maximum distance we need to cover.
1093 //
1094 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1095 // that to get the full insetWidth.
1096 SkScalar maxOffset;
1097 if (rrect.isRect()) {
1098 // Manhattan distance works better for rects
1099 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1100 rrect.rect().fLeft),
1101 SkTAbs(spotShadowRRect.rect().fTop -
1102 rrect.rect().fTop)),
1103 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1104 rrect.rect().fRight),
1105 SkTAbs(spotShadowRRect.rect().fBottom -
1106 rrect.rect().fBottom)));
1107 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001108 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001109 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1110 rrect.rect().fLeft + dr,
1111 spotShadowRRect.rect().fTop -
1112 rrect.rect().fTop + dr);
1113 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1114 rrect.rect().fRight - dr,
1115 spotShadowRRect.rect().fBottom -
1116 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001117 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1118 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001119 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001120 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001121 }
1122
1123 // Outset the shadow rrect to the border of the penumbra
1124 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1125 if (spotShadowRRect.isOval()) {
1126 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1127 } else {
1128 SkScalar outsetRad = spotRadius + blurOutset;
1129 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1130 }
1131
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001132 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001133
Robert Phillips7c525e62018-06-12 10:11:12 -04001134 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1135 spotColor,
1136 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001137 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001138 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001139 insetWidth);
1140 SkASSERT(op);
1141 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001142 }
1143
1144 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001145}
1146
1147///////////////////////////////////////////////////////////////////////////////
1148
Brian Osman11052242016-10-27 14:47:55 -04001149bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001150 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001151 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001152 const SkMatrix& viewMatrix,
1153 const SkRRect& origOuter,
1154 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001155 SkASSERT(!origInner.isEmpty());
1156 SkASSERT(!origOuter.isEmpty());
1157
Brian Salomon65749212017-12-01 16:01:47 -05001158 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1159
Brian Salomon45839f92017-12-04 09:02:35 -05001160 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1161
1162 if (GrAAType::kMSAA == aaType) {
1163 return false;
1164 }
1165
Mike Reed242135a2018-02-22 13:41:39 -05001166 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1167 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001168 auto outerR = outer->width() / 2.f;
1169 auto innerR = inner->width() / 2.f;
1170 auto cx = outer->getBounds().fLeft + outerR;
1171 auto cy = outer->getBounds().fTop + outerR;
1172 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1173 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1174 auto avgR = (innerR + outerR) / 2.f;
1175 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1176 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1177 stroke.setStrokeStyle(outerR - innerR);
Robert Phillips7c525e62018-06-12 10:11:12 -04001178 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
1179 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001180 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001181 if (op) {
1182 this->addDrawOp(clip, std::move(op));
1183 return true;
1184 }
1185 }
1186 }
1187
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001188 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001189 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001190 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1191 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001192 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001193 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1194 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001195 }
robertphillips00095892016-02-29 13:50:40 -08001196
robertphillips00095892016-02-29 13:50:40 -08001197 SkMatrix inverseVM;
1198 if (!viewMatrix.isIdentity()) {
1199 if (!origInner.transform(viewMatrix, inner.writable())) {
1200 return false;
1201 }
1202 if (!origOuter.transform(viewMatrix, outer.writable())) {
1203 return false;
1204 }
1205 if (!viewMatrix.invert(&inverseVM)) {
1206 return false;
1207 }
1208 } else {
1209 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001210 }
robertphillips00095892016-02-29 13:50:40 -08001211
Brian Salomon14471772017-12-05 10:35:15 -05001212 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001213 // TODO these need to be a geometry processors
Brian Salomon14471772017-12-05 10:35:15 -05001214 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001215 if (!innerEffect) {
1216 return false;
1217 }
1218
Brian Salomon14471772017-12-05 10:35:15 -05001219 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001220 if (!outerEffect) {
1221 return false;
1222 }
1223
Brian Salomon82f44312017-01-11 13:42:54 -05001224 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1225 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001226
1227 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001228 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001229 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1230 }
halcanary9d524f22016-03-29 09:03:52 -07001231
Brian Salomon82f44312017-01-11 13:42:54 -05001232 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1233 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001234 return true;
1235}
1236
Brian Osman11052242016-10-27 14:47:55 -04001237void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001238 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001239 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001240 const SkMatrix& viewMatrix,
1241 const SkRRect& outer,
1242 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001243 ASSERT_SINGLE_OWNER
1244 RETURN_IF_ABANDONED
1245 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001246 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001247
1248 SkASSERT(!outer.isEmpty());
1249 SkASSERT(!inner.isEmpty());
1250
Robert Phillips72152832017-01-25 17:31:35 -05001251 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001252
Brian Salomon82f44312017-01-11 13:42:54 -05001253 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001254 return;
1255 }
1256
1257 SkPath path;
1258 path.setIsVolatile(true);
1259 path.addRRect(inner);
1260 path.addRRect(outer);
1261 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001262 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001263}
1264
robertphillipsea461502015-05-26 11:38:03 -07001265///////////////////////////////////////////////////////////////////////////////
1266
Brian Osman11052242016-10-27 14:47:55 -04001267void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001268 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001269 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001270 const SkMatrix& viewMatrix,
1271 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001272 const GrStyle& style,
1273 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001274 ASSERT_SINGLE_OWNER
1275 RETURN_IF_ABANDONED
1276 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001277 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001278
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001279 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001280 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001281 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001282 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001283 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1284 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001285 aa = GrAA::kNo;
1286 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001287 }
msarettcc319b92016-08-25 18:07:18 -07001288 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001289 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001290 SkPath path;
1291 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001292 path.setIsVolatile(true);
1293
Brian Salomon82f44312017-01-11 13:42:54 -05001294 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001295 }
1296
Brian Salomonf0366322017-07-11 15:53:05 -04001297 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001298 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1299 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001300 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001301}
1302
Brian Osman11052242016-10-27 14:47:55 -04001303void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001304 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001305 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001306 const SkMatrix& viewMatrix,
1307 const SkRect& oval,
1308 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001309 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001310 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001311 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001312 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001313
Robert Phillips7484d202018-07-03 09:09:08 -04001314 const SkStrokeRec& stroke = style.strokeRec();
1315
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001316 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001317 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1318 return;
1319 }
1320
1321 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001322 return;
robertphillipsea461502015-05-26 11:38:03 -07001323 }
1324
Robert Phillips72152832017-01-25 17:31:35 -05001325 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001326
Brian Salomonea26d6b2018-01-23 20:33:21 +00001327 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001328 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001329 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001330 if (auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval,
1331 style, shaderCaps)) {
Brian Salomon05441c42017-05-15 16:45:49 -04001332 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001333 return;
1334 }
robertphillipsea461502015-05-26 11:38:03 -07001335 }
robertphillipsb56f9272016-02-25 11:03:52 -08001336
Brian Salomon5209d7f2018-04-20 16:52:42 -04001337 this->drawShapeUsingPathRenderer(
1338 clip, std::move(paint), aa, viewMatrix,
1339 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001340}
1341
Brian Osman11052242016-10-27 14:47:55 -04001342void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001343 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001344 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001345 const SkMatrix& viewMatrix,
1346 const SkRect& oval,
1347 SkScalar startAngle,
1348 SkScalar sweepAngle,
1349 bool useCenter,
1350 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001351 ASSERT_SINGLE_OWNER
1352 RETURN_IF_ABANDONED
1353 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001354 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001355
1356 AutoCheckFlush acf(this->drawingManager());
1357
Brian Salomonea26d6b2018-01-23 20:33:21 +00001358 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001359 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001360 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001361 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1362 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +00001363 viewMatrix,
1364 oval,
1365 startAngle,
1366 sweepAngle,
1367 useCenter,
1368 style,
1369 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001370 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001371 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001372 return;
1373 }
1374 }
Brian Salomone4949402018-04-26 15:22:04 -04001375 this->drawShapeUsingPathRenderer(
1376 clip, std::move(paint), aa, viewMatrix,
1377 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001378}
1379
Brian Osman11052242016-10-27 14:47:55 -04001380void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001381 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001382 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001383 sk_sp<GrTextureProxy> image,
1384 sk_sp<GrColorSpaceXform> csxf,
1385 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001386 std::unique_ptr<SkLatticeIter> iter,
1387 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001388 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001389 RETURN_IF_ABANDONED
1390 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001391 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001392
Robert Phillips72152832017-01-25 17:31:35 -05001393 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001394
Brian Salomon2a943df2018-05-04 13:43:19 -04001395 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001396 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1397 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001398 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001399}
1400
Greg Daniel51316782017-08-02 15:10:09 +00001401GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1402 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001403 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001404 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001405 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001406 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001407
Greg Daniel51316782017-08-02 15:10:09 +00001408 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1409 numSemaphores,
1410 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001411}
1412
Greg Danielc64ee462017-06-15 16:59:49 -04001413bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001414 const GrBackendSemaphore* waitSemaphores) {
1415 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001416 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001417 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001418 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001419
1420 AutoCheckFlush acf(this->drawingManager());
1421
Greg Danielc64ee462017-06-15 16:59:49 -04001422 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1423 return false;
1424 }
1425
Robert Phillips6be756b2018-01-16 15:07:54 -05001426 auto resourceProvider = fContext->contextPriv().resourceProvider();
1427
Greg Daniela5cb7812017-06-16 09:45:32 -04001428 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1429 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001430 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001431 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1432 kAdopt_GrWrapOwnership);
Robert Phillips7c525e62018-06-12 10:11:12 -04001433 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, sema,
1434 fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001435 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001436 }
Greg Danielc64ee462017-06-15 16:59:49 -04001437 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001438}
joshualitt33a5fce2015-11-18 13:28:51 -08001439
Robert Phillips65a88fa2017-08-08 08:36:22 -04001440void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001441 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001442 this->getRTOpList()->addOp(std::move(op), *this->caps());
1443}
1444
Brian Osman11052242016-10-27 14:47:55 -04001445void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001446 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001447 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001448 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001449 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001450 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001451 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001452 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001453 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001454 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1455
Brian Salomon40b77a62017-12-22 11:25:52 -05001456 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001457
Robert Phillips27927a52018-08-20 13:18:12 -04001458 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001459}
1460
1461void GrRenderTargetContext::drawShape(const GrClip& clip,
1462 GrPaint&& paint,
1463 GrAA aa,
1464 const SkMatrix& viewMatrix,
1465 const GrShape& shape) {
1466 ASSERT_SINGLE_OWNER
1467 RETURN_IF_ABANDONED
1468 SkDEBUGCODE(this->validate();)
1469 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1470
Brian Salomon2fad74a2017-12-20 13:28:55 -05001471 if (shape.isEmpty()) {
1472 if (shape.inverseFilled()) {
1473 this->drawPaint(clip, std::move(paint), viewMatrix);
1474 }
1475 return;
robertphillipsea461502015-05-26 11:38:03 -07001476 }
1477
Robert Phillips72152832017-01-25 17:31:35 -05001478 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001479
Brian Salomon2fad74a2017-12-20 13:28:55 -05001480 if (!shape.style().hasPathEffect()) {
Robert Phillips73653b42018-08-22 12:42:42 -04001481 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001482 SkRRect rrect;
1483 // We can ignore the starting point and direction since there is no path effect.
1484 bool inverted;
1485 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1486 if (rrect.isRect()) {
1487 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1488 &shape.style());
1489 return;
1490 } else if (rrect.isOval()) {
1491 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001492 return;
1493 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001494 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1495 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001496 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1497 viewMatrix.rectStaysRect()) {
1498 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1499 // the matrix to all the points individually rather than just to the rect
1500 SkRect rects[2];
1501 if (shape.asNestedRects(rects)) {
1502 // Concave AA paths are expensive - try to avoid them for special cases
1503 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeAAFillNestedRects(
1504 fContext, std::move(paint), viewMatrix, rects);
1505 if (op) {
1506 this->addDrawOp(clip, std::move(op));
1507 }
1508 // Returning here indicates that there is nothing to draw in this case.
1509 return;
1510 }
robertphillipsea461502015-05-26 11:38:03 -07001511 }
1512 }
robertphillips4bc31812016-03-01 12:22:49 -08001513
Brian Salomon2fad74a2017-12-20 13:28:55 -05001514 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001515}
1516
Chris Daltonbbfd5162017-11-07 13:35:22 -07001517bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001518 const GrUserStencilSettings* ss,
1519 SkRegion::Op op,
1520 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001521 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001522 const SkMatrix& viewMatrix,
1523 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001524 ASSERT_SINGLE_OWNER_PRIV
1525 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001526 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001527 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1528 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001529
1530 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001531 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001532 SkRect::MakeIWH(fRenderTargetContext->width(),
1533 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001534 return true;
1535 }
1536
Robert Phillips72152832017-01-25 17:31:35 -05001537 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001538
1539 // An Assumption here is that path renderer would use some form of tweaking
1540 // the src color (either the input alpha or in the frag shader) to implement
1541 // aa. If we have some future driver-mojo path AA that can do the right
1542 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001543 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001544 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001545
Chris Daltondb91c6e2017-09-08 16:25:08 -06001546 SkIRect clipConservativeBounds;
1547 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1548 &clipConservativeBounds, nullptr);
1549
bsalomon8acedde2016-06-24 10:42:16 -07001550 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001551 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001552 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001553 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001554 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001555 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001556 canDrawArgs.fAAType = aaType;
cdalton93a379b2016-05-11 13:58:08 -07001557 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001558
1559 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001560 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001561 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001562 if (!pr) {
1563 return false;
1564 }
1565
1566 GrPaint paint;
1567 paint.setCoverageSetOpXPFactory(op, invert);
1568
Brian Salomonf3569f02017-10-24 12:52:33 -04001569 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1570 std::move(paint),
1571 ss,
1572 fRenderTargetContext,
1573 &clip,
1574 &clipConservativeBounds,
1575 &viewMatrix,
1576 &shape,
1577 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001578 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001579 pr->drawPath(args);
1580 return true;
1581}
1582
Brian Osman11052242016-10-27 14:47:55 -04001583SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001584 ASSERT_SINGLE_OWNER_PRIV
1585
Brian Osman11052242016-10-27 14:47:55 -04001586 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001587 return SkBudgeted::kNo;
1588 }
1589
Brian Osman11052242016-10-27 14:47:55 -04001590 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001591
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001592 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001593}
1594
Brian Salomon2fad74a2017-12-20 13:28:55 -05001595void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1596 GrPaint&& paint,
1597 GrAA aa,
1598 const SkMatrix& viewMatrix,
1599 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001600 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001601 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001602 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1603
Jim Van Verthf86073a2018-10-02 13:05:38 -04001604 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1605 return;
1606 }
1607
Chris Daltondb91c6e2017-09-08 16:25:08 -06001608 SkIRect clipConservativeBounds;
1609 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1610
Brian Salomon2fad74a2017-12-20 13:28:55 -05001611 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001612 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1613 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1614 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001615 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1616 ? GrAllowMixedSamples::kNo
1617 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001618 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001619 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001620 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001621 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001622 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001623 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001624 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001625
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001626 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001627 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001628 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001629 return;
1630 }
1631
1632 canDrawArgs.fAAType = aaType;
1633
1634 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1635 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1636 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1637
Brian Salomon2fad74a2017-12-20 13:28:55 -05001638 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001639 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001640 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1641 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001642 return;
1643 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001644 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001645 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001646 }
1647 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001648 if (canDrawArgs.fShape->style().applies()) {
1649 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1650 styleScale);
1651 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001652 return;
1653 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001654 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001655 // This time, allow SW renderer
1656 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1657 } else {
1658 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001659 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001660 }
robertphillipsea461502015-05-26 11:38:03 -07001661
bsalomon8acedde2016-06-24 10:42:16 -07001662 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001663#ifdef SK_DEBUG
1664 SkDebugf("Unable to find path renderer compatible with path.\n");
1665#endif
1666 return;
1667 }
1668
Robert Phillips256c37b2017-03-01 14:32:46 -05001669 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001670 std::move(paint),
1671 &GrUserStencilSettings::kUnused,
1672 this,
1673 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001674 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001675 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001676 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001677 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001678 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001679 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001680}
1681
Brian Salomon467921e2017-03-06 16:17:12 -05001682static void op_bounds(SkRect* bounds, const GrOp* op) {
1683 *bounds = op->bounds();
1684 if (op->hasZeroArea()) {
1685 if (op->hasAABloat()) {
1686 bounds->outset(0.5f, 0.5f);
1687 } else {
1688 // We don't know which way the particular GPU will snap lines or points at integer
1689 // coords. So we ensure that the bounds is large enough for either snap.
1690 SkRect before = *bounds;
1691 bounds->roundOut(bounds);
1692 if (bounds->fLeft == before.fLeft) {
1693 bounds->fLeft -= 1;
1694 }
1695 if (bounds->fTop == before.fTop) {
1696 bounds->fTop -= 1;
1697 }
1698 if (bounds->fRight == before.fRight) {
1699 bounds->fRight += 1;
1700 }
1701 if (bounds->fBottom == before.fBottom) {
1702 bounds->fBottom += 1;
1703 }
1704 }
1705 }
1706}
1707
Brian Salomon54d212e2017-03-21 14:22:38 -04001708uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
joshualitt1de610a2016-01-06 08:26:09 -08001709 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001710 if (this->drawingManager()->wasAbandoned()) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001711 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Robert Phillipsc0138922017-03-08 11:50:55 -05001712 return SK_InvalidUniqueID;
1713 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001714 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001715 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001716
Brian Salomon467921e2017-03-06 16:17:12 -05001717 // Setup clip
1718 SkRect bounds;
1719 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001720 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001721 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1722 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1723 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1724 &bounds)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001725 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001726 return SK_InvalidUniqueID;
1727 }
1728
Brian Salomon54d212e2017-03-21 14:22:38 -04001729 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1730 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001731 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001732
Robert Phillips65048132017-08-10 08:44:49 -04001733 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001734 }
1735
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001736 GrXferProcessor::DstProxy dstProxy;
Brian Osman532b3f92018-07-11 10:02:07 -04001737 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip)) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001738 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, op->bounds(), &dstProxy)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001739 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon54d212e2017-03-21 14:22:38 -04001740 return SK_InvalidUniqueID;
1741 }
1742 }
1743
1744 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001745 return this->getRTOpList()->addOp(std::move(op), *this->caps(),
1746 std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001747}
1748
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001749bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001750 const SkRect& opBounds,
1751 GrXferProcessor::DstProxy* dstProxy) {
Brian Salomon467921e2017-03-06 16:17:12 -05001752 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001753 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001754 // The render target is a texture, so we can read from it directly in the shader. The XP
1755 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001756 dstProxy->setProxy(sk_ref_sp(texProxy));
1757 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001758 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001759 }
1760 }
1761
Robert Phillipsbf25d432017-04-07 10:08:53 -04001762 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001763
Eric Karl74480882017-04-03 14:49:05 -07001764 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001765 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Eric Karl72e551e2017-04-04 13:42:10 -07001766 SkIRect drawIBounds;
Brian Salomon467921e2017-03-06 16:17:12 -05001767 opBounds.roundOut(&drawIBounds);
Brian Salomon859621f2017-03-16 09:21:54 -04001768 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1769 drawIBounds.outset(1, 1);
Eric Karl72e551e2017-04-04 13:42:10 -07001770 if (!clippedRect.intersect(drawIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001771#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001772 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001773#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001774 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001775 }
1776
1777 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1778 // have per-sample dst values by making the copy multisampled.
1779 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001780 bool rectsMustMatch = false;
1781 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001782 GrSurfaceOrigin origin;
1783 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
1784 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001785 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001786 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04001787 origin = rtProxy->origin();
Brian Salomon467921e2017-03-06 16:17:12 -05001788 }
1789
Eric Karl74480882017-04-03 14:49:05 -07001790 if (!disallowSubrect) {
1791 copyRect = clippedRect;
1792 }
Brian Salomon467921e2017-03-06 16:17:12 -05001793
Robert Phillipsbf25d432017-04-07 10:08:53 -04001794 SkIPoint dstPoint, dstOffset;
1795 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001796 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001797 desc.fWidth = rtProxy->width();
1798 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001799 dstPoint = {copyRect.fLeft, copyRect.fTop};
1800 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001801 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001802 } else {
1803 desc.fWidth = copyRect.width();
1804 desc.fHeight = copyRect.height();
1805 dstPoint = {0, 0};
1806 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001807 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001808 }
Brian Salomon467921e2017-03-06 16:17:12 -05001809
Robert Phillipsbf25d432017-04-07 10:08:53 -04001810 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -05001811 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05001812 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001813 if (!sContext) {
1814 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1815 return false;
1816 }
1817
1818 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1819 SkDebugf("setupDstTexture: copy failed.\n");
1820 return false;
1821 }
1822
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001823 dstProxy->setProxy(sContext->asTextureProxyRef());
1824 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001825 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001826}