blob: ec40e4849b627bd16145b9ddeb2f0cda3e541810 [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"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040029#include "SkDrawable.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040030#include "SkDrawShadowInfo.h"
Herb Derby8378dfb2018-08-30 14:50:04 -040031#include "SkGlyphRunPainter.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040032#include "SkGr.h"
Brian Salomon467921e2017-03-06 16:17:12 -050033#include "SkLatticeIter.h"
34#include "SkMatrixPriv.h"
Mike Reed242135a2018-02-22 13:41:39 -050035#include "SkRRectPriv.h"
Jim Van Verth34d6e4b2017-06-09 11:09:03 -040036#include "SkShadowUtils.h"
robertphillips2d70dcb2015-10-06 07:38:23 -070037#include "SkSurfacePriv.h"
Brian Salomon467921e2017-03-06 16:17:12 -050038#include "effects/GrRRectEffect.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040039#include "ops/GrAtlasTextOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050040#include "ops/GrClearOp.h"
Robert Phillipsb9a02a12017-04-06 11:08:40 -040041#include "ops/GrClearStencilClipOp.h"
Robert Phillips65a88fa2017-08-08 08:36:22 -040042#include "ops/GrDebugMarkerOp.h"
Greg Daniel64cc9aa2018-10-19 13:54:56 -040043#include "ops/GrDrawableOp.h"
Brian Salomon0f353322017-05-03 20:58:59 +000044#include "ops/GrDrawAtlasOp.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040045#include "ops/GrDrawOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050046#include "ops/GrDrawVerticesOp.h"
Michael Ludwig69858532018-11-28 15:34:34 -050047#include "ops/GrFillRectOp.h"
Chris Dalton133944a2018-11-16 23:30:29 -050048#include "ops/GrAAFillRRectOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050049#include "ops/GrLatticeOp.h"
50#include "ops/GrOp.h"
51#include "ops/GrOvalOpFactory.h"
Brian Salomon89527432016-12-16 09:52:16 -050052#include "ops/GrRegionOp.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040053#include "ops/GrSemaphoreOp.h"
Brian Salomon89527432016-12-16 09:52:16 -050054#include "ops/GrShadowRRectOp.h"
Brian Salomon467921e2017-03-06 16:17:12 -050055#include "ops/GrStencilPathOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050056#include "ops/GrStrokeRectOp.h"
Brian Salomon34169692017-08-28 15:32:01 -040057#include "ops/GrTextureOp.h"
Herb Derby26cbe512018-05-24 14:39:01 -040058#include "text/GrTextContext.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040059#include "text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040060
Herb Derbyc1b482c2018-08-09 15:02:27 -040061class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040062public:
63 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040064 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomonf18b1d82017-10-27 11:30:49 -040065 renderTargetContext->colorSpaceInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040066 , fRenderTargetContext(renderTargetContext)
Herb Derby65956872018-08-21 16:55:04 -040067 , fGlyphPainter{*renderTargetContext}{}
Brian Salomonf18b1d82017-10-27 11:30:49 -040068
Robert Phillips7c525e62018-06-12 10:11:12 -040069 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040070 fRenderTargetContext->addDrawOp(clip, std::move(op));
71 }
72
Robert Phillips46a13382018-08-23 13:53:01 -040073 void drawShape(const GrClip& clip, const SkPaint& paint,
74 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040075 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
76 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040077 }
78
79 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040080 GrPaint* grPaint) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040081 GrContext* context = fRenderTargetContext->fContext;
82 const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo();
83 if (kARGB_GrMaskFormat == maskFormat) {
84 SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint);
85 } else {
86 SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint);
87 }
88 }
89
Robert Phillips7c525e62018-06-12 10:11:12 -040090 GrContext* getContext() override {
91 return fRenderTargetContext->fContext;
92 }
93
Herb Derby65956872018-08-21 16:55:04 -040094 SkGlyphRunListPainter* glyphPainter() override {
95 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -040096 }
97
Brian Salomonf18b1d82017-10-27 11:30:49 -040098private:
99 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400100 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400101
Brian Salomonf18b1d82017-10-27 11:30:49 -0400102};
joshualittbc907352016-01-13 06:45:40 -0800103
Robert Phillips72152832017-01-25 17:31:35 -0500104#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800105#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400106 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800107#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400108 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips72152832017-01-25 17:31:35 -0500109#define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
110#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
111#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
112#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
113#define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700114
Brian Salomone225b562017-06-14 13:00:03 -0400115//////////////////////////////////////////////////////////////////////////////
116
117GrAAType GrChooseAAType(GrAA aa, GrFSAAType fsaaType, GrAllowMixedSamples allowMixedSamples,
118 const GrCaps& caps) {
119 if (GrAA::kNo == aa) {
120 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
121 // that.
122 if (fsaaType == GrFSAAType::kUnifiedMSAA && !caps.multisampleDisableSupport()) {
123 return GrAAType::kMSAA;
124 }
125 return GrAAType::kNone;
126 }
127 switch (fsaaType) {
128 case GrFSAAType::kNone:
129 return GrAAType::kCoverage;
130 case GrFSAAType::kUnifiedMSAA:
131 return GrAAType::kMSAA;
132 case GrFSAAType::kMixedSamples:
133 return GrAllowMixedSamples::kYes == allowMixedSamples ? GrAAType::kMixedSamples
134 : GrAAType::kCoverage;
135 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400136 SK_ABORT("Unexpected fsaa type");
Brian Salomone225b562017-06-14 13:00:03 -0400137 return GrAAType::kNone;
138}
139
140//////////////////////////////////////////////////////////////////////////////
141
robertphillipsea461502015-05-26 11:38:03 -0700142class AutoCheckFlush {
143public:
halcanary9d524f22016-03-29 09:03:52 -0700144 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700145 SkASSERT(fDrawingManager);
146 }
bsalomonb77a9072016-09-07 10:02:04 -0700147 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700148
149private:
robertphillips77a2e522015-10-17 07:43:27 -0700150 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700151};
152
Brian Osman11052242016-10-27 14:47:55 -0400153bool GrRenderTargetContext::wasAbandoned() const {
Robert Phillips72152832017-01-25 17:31:35 -0500154 return this->drawingManager()->wasAbandoned();
robertphillips7761d612016-05-16 09:14:53 -0700155}
156
Robert Phillipsf2361d22016-10-25 14:20:06 -0400157// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
Brian Osman11052242016-10-27 14:47:55 -0400158// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
Robert Phillipsf2361d22016-10-25 14:20:06 -0400159// stack. When this occurs with a closed GrOpList, a new one will be allocated
Brian Osman11052242016-10-27 14:47:55 -0400160// when the renderTargetContext attempts to use it (via getOpList).
161GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
162 GrDrawingManager* drawingMgr,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400163 sk_sp<GrRenderTargetProxy> rtp,
Brian Osman11052242016-10-27 14:47:55 -0400164 sk_sp<SkColorSpace> colorSpace,
165 const SkSurfaceProps* surfaceProps,
166 GrAuditTrail* auditTrail,
Robert Phillips941d1442017-06-14 16:37:02 -0400167 GrSingleOwner* singleOwner,
168 bool managedOpList)
Brian Salomonf3569f02017-10-24 12:52:33 -0400169 : GrSurfaceContext(context, drawingMgr, rtp->config(), std::move(colorSpace), auditTrail,
170 singleOwner)
171 , fRenderTargetProxy(std::move(rtp))
172 , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400173 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
174 , fManagedOpList(managedOpList) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500175 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
176 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
177 // MDB TODO: to ensure all resources still get allocated in the correct order in the hybrid
178 // world we need to get the correct opList here so that it, in turn, can grab and hold
179 // its rendertarget.
180 this->getRTOpList();
181 }
182
Brian Salomonf18b1d82017-10-27 11:30:49 -0400183 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700184 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700185}
186
robertphillips2e1e51f2015-10-15 08:01:48 -0700187#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400188void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400189 SkASSERT(fRenderTargetProxy);
190 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700191
Robert Phillipsf2361d22016-10-25 14:20:06 -0400192 if (fOpList && !fOpList->isClosed()) {
Robert Phillipsdc83b892017-04-13 12:23:54 -0400193 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get());
robertphillipsa106c622015-10-16 09:07:06 -0700194 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700195}
196#endif
197
Brian Osman11052242016-10-27 14:47:55 -0400198GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800199 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700200}
201
Robert Phillipsf200a902017-01-30 13:27:37 -0500202GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000203 return fRenderTargetProxy->asTextureProxy();
204}
205
Greg Daniele252f082017-10-23 16:05:23 -0400206const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
207 return fRenderTargetProxy->asTextureProxy();
208}
209
Robert Phillipsf200a902017-01-30 13:27:37 -0500210sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
211 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
212}
213
Greg Daniele252f082017-10-23 16:05:23 -0400214GrMipMapped GrRenderTargetContext::mipMapped() const {
215 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
216 return proxy->mipMapped();
217 }
218 return GrMipMapped::kNo;
219}
220
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400221GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() {
joshualitt1de610a2016-01-06 08:26:09 -0800222 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700223 SkDEBUGCODE(this->validate();)
224
Robert Phillipsf2361d22016-10-25 14:20:06 -0400225 if (!fOpList || fOpList->isClosed()) {
Robert Phillips941d1442017-06-14 16:37:02 -0400226 fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy.get(), fManagedOpList);
robertphillipsa106c622015-10-16 09:07:06 -0700227 }
228
Robert Phillipsdc83b892017-04-13 12:23:54 -0400229 return fOpList.get();
robertphillipsa106c622015-10-16 09:07:06 -0700230}
231
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400232GrOpList* GrRenderTargetContext::getOpList() {
233 return this->getRTOpList();
robertphillipsea461502015-05-26 11:38:03 -0700234}
235
Herb Derbycddab252018-07-16 11:19:04 -0400236void GrRenderTargetContext::drawGlyphRunList(
237 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400238 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700240 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700241 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400242 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700243
Greg Danielbe7fc462019-01-03 16:40:42 -0500244 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
245 // secondary command buffers because it would require stopping and starting a render pass which
246 // we don't have access to.
247 if (this->wrapsVkSecondaryCB()) {
248 return;
249 }
250
Herb Derby26cbe512018-05-24 14:39:01 -0400251 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400252 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400253 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700254}
255
Brian Osman11052242016-10-27 14:47:55 -0400256void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800257 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700258 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700259 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400260 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700261
Robert Phillips72152832017-01-25 17:31:35 -0500262 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400263
Robert Phillips380b90c2017-08-30 07:41:07 -0400264 this->getRTOpList()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700265}
266
Brian Osman11052242016-10-27 14:47:55 -0400267void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500268 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700269 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800270 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700271 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700272 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400273 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700274
Robert Phillips72152832017-01-25 17:31:35 -0500275 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700276 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
277 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700278}
robertphillips9199a9f2016-07-13 07:48:43 -0700279
Brian Osman9a9baae2018-11-05 15:06:26 -0500280void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const SkPMColor4f& color) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500281 ASSERT_SINGLE_OWNER_PRIV
282 RETURN_IF_ABANDONED_PRIV
283 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400284 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear",
285 fRenderTargetContext->fContext);
Robert Phillips784b7bf2016-12-09 13:35:02 -0500286
Robert Phillips72152832017-01-25 17:31:35 -0500287 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500288
Brian Salomonbb5711a2017-05-17 13:49:59 -0400289 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(),
290 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500291
292 if (clearRect) {
293 if (clearRect->contains(rtRect)) {
294 clearRect = nullptr; // full screen
295 } else {
296 if (!rtRect.intersect(*clearRect)) {
297 return;
298 }
299 }
300 }
301
302 // TODO: in a post-MDB world this should be handled at the OpList level.
303 // An op-list that is initially cleared and has no other ops should receive an
304 // extra draw.
Brian Salomon43f8bf02017-10-18 08:33:29 -0400305 // This path doesn't handle coalescing of full screen clears b.c. it
306 // has to clear the entire render target - not just the content area.
307 // It could be done but will take more finagling.
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400308 if (clearRect && fRenderTargetContext->caps()->performPartialClearsAsDraws()) {
309 GrPaint paint;
Brian Osman9a9baae2018-11-05 15:06:26 -0500310 paint.setColor4f(color);
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400311 SkRect scissor = SkRect::Make(rtRect);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500312 std::unique_ptr<GrDrawOp> op(GrFillRectOp::Make(fRenderTargetContext->fContext,
313 std::move(paint), GrAAType::kNone,
314 SkMatrix::I(), scissor));
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400315 if (!op) {
316 return;
317 }
318 fRenderTargetContext->addDrawOp(GrFixedClip(), std::move(op));
Robert Phillips784b7bf2016-12-09 13:35:02 -0500319 }
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400320 else {
321 std::unique_ptr<GrOp> op(GrClearOp::Make(fRenderTargetContext->fContext, rtRect,
322 color, !clearRect));
323 if (!op) {
324 return;
325 }
326 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
327 }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500328}
329
Brian Osman11052242016-10-27 14:47:55 -0400330void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
Brian Osman9a9baae2018-11-05 15:06:26 -0500331 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700332 CanClearFullscreen canClearFullscreen) {
csmartdalton29df7602016-08-31 11:55:52 -0700333 ASSERT_SINGLE_OWNER_PRIV
334 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400335 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400336 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
337 fRenderTargetContext->fContext);
csmartdalton29df7602016-08-31 11:55:52 -0700338
Robert Phillips72152832017-01-25 17:31:35 -0500339 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700340 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700341}
342
Brian Osman11052242016-10-27 14:47:55 -0400343void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
Brian Osman9a9baae2018-11-05 15:06:26 -0500344 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700345 CanClearFullscreen canClearFullscreen) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700346 bool isFull = false;
347 if (!clip.hasWindowRectangles()) {
Brian Salomond818ebf2018-07-02 14:08:49 +0000348 isFull = !clip.scissorEnabled() ||
Chris Dalton344e9032017-12-11 15:42:09 -0700349 (CanClearFullscreen::kYes == canClearFullscreen &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400350 this->caps()->preferFullscreenClears()) ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700351 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
352 }
robertphillips9199a9f2016-07-13 07:48:43 -0700353
Brian Salomon43f8bf02017-10-18 08:33:29 -0400354 if (isFull) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400355 this->getRTOpList()->fullClear(fContext, color);
robertphillips9199a9f2016-07-13 07:48:43 -0700356 } else {
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400357 if (this->caps()->performPartialClearsAsDraws()) {
358 GrPaint paint;
Brian Osman9a9baae2018-11-05 15:06:26 -0500359 paint.setColor4f(color);
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400360 SkRect scissor = SkRect::Make(clip.scissorRect());
Michael Ludwig72ab3462018-12-10 12:43:36 -0500361 std::unique_ptr<GrDrawOp> op(GrFillRectOp::Make(fContext, std::move(paint),
362 GrAAType::kNone, SkMatrix::I(),
363 scissor));
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400364 if (!op) {
365 return;
366 }
367 this->addDrawOp(clip, std::move(op));
csmartdalton29df7602016-08-31 11:55:52 -0700368 }
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400369 else {
370 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
371 this->asSurfaceProxy()));
372 if (!op) {
373 return;
374 }
375 this->getRTOpList()->addOp(std::move(op), *this->caps());
376 }
robertphillips9199a9f2016-07-13 07:48:43 -0700377 }
robertphillipsea461502015-05-26 11:38:03 -0700378}
379
Brian Osman11052242016-10-27 14:47:55 -0400380void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500381 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400382 const SkMatrix& viewMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800383 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700384 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700385 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400386 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPaint", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700387
robertphillipsea461502015-05-26 11:38:03 -0700388 // set rect to be big enough to fill the space, but not super-huge, so we
389 // don't overflow fixed-point implementations
robertphillips13a7eee2016-08-31 15:06:24 -0700390
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400391 SkRect r = fRenderTargetProxy->getBoundsRect();
robertphillipsea461502015-05-26 11:38:03 -0700392
bsalomoncb31e512016-08-26 10:48:19 -0700393 SkRRect rrect;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400394 GrAA aa = GrAA::kNo;
395
396 // rrect is calculated in one of two places, don't do it twice
397 enum RRectState {
398 kUnknown, kValid, kNotValid
399 };
400 RRectState rrectState = kUnknown;
401
402 // Check if the paint is a constant color, which is the first criterion to being able to turn
403 // the drawPaint() into a clear(). More expensive geometry checks can happen after that.
Brian Osman9a9baae2018-11-05 15:06:26 -0500404 SkPMColor4f clearColor;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400405 if (paint.isConstantBlendedColor(&clearColor)) {
406 // Regardless of the actual clip geometry, if it completely covers the device bounds it can
407 // be turned into a fullscreen clear.
408 if (clip.quickContains(r)) {
409 // Fill the device with the constant color
410 this->clear(nullptr, clearColor, CanClearFullscreen::kYes);
411 return;
412 }
Michael Ludwig43a54972018-10-09 18:02:19 -0400413 // If the clip intersection with the device is a non-rounded rectangle, it could be a
414 // implemented faster as a clear limited by the scissor test. Not all rectangular clips can
415 // be converted into a simple clear (see GrReducedClip), but for non-AA and "AA rects that
416 // line up with pixel boundaries", we can map it to a clear using the rounded rect.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400417 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
Michael Ludwig43a54972018-10-09 18:02:19 -0400418 if (rrectState == kValid && rrect.isRect() && (aa == GrAA::kNo ||
419 GrClip::IsPixelAligned(rrect.getBounds()))) {
420 SkIRect scissorRect;
421 rrect.getBounds().round(&scissorRect);
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400422 this->clear(&scissorRect, clearColor, CanClearFullscreen::kNo);
423 return;
424 }
425 }
426
bsalomoncb31e512016-08-26 10:48:19 -0700427 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
428 // transformation for non-rect rrects. Rects caused a performance regression on an Android
429 // test that needs investigation. We also skip cases where there are fragment processors
430 // because they may depend on having correct local coords and this path draws in device space
431 // without a local matrix.
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400432 if (!paint.numTotalFragmentProcessors()) {
433 if (rrectState == kUnknown) {
434 rrectState = clip.isRRect(r, &rrect, &aa) ? kValid : kNotValid;
435 }
436 if (rrectState == kValid && !rrect.isRect()) {
437 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
438 GrStyle::SimpleFill());
439 return;
440 }
bsalomoncb31e512016-08-26 10:48:19 -0700441 }
442
robertphillipsea461502015-05-26 11:38:03 -0700443 bool isPerspective = viewMatrix.hasPerspective();
444
445 // We attempt to map r by the inverse matrix and draw that. mapRect will
446 // map the four corners and bound them with a new rect. This will not
447 // produce a correct result for some perspective matrices.
448 if (!isPerspective) {
reeda39667c2016-08-22 06:39:49 -0700449 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
robertphillipsea461502015-05-26 11:38:03 -0700450 return;
451 }
Brian Salomon82f44312017-01-11 13:42:54 -0500452 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
robertphillipsea461502015-05-26 11:38:03 -0700453 } else {
454 SkMatrix localMatrix;
455 if (!viewMatrix.invert(&localMatrix)) {
robertphillipsea461502015-05-26 11:38:03 -0700456 return;
457 }
458
Robert Phillips72152832017-01-25 17:31:35 -0500459 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700460
Michael Ludwig72ab3462018-12-10 12:43:36 -0500461 std::unique_ptr<GrDrawOp> op = GrFillRectOp::MakeWithLocalMatrix(
462 fContext, std::move(paint), GrAAType::kNone, SkMatrix::I(), localMatrix, r);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400463 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700464 }
465}
466
robertphillipsea461502015-05-26 11:38:03 -0700467static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
468 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
469 point.fY >= rect.fTop && point.fY <= rect.fBottom;
470}
471
csmartdalton97f6cd52016-07-13 13:37:08 -0700472// Attempts to crop a rect and optional local rect to the clip boundaries.
473// Returns false if the draw can be skipped entirely.
robertphillips13a7eee2016-08-31 15:06:24 -0700474static bool crop_filled_rect(int width, int height, const GrClip& clip,
csmartdalton97f6cd52016-07-13 13:37:08 -0700475 const SkMatrix& viewMatrix, SkRect* rect,
476 SkRect* localRect = nullptr) {
477 if (!viewMatrix.rectStaysRect()) {
478 return true;
479 }
480
csmartdalton97f6cd52016-07-13 13:37:08 -0700481 SkIRect clipDevBounds;
482 SkRect clipBounds;
csmartdalton97f6cd52016-07-13 13:37:08 -0700483
robertphillips13a7eee2016-08-31 15:06:24 -0700484 clip.getConservativeBounds(width, height, &clipDevBounds);
reeda39667c2016-08-22 06:39:49 -0700485 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
486 return false;
487 }
csmartdalton97f6cd52016-07-13 13:37:08 -0700488
489 if (localRect) {
490 if (!rect->intersects(clipBounds)) {
491 return false;
492 }
493 const SkScalar dx = localRect->width() / rect->width();
494 const SkScalar dy = localRect->height() / rect->height();
495 if (clipBounds.fLeft > rect->fLeft) {
496 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
497 rect->fLeft = clipBounds.fLeft;
498 }
499 if (clipBounds.fTop > rect->fTop) {
500 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
501 rect->fTop = clipBounds.fTop;
502 }
503 if (clipBounds.fRight < rect->fRight) {
504 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
505 rect->fRight = clipBounds.fRight;
506 }
507 if (clipBounds.fBottom < rect->fBottom) {
508 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
509 rect->fBottom = clipBounds.fBottom;
510 }
511 return true;
512 }
513
514 return rect->intersect(clipBounds);
515}
516
Brian Osman11052242016-10-27 14:47:55 -0400517bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500518 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500519 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400520 const SkMatrix& viewMatrix,
521 const SkRect& rect,
522 const GrUserStencilSettings* ss) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700523 SkRect croppedRect = rect;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500524 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700525 return true;
526 }
robertphillips44302392016-07-08 14:43:03 -0700527
Brian Salomon7c8460e2017-05-12 11:36:10 -0400528 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500529 this->addDrawOp(clip, GrFillRectOp::Make(fContext, std::move(paint), aaType, viewMatrix,
530 croppedRect, ss));
Brian Salomonbaaf4392017-06-15 09:59:23 -0400531 return true;
robertphillips391395d2016-03-02 09:26:36 -0800532}
533
Brian Osman11052242016-10-27 14:47:55 -0400534void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500535 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500536 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400537 const SkMatrix& viewMatrix,
538 const SkRect& rect,
539 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700540 if (!style) {
541 style = &GrStyle::SimpleFill();
542 }
joshualitt1de610a2016-01-06 08:26:09 -0800543 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700544 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700545 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400546 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700547
bsalomon6663acf2016-05-10 09:14:17 -0700548 // Path effects should've been devolved to a path in SkGpuDevice
549 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700550
Robert Phillips72152832017-01-25 17:31:35 -0500551 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700552
bsalomon6663acf2016-05-10 09:14:17 -0700553 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400554 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400555 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
556 // checking cases where the RT is fully inside a stroke.
557 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
558 // Does the clip contain the entire RT?
Robert Phillips8c8b0462018-08-24 16:18:03 -0400559 if (clip.quickContains(rtRect) && !paint.numCoverageFragmentProcessors()) {
Brian Salomon43f8bf02017-10-18 08:33:29 -0400560 SkMatrix invM;
561 if (!viewMatrix.invert(&invM)) {
562 return;
563 }
564 // Does the rect bound the RT?
Brian Salomona33b67c2018-05-17 10:42:14 -0400565 GrQuad quad(rtRect, invM);
Brian Salomon43f8bf02017-10-18 08:33:29 -0400566 if (rect_contains_inclusive(rect, quad.point(0)) &&
567 rect_contains_inclusive(rect, quad.point(1)) &&
568 rect_contains_inclusive(rect, quad.point(2)) &&
569 rect_contains_inclusive(rect, quad.point(3))) {
570 // Will it blend?
Brian Osman9a9baae2018-11-05 15:06:26 -0500571 SkPMColor4f clearColor;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400572 if (paint.isConstantBlendedColor(&clearColor)) {
Chris Dalton344e9032017-12-11 15:42:09 -0700573 this->clear(nullptr, clearColor,
574 GrRenderTargetContext::CanClearFullscreen::kYes);
robertphillipsea461502015-05-26 11:38:03 -0700575 return;
576 }
577 }
578 }
robertphillips44302392016-07-08 14:43:03 -0700579
Brian Salomon82f44312017-01-11 13:42:54 -0500580 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
robertphillips44302392016-07-08 14:43:03 -0700581 return;
582 }
bsalomona7d85ba2016-07-06 11:54:59 -0700583 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
584 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
585 if ((!rect.width() || !rect.height()) &&
586 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
587 SkScalar r = stroke.getWidth() / 2;
588 // TODO: Move these stroke->fill fallbacks to GrShape?
589 switch (stroke.getJoin()) {
590 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500591 this->drawRect(
592 clip, std::move(paint), aa, viewMatrix,
593 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
594 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700595 return;
596 case SkPaint::kRound_Join:
597 // Raster draws nothing when both dimensions are empty.
598 if (rect.width() || rect.height()){
599 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500600 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
601 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700602 return;
603 }
604 case SkPaint::kBevel_Join:
605 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500606 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700607 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
608 &GrStyle::SimpleFill());
609 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500610 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700611 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
612 &GrStyle::SimpleFill());
613 }
614 return;
615 }
616 }
robertphillips44302392016-07-08 14:43:03 -0700617
Brian Salomonbaaf4392017-06-15 09:59:23 -0400618 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700619
Brian Salomon7c8460e2017-05-12 11:36:10 -0400620 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500621 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
622 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
623 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500624 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400625 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700626 return;
robertphillips4bc31812016-03-01 12:22:49 -0800627 }
robertphillips4bc31812016-03-01 12:22:49 -0800628 }
Mike Klein16885072018-12-11 09:54:31 -0500629 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500630 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700631}
632
Michael Ludwig69858532018-11-28 15:34:34 -0500633void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
634 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
635 int cnt) {
636 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
637 this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix,
638 quads, cnt));
639}
640
Robert Phillipsec2249f2016-11-09 08:54:35 -0500641int GrRenderTargetContextPriv::maxWindowRectangles() const {
642 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400643 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500644}
645
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000646void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700647 ASSERT_SINGLE_OWNER_PRIV
648 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400649 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400650 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
651 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700652
Robert Phillips72152832017-01-25 17:31:35 -0500653 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400654
Robert Phillips7c525e62018-06-12 10:11:12 -0400655 GrRenderTargetProxy* rtProxy = fRenderTargetContext->fRenderTargetProxy.get();
656 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fRenderTargetContext->fContext,
657 clip, insideStencilMask, rtProxy));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400658 if (!op) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500659 return;
660 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400661 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700662}
663
Chris Daltonbbfd5162017-11-07 13:35:22 -0700664void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500665 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400666 const SkMatrix& viewMatrix,
667 const GrPath* path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500668 ASSERT_SINGLE_OWNER_PRIV
669 RETURN_IF_ABANDONED_PRIV
670 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400671 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
672 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500673
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500674 SkASSERT(aaType != GrAAType::kCoverage);
Brian Salomon467921e2017-03-06 16:17:12 -0500675
676 bool useHWAA = GrAATypeIsHW(aaType);
677 // TODO: extract portions of checkDraw that are relevant to path stenciling.
678 SkASSERT(path);
679 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
680
681 // FIXME: Use path bounds instead of this WAR once
682 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
683 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
684
685 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700686 GrAppliedHardClip appliedClip;
687 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
688 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500689 return;
690 }
691
Robert Phillips65048132017-08-10 08:44:49 -0400692 fRenderTargetContext->setNeedsStencil();
Brian Salomon467921e2017-03-06 16:17:12 -0500693
Robert Phillips7c525e62018-06-12 10:11:12 -0400694 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
695 viewMatrix,
Brian Salomon467921e2017-03-06 16:17:12 -0500696 useHWAA,
697 path->getFillType(),
698 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500699 appliedClip.scissorState(),
Brian Salomon467921e2017-03-06 16:17:12 -0500700 path);
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400701 if (!op) {
702 return;
703 }
Brian Salomon97180af2017-03-14 13:42:58 -0400704 op->setClippedBounds(bounds);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400705 fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps());
robertphillips976f5f02016-06-03 10:59:20 -0700706}
707
Chris Daltonbbfd5162017-11-07 13:35:22 -0700708void GrRenderTargetContextPriv::stencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400709 const GrUserStencilSettings* ss,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500710 GrAAType aaType,
Brian Osman11052242016-10-27 14:47:55 -0400711 const SkMatrix& viewMatrix,
712 const SkRect& rect) {
robertphillips976f5f02016-06-03 10:59:20 -0700713 ASSERT_SINGLE_OWNER_PRIV
714 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400715 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400716 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilRect",
717 fRenderTargetContext->fContext);
718
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500719 SkASSERT(GrAAType::kCoverage != aaType);
Robert Phillips72152832017-01-25 17:31:35 -0500720 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips976f5f02016-06-03 10:59:20 -0700721
722 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500723 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwig72ab3462018-12-10 12:43:36 -0500724 std::unique_ptr<GrDrawOp> op = GrFillRectOp::Make(
725 fRenderTargetContext->fContext, std::move(paint), aaType, viewMatrix, rect, ss);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400726 fRenderTargetContext->addDrawOp(clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700727}
728
Chris Daltonbbfd5162017-11-07 13:35:22 -0700729bool GrRenderTargetContextPriv::drawAndStencilRect(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -0400730 const GrUserStencilSettings* ss,
731 SkRegion::Op op,
732 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500733 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400734 const SkMatrix& viewMatrix,
735 const SkRect& rect) {
robertphillips391395d2016-03-02 09:26:36 -0800736 ASSERT_SINGLE_OWNER_PRIV
737 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400738 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400739 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilRect",
740 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -0800741
Robert Phillips72152832017-01-25 17:31:35 -0500742 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -0800743
744 GrPaint paint;
robertphillips391395d2016-03-02 09:26:36 -0800745 paint.setCoverageSetOpXPFactory(op, invert);
746
Brian Salomon82f44312017-01-11 13:42:54 -0500747 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
robertphillips391395d2016-03-02 09:26:36 -0800748 return true;
749 }
robertphillips391395d2016-03-02 09:26:36 -0800750 SkPath path;
751 path.setIsVolatile(true);
752 path.addRect(rect);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500753 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
robertphillips391395d2016-03-02 09:26:36 -0800754}
755
Brian Osman11052242016-10-27 14:47:55 -0400756void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500757 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500758 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400759 const SkMatrix& viewMatrix,
760 const SkRect& rectToDraw,
761 const SkRect& localRect) {
joshualitt1de610a2016-01-06 08:26:09 -0800762 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700763 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700764 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400765 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectToRect", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700766
csmartdalton97f6cd52016-07-13 13:37:08 -0700767 SkRect croppedRect = rectToDraw;
768 SkRect croppedLocalRect = localRect;
robertphillips13a7eee2016-08-31 15:06:24 -0700769 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
770 &croppedRect, &croppedLocalRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700771 return;
772 }
773
Robert Phillips72152832017-01-25 17:31:35 -0500774 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700775
Brian Salomon7c8460e2017-05-12 11:36:10 -0400776 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500777 this->addDrawOp(clip, GrFillRectOp::MakeWithLocalRect(fContext, std::move(paint), aaType,
778 viewMatrix, croppedRect, croppedLocalRect));
joshualittb6b513b2015-08-21 10:25:18 -0700779}
780
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400781void GrRenderTargetContext::drawTexture(const GrClip& clip, sk_sp<GrTextureProxy> proxy,
Brian Osman3d139a42018-11-19 10:42:10 -0500782 GrSamplerState::Filter filter, const SkPMColor4f& color,
Brian Salomon2213ee92018-10-02 10:44:21 -0400783 const SkRect& srcRect, const SkRect& dstRect,
784 GrQuadAAFlags aaFlags,
Brian Salomonb80ffee2018-05-23 16:39:39 -0400785 SkCanvas::SrcRectConstraint constraint,
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400786 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500787 sk_sp<GrColorSpaceXform> textureColorSpaceXform) {
Brian Salomon34169692017-08-28 15:32:01 -0400788 ASSERT_SINGLE_OWNER
789 RETURN_IF_ABANDONED
790 SkDEBUGCODE(this->validate();)
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400791 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexture", fContext);
Brian Salomonf1709042018-10-03 11:57:00 -0400792 if (constraint == SkCanvas::kStrict_SrcRectConstraint &&
793 srcRect.contains(proxy->getWorstCaseBoundsRect())) {
794 constraint = SkCanvas::kFast_SrcRectConstraint;
Brian Salomon34169692017-08-28 15:32:01 -0400795 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400796 GrAAType aaType =
797 this->chooseAAType(GrAA(aaFlags != GrQuadAAFlags::kNone), GrAllowMixedSamples::kNo);
Brian Salomonff9d6d32017-08-30 10:27:49 -0400798 SkRect clippedDstRect = dstRect;
799 SkRect clippedSrcRect = srcRect;
800 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &clippedDstRect,
801 &clippedSrcRect)) {
802 return;
803 }
Brian Salomon2213ee92018-10-02 10:44:21 -0400804 auto op = GrTextureOp::Make(fContext, std::move(proxy), filter, color, clippedSrcRect,
805 clippedDstRect, aaType, aaFlags, constraint, viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500806 std::move(textureColorSpaceXform));
Brian Salomon2213ee92018-10-02 10:44:21 -0400807 this->addDrawOp(clip, std::move(op));
Brian Salomon34169692017-08-28 15:32:01 -0400808}
809
Brian Salomond7065e72018-10-12 11:42:02 -0400810void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Brian Salomond003d222018-11-26 13:25:05 -0500811 GrSamplerState::Filter filter,
Brian Salomond7065e72018-10-12 11:42:02 -0400812 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500813 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400814 ASSERT_SINGLE_OWNER
815 RETURN_IF_ABANDONED
816 SkDEBUGCODE(this->validate();)
817 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
818 GrAAType aaType = this->chooseAAType(GrAA::kYes, GrAllowMixedSamples::kNo);
Brian Salomond003d222018-11-26 13:25:05 -0500819 auto op =
820 GrTextureOp::Make(fContext, set, cnt, filter, aaType, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400821 this->addDrawOp(clip, std::move(op));
822}
823
Brian Osman11052242016-10-27 14:47:55 -0400824void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500825 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500826 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400827 const SkMatrix& viewMatrix,
828 const SkRect& rectToDraw,
829 const SkMatrix& localMatrix) {
joshualitt1de610a2016-01-06 08:26:09 -0800830 ASSERT_SINGLE_OWNER
joshualittb6b513b2015-08-21 10:25:18 -0700831 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700832 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400833 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "fillRectWithLocalMatrix", fContext);
joshualittb6b513b2015-08-21 10:25:18 -0700834
csmartdalton97f6cd52016-07-13 13:37:08 -0700835 SkRect croppedRect = rectToDraw;
robertphillips13a7eee2016-08-31 15:06:24 -0700836 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700837 return;
838 }
839
Robert Phillips72152832017-01-25 17:31:35 -0500840 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700841
Brian Salomon7c8460e2017-05-12 11:36:10 -0400842 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500843 this->addDrawOp(clip, GrFillRectOp::MakeWithLocalMatrix(fContext, std::move(paint), aaType,
844 viewMatrix, localMatrix, croppedRect));
robertphillipsea461502015-05-26 11:38:03 -0700845}
846
Brian Osman11052242016-10-27 14:47:55 -0400847void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500848 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400849 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400850 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400851 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400852 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400853 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500854 ASSERT_SINGLE_OWNER
855 RETURN_IF_ABANDONED
856 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400857 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500858
859 AutoCheckFlush acf(this->drawingManager());
860
861 SkASSERT(vertices);
Brian Salomonc2f42542017-07-12 14:11:22 -0400862 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400863 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400864 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomonf3569f02017-10-24 12:52:33 -0400865 this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400866 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700867}
868
869///////////////////////////////////////////////////////////////////////////////
870
Brian Osman11052242016-10-27 14:47:55 -0400871void GrRenderTargetContext::drawAtlas(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500872 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400873 const SkMatrix& viewMatrix,
874 int spriteCount,
875 const SkRSXform xform[],
876 const SkRect texRect[],
877 const SkColor colors[]) {
joshualitt1de610a2016-01-06 08:26:09 -0800878 ASSERT_SINGLE_OWNER
jvanverth31ff7622015-08-07 10:09:28 -0700879 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700880 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400881 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700882
Robert Phillips72152832017-01-25 17:31:35 -0500883 AutoCheckFlush acf(this->drawingManager());
halcanary9d524f22016-03-29 09:03:52 -0700884
Brian Salomon0088f942017-07-12 11:51:27 -0400885 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -0400886 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
887 aaType, spriteCount, xform, texRect, colors);
Brian Salomon0088f942017-07-12 11:51:27 -0400888 this->addDrawOp(clip, std::move(op));
jvanverth31ff7622015-08-07 10:09:28 -0700889}
890
891///////////////////////////////////////////////////////////////////////////////
892
Brian Osman11052242016-10-27 14:47:55 -0400893void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500894 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500895 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400896 const SkMatrix& viewMatrix,
897 const SkRRect& rrect,
898 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800899 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700900 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700901 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400902 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400903
904 const SkStrokeRec& stroke = style.strokeRec();
905 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700906 return;
907 }
908
bsalomon7f0d9f32016-08-15 14:49:10 -0700909 GrNoClip noclip;
910 const GrClip* clip = &origClip;
911#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
912 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500913 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700914 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
915 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
916 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
917 SkRRect devRRect;
918 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
919 clip = &noclip;
920 }
921#endif
bsalomon6663acf2016-05-10 09:14:17 -0700922 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700923
Robert Phillips72152832017-01-25 17:31:35 -0500924 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700925
Brian Salomonea26d6b2018-01-23 20:33:21 +0000926 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500927 if (GrAAType::kCoverage == aaType) {
Chris Dalton133944a2018-11-16 23:30:29 -0500928 std::unique_ptr<GrDrawOp> op;
929 if (style.isSimpleFill()) {
930 op = GrAAFillRRectOp::Make(fContext, viewMatrix, rrect, *this->caps(),
931 std::move(paint));
932 }
933 if (!op) {
Mike Klein16885072018-12-11 09:54:31 -0500934 assert_alive(paint);
Chris Dalton133944a2018-11-16 23:30:29 -0500935 op = GrOvalOpFactory::MakeRRectOp(fContext, std::move(paint), viewMatrix, rrect, stroke,
936 this->caps()->shaderCaps());
937 }
938
Brian Salomon42521e82016-12-07 16:44:58 -0500939 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -0400940 this->addDrawOp(*clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -0800941 return;
942 }
robertphillipsea461502015-05-26 11:38:03 -0700943 }
robertphillipsb56f9272016-02-25 11:03:52 -0800944
Mike Klein16885072018-12-11 09:54:31 -0500945 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500946 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
947 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700948}
949
Jim Van Verthc5903412016-11-17 15:27:09 -0500950///////////////////////////////////////////////////////////////////////////////
951
Jim Van Verth3af1af92017-05-18 15:06:54 -0400952static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
953 SkPoint3 result;
954 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
955 result.fZ = pt.fZ;
956 return result;
957}
958
959bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -0400960 const SkMatrix& viewMatrix,
961 const SkPath& path,
962 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -0500963 ASSERT_SINGLE_OWNER
Jim Van Verth3af1af92017-05-18 15:06:54 -0400964 if (this->drawingManager()->wasAbandoned()) {
965 return true;
966 }
Jim Van Verthc5903412016-11-17 15:27:09 -0500967 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400968 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -0400969
970 // check z plane
971 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
972 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
973 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
974 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
975 return false;
976 }
977
978 SkRRect rrect;
979 SkRect rect;
980 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -0500981 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -0400982 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
983 if (!isRRect &&
984 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
985 rect.width() > SK_ScalarNearlyZero) {
986 rrect.setOval(rect);
987 isRRect = true;
988 }
989 if (!isRRect && path.isRect(&rect)) {
990 rrect.setRect(rect);
991 isRRect = true;
992 }
993
994 if (!isRRect) {
995 return false;
996 }
997
Jim Van Verthc5903412016-11-17 15:27:09 -0500998 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -0400999 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001000 }
1001
Robert Phillips72152832017-01-25 17:31:35 -05001002 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001003
Jim Van Verth3af1af92017-05-18 15:06:54 -04001004 // transform light
1005 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1006
1007 // 1/scale
1008 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
1009 SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
1010 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1011 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1012
1013 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001014 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1015
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001016 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001017 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1018 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1019 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001020
1021 // Outset the shadow rrect to the border of the penumbra
1022 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1023 SkRRect ambientRRect;
1024 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1025 // If the rrect was an oval then its outset will also be one.
1026 // We set it explicitly to avoid errors.
1027 if (rrect.isOval()) {
1028 ambientRRect = SkRRect::MakeOval(outsetRect);
1029 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001030 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001031 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1032 }
1033
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001034 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001035 if (transparent) {
1036 // set a large inset to force a fill
1037 devSpaceInsetWidth = ambientRRect.width();
1038 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001039
Robert Phillips7c525e62018-06-12 10:11:12 -04001040 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1041 ambientColor,
1042 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001043 ambientRRect,
1044 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001045 devSpaceInsetWidth);
Brian Salomon05969092017-07-13 11:20:51 -04001046 SkASSERT(op);
1047 this->addDrawOp(clip, std::move(op));
Jim Van Verthc5903412016-11-17 15:27:09 -05001048 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001049
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001050 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001051 SkScalar devSpaceSpotBlur;
1052 SkScalar spotScale;
1053 SkVector spotOffset;
1054 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1055 devLightPos.fZ, rec.fLightRadius,
1056 &devSpaceSpotBlur, &spotScale, &spotOffset);
1057 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001058 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1059
Jim Van Verth3af1af92017-05-18 15:06:54 -04001060 // Adjust translate for the effect of the scale.
1061 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1062 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1063 // This offset is in dev space, need to transform it into source space.
1064 SkMatrix ctmInverse;
1065 if (viewMatrix.invert(&ctmInverse)) {
1066 ctmInverse.mapPoints(&spotOffset, 1);
1067 } else {
1068 // Since the matrix is a similarity, this should never happen, but just in case...
1069 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1070 SkASSERT(false);
1071 }
1072
1073 // Compute the transformed shadow rrect
1074 SkRRect spotShadowRRect;
1075 SkMatrix shadowTransform;
1076 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1077 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001078 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001079
1080 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001081 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001082 SkScalar insetWidth = blurOutset;
1083 if (transparent) {
1084 // If transparent, just do a fill
1085 insetWidth += spotShadowRRect.width();
1086 } else {
1087 // For shadows, instead of using a stroke we specify an inset from the penumbra
1088 // border. We want to extend this inset area so that it meets up with the caster
1089 // geometry. The inset geometry will by default already be inset by the blur width.
1090 //
1091 // We compare the min and max corners inset by the radius between the original
1092 // rrect and the shadow rrect. The distance between the two plus the difference
1093 // between the scaled radius and the original radius gives the distance from the
1094 // transformed shadow shape to the original shape in that corner. The max
1095 // of these gives the maximum distance we need to cover.
1096 //
1097 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1098 // that to get the full insetWidth.
1099 SkScalar maxOffset;
1100 if (rrect.isRect()) {
1101 // Manhattan distance works better for rects
1102 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1103 rrect.rect().fLeft),
1104 SkTAbs(spotShadowRRect.rect().fTop -
1105 rrect.rect().fTop)),
1106 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1107 rrect.rect().fRight),
1108 SkTAbs(spotShadowRRect.rect().fBottom -
1109 rrect.rect().fBottom)));
1110 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001111 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001112 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1113 rrect.rect().fLeft + dr,
1114 spotShadowRRect.rect().fTop -
1115 rrect.rect().fTop + dr);
1116 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1117 rrect.rect().fRight - dr,
1118 spotShadowRRect.rect().fBottom -
1119 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001120 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1121 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001122 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001123 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001124 }
1125
1126 // Outset the shadow rrect to the border of the penumbra
1127 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1128 if (spotShadowRRect.isOval()) {
1129 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1130 } else {
1131 SkScalar outsetRad = spotRadius + blurOutset;
1132 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1133 }
1134
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001135 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001136
Robert Phillips7c525e62018-06-12 10:11:12 -04001137 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1138 spotColor,
1139 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001140 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001141 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001142 insetWidth);
1143 SkASSERT(op);
1144 this->addDrawOp(clip, std::move(op));
Jim Van Verth3af1af92017-05-18 15:06:54 -04001145 }
1146
1147 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001148}
1149
1150///////////////////////////////////////////////////////////////////////////////
1151
Brian Osman11052242016-10-27 14:47:55 -04001152bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001153 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001154 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001155 const SkMatrix& viewMatrix,
1156 const SkRRect& origOuter,
1157 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001158 SkASSERT(!origInner.isEmpty());
1159 SkASSERT(!origOuter.isEmpty());
1160
Brian Salomon65749212017-12-01 16:01:47 -05001161 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1162
Brian Salomon45839f92017-12-04 09:02:35 -05001163 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
1164
1165 if (GrAAType::kMSAA == aaType) {
1166 return false;
1167 }
1168
Mike Reed242135a2018-02-22 13:41:39 -05001169 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1170 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001171 auto outerR = outer->width() / 2.f;
1172 auto innerR = inner->width() / 2.f;
1173 auto cx = outer->getBounds().fLeft + outerR;
1174 auto cy = outer->getBounds().fTop + outerR;
1175 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1176 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1177 auto avgR = (innerR + outerR) / 2.f;
1178 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1179 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1180 stroke.setStrokeStyle(outerR - innerR);
Robert Phillips7c525e62018-06-12 10:11:12 -04001181 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
1182 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001183 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001184 if (op) {
1185 this->addDrawOp(clip, std::move(op));
1186 return true;
1187 }
Mike Klein16885072018-12-11 09:54:31 -05001188 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001189 }
1190 }
1191
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001192 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001193 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001194 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1195 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001196 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001197 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1198 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001199 }
robertphillips00095892016-02-29 13:50:40 -08001200
robertphillips00095892016-02-29 13:50:40 -08001201 SkMatrix inverseVM;
1202 if (!viewMatrix.isIdentity()) {
1203 if (!origInner.transform(viewMatrix, inner.writable())) {
1204 return false;
1205 }
1206 if (!origOuter.transform(viewMatrix, outer.writable())) {
1207 return false;
1208 }
1209 if (!viewMatrix.invert(&inverseVM)) {
1210 return false;
1211 }
1212 } else {
1213 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001214 }
robertphillips00095892016-02-29 13:50:40 -08001215
Ethan Nicholaseace9352018-10-15 20:09:54 +00001216 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001217 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001218 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001219 if (!innerEffect) {
1220 return false;
1221 }
1222
Ethan Nicholaseace9352018-10-15 20:09:54 +00001223 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001224 if (!outerEffect) {
1225 return false;
1226 }
1227
Brian Salomon82f44312017-01-11 13:42:54 -05001228 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1229 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001230
1231 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001232 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001233 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1234 }
halcanary9d524f22016-03-29 09:03:52 -07001235
Brian Salomon82f44312017-01-11 13:42:54 -05001236 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1237 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001238 return true;
1239}
1240
Brian Osman11052242016-10-27 14:47:55 -04001241void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001242 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001243 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001244 const SkMatrix& viewMatrix,
1245 const SkRRect& outer,
1246 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001247 ASSERT_SINGLE_OWNER
1248 RETURN_IF_ABANDONED
1249 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001250 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001251
1252 SkASSERT(!outer.isEmpty());
1253 SkASSERT(!inner.isEmpty());
1254
Robert Phillips72152832017-01-25 17:31:35 -05001255 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001256
Brian Salomon82f44312017-01-11 13:42:54 -05001257 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001258 return;
1259 }
Mike Klein16885072018-12-11 09:54:31 -05001260 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001261
1262 SkPath path;
1263 path.setIsVolatile(true);
1264 path.addRRect(inner);
1265 path.addRRect(outer);
1266 path.setFillType(SkPath::kEvenOdd_FillType);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001267 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001268}
1269
robertphillipsea461502015-05-26 11:38:03 -07001270///////////////////////////////////////////////////////////////////////////////
1271
Brian Osman11052242016-10-27 14:47:55 -04001272void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001273 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001274 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001275 const SkMatrix& viewMatrix,
1276 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001277 const GrStyle& style,
1278 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001279 ASSERT_SINGLE_OWNER
1280 RETURN_IF_ABANDONED
1281 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001282 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001283
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001284 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001285 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001286 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001287 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001288 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1289 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001290 aa = GrAA::kNo;
1291 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001292 }
msarettcc319b92016-08-25 18:07:18 -07001293 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001294 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001295 SkPath path;
1296 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001297 path.setIsVolatile(true);
1298
Brian Salomon82f44312017-01-11 13:42:54 -05001299 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001300 }
1301
Brian Salomonf0366322017-07-11 15:53:05 -04001302 GrAAType aaType = this->chooseAAType(GrAA::kNo, GrAllowMixedSamples::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001303 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1304 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001305 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001306}
1307
Brian Osman11052242016-10-27 14:47:55 -04001308void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001309 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001310 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001311 const SkMatrix& viewMatrix,
1312 const SkRect& oval,
1313 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001314 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001315 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001316 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001317 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001318
Robert Phillips7484d202018-07-03 09:09:08 -04001319 const SkStrokeRec& stroke = style.strokeRec();
1320
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001321 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001322 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1323 return;
1324 }
1325
1326 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001327 return;
robertphillipsea461502015-05-26 11:38:03 -07001328 }
1329
Robert Phillips72152832017-01-25 17:31:35 -05001330 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001331
Brian Salomonea26d6b2018-01-23 20:33:21 +00001332 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001333 if (GrAAType::kCoverage == aaType) {
Chris Daltonebc38c92018-11-28 16:58:09 -07001334 std::unique_ptr<GrDrawOp> op;
1335 // GrAAFillRRectOp has special geometry and a fragment-shader branch to conditionally
1336 // evaluate the arc equation. This same special geometry and fragment branch also turn out
1337 // to be a substantial optimization for drawing ovals (namely, by not evaluating the arc
1338 // equation inside the oval's inner diamond). Given these optimizations, it's a clear win to
1339 // draw ovals the exact same way we do round rects.
1340 //
1341 // However, we still don't draw true circles as round rects, because it can cause perf
1342 // regressions on some platforms as compared to the dedicated circle Op.
1343 if (style.isSimpleFill() && oval.height() != oval.width()) {
1344 op = GrAAFillRRectOp::Make(fContext, viewMatrix, SkRRect::MakeOval(oval), *this->caps(),
1345 std::move(paint));
1346 }
1347 if (!op) {
Mike Klein16885072018-12-11 09:54:31 -05001348 assert_alive(paint);
Chris Daltonebc38c92018-11-28 16:58:09 -07001349 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1350 this->caps()->shaderCaps());
1351 }
1352 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001353 this->addDrawOp(clip, std::move(op));
robertphillipsb56f9272016-02-25 11:03:52 -08001354 return;
1355 }
robertphillipsea461502015-05-26 11:38:03 -07001356 }
robertphillipsb56f9272016-02-25 11:03:52 -08001357
Mike Klein16885072018-12-11 09:54:31 -05001358 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001359 this->drawShapeUsingPathRenderer(
1360 clip, std::move(paint), aa, viewMatrix,
1361 GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001362}
1363
Brian Osman11052242016-10-27 14:47:55 -04001364void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001365 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001366 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001367 const SkMatrix& viewMatrix,
1368 const SkRect& oval,
1369 SkScalar startAngle,
1370 SkScalar sweepAngle,
1371 bool useCenter,
1372 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001373 ASSERT_SINGLE_OWNER
1374 RETURN_IF_ABANDONED
1375 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001376 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001377
1378 AutoCheckFlush acf(this->drawingManager());
1379
Brian Salomonea26d6b2018-01-23 20:33:21 +00001380 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001381 if (GrAAType::kCoverage == aaType) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001382 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Robert Phillips7c525e62018-06-12 10:11:12 -04001383 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1384 std::move(paint),
Brian Salomonea26d6b2018-01-23 20:33:21 +00001385 viewMatrix,
1386 oval,
1387 startAngle,
1388 sweepAngle,
1389 useCenter,
1390 style,
1391 shaderCaps);
Brian Salomon42521e82016-12-07 16:44:58 -05001392 if (op) {
Brian Salomon05441c42017-05-15 16:45:49 -04001393 this->addDrawOp(clip, std::move(op));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001394 return;
1395 }
Mike Klein16885072018-12-11 09:54:31 -05001396 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001397 }
Brian Salomone4949402018-04-26 15:22:04 -04001398 this->drawShapeUsingPathRenderer(
1399 clip, std::move(paint), aa, viewMatrix,
1400 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001401}
1402
Brian Osman11052242016-10-27 14:47:55 -04001403void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001404 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001405 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001406 sk_sp<GrTextureProxy> image,
1407 sk_sp<GrColorSpaceXform> csxf,
1408 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001409 std::unique_ptr<SkLatticeIter> iter,
1410 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001411 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001412 RETURN_IF_ABANDONED
1413 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001414 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001415
Robert Phillips72152832017-01-25 17:31:35 -05001416 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001417
Brian Salomon2a943df2018-05-04 13:43:19 -04001418 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001419 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
1420 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001421 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001422}
1423
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001424void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1425 const SkRect& bounds) {
1426 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1427 SkASSERT(op);
1428 this->getRTOpList()->addOp(std::move(op), *this->caps());
1429}
1430
Greg Daniel51316782017-08-02 15:10:09 +00001431GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
1432 int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
robertphillips8c523e02016-07-26 07:41:00 -07001433 ASSERT_SINGLE_OWNER
Greg Daniel51316782017-08-02 15:10:09 +00001434 if (this->drawingManager()->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
robertphillips8c523e02016-07-26 07:41:00 -07001435 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001436 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "prepareForExternalIO", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001437
Greg Daniel51316782017-08-02 15:10:09 +00001438 return this->drawingManager()->prepareSurfaceForExternalIO(fRenderTargetProxy.get(),
1439 numSemaphores,
1440 backendSemaphores);
Greg Daniela5cb7812017-06-16 09:45:32 -04001441}
1442
Greg Danielc64ee462017-06-15 16:59:49 -04001443bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Greg Daniela5cb7812017-06-16 09:45:32 -04001444 const GrBackendSemaphore* waitSemaphores) {
1445 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001446 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001447 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001448 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001449
1450 AutoCheckFlush acf(this->drawingManager());
1451
Greg Danielc64ee462017-06-15 16:59:49 -04001452 if (numSemaphores && !this->caps()->fenceSyncSupport()) {
1453 return false;
1454 }
1455
Robert Phillips6be756b2018-01-16 15:07:54 -05001456 auto resourceProvider = fContext->contextPriv().resourceProvider();
1457
Greg Daniela5cb7812017-06-16 09:45:32 -04001458 SkTArray<sk_sp<GrSemaphore>> semaphores(numSemaphores);
1459 for (int i = 0; i < numSemaphores; ++i) {
Robert Phillips6be756b2018-01-16 15:07:54 -05001460 sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001461 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1462 kAdopt_GrWrapOwnership);
Robert Phillips7c525e62018-06-12 10:11:12 -04001463 std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, sema,
1464 fRenderTargetProxy.get()));
Robert Phillips2de8cfa2017-06-28 10:33:41 -04001465 this->getRTOpList()->addOp(std::move(waitOp), *this->caps());
Greg Daniela5cb7812017-06-16 09:45:32 -04001466 }
Greg Danielc64ee462017-06-15 16:59:49 -04001467 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001468}
joshualitt33a5fce2015-11-18 13:28:51 -08001469
Robert Phillips65a88fa2017-08-08 08:36:22 -04001470void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001471 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001472 this->getRTOpList()->addOp(std::move(op), *this->caps());
1473}
1474
Brian Osman11052242016-10-27 14:47:55 -04001475void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001476 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001477 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001478 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001479 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001480 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001481 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001482 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001483 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001484 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1485
Brian Salomon40b77a62017-12-22 11:25:52 -05001486 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001487
Robert Phillips27927a52018-08-20 13:18:12 -04001488 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04001489}
1490
1491void GrRenderTargetContext::drawShape(const GrClip& clip,
1492 GrPaint&& paint,
1493 GrAA aa,
1494 const SkMatrix& viewMatrix,
1495 const GrShape& shape) {
1496 ASSERT_SINGLE_OWNER
1497 RETURN_IF_ABANDONED
1498 SkDEBUGCODE(this->validate();)
1499 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
1500
Brian Salomon2fad74a2017-12-20 13:28:55 -05001501 if (shape.isEmpty()) {
1502 if (shape.inverseFilled()) {
1503 this->drawPaint(clip, std::move(paint), viewMatrix);
1504 }
1505 return;
robertphillipsea461502015-05-26 11:38:03 -07001506 }
1507
Robert Phillips72152832017-01-25 17:31:35 -05001508 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07001509
Brian Salomon2fad74a2017-12-20 13:28:55 -05001510 if (!shape.style().hasPathEffect()) {
Robert Phillips73653b42018-08-22 12:42:42 -04001511 GrAAType aaType = this->chooseAAType(aa, GrAllowMixedSamples::kNo);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001512 SkRRect rrect;
1513 // We can ignore the starting point and direction since there is no path effect.
1514 bool inverted;
1515 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
1516 if (rrect.isRect()) {
1517 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
1518 &shape.style());
1519 return;
1520 } else if (rrect.isOval()) {
1521 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07001522 return;
1523 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001524 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
1525 return;
Robert Phillips73653b42018-08-22 12:42:42 -04001526 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
1527 viewMatrix.rectStaysRect()) {
1528 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
1529 // the matrix to all the points individually rather than just to the rect
1530 SkRect rects[2];
1531 if (shape.asNestedRects(rects)) {
1532 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05001533 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04001534 fContext, std::move(paint), viewMatrix, rects);
1535 if (op) {
1536 this->addDrawOp(clip, std::move(op));
1537 }
1538 // Returning here indicates that there is nothing to draw in this case.
1539 return;
1540 }
robertphillipsea461502015-05-26 11:38:03 -07001541 }
1542 }
robertphillips4bc31812016-03-01 12:22:49 -08001543
Brian Salomon2fad74a2017-12-20 13:28:55 -05001544 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07001545}
1546
Chris Daltonbbfd5162017-11-07 13:35:22 -07001547bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04001548 const GrUserStencilSettings* ss,
1549 SkRegion::Op op,
1550 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001551 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001552 const SkMatrix& viewMatrix,
1553 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08001554 ASSERT_SINGLE_OWNER_PRIV
1555 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04001556 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001557 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
1558 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08001559
1560 if (path.isEmpty() && path.isInverseFillType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001561 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
Brian Osman11052242016-10-27 14:47:55 -04001562 SkRect::MakeIWH(fRenderTargetContext->width(),
1563 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08001564 return true;
1565 }
1566
Robert Phillips72152832017-01-25 17:31:35 -05001567 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08001568
1569 // An Assumption here is that path renderer would use some form of tweaking
1570 // the src color (either the input alpha or in the frag shader) to implement
1571 // aa. If we have some future driver-mojo path AA that can do the right
1572 // thing WRT to the blend then we'll need some query on the PR.
Brian Salomon7c8460e2017-05-12 11:36:10 -04001573 GrAAType aaType = fRenderTargetContext->chooseAAType(aa, GrAllowMixedSamples::kNo);
robertphillips976f5f02016-06-03 10:59:20 -07001574 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08001575
Chris Daltondb91c6e2017-09-08 16:25:08 -06001576 SkIRect clipConservativeBounds;
1577 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
1578 &clipConservativeBounds, nullptr);
1579
bsalomon8acedde2016-06-24 10:42:16 -07001580 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08001581 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001582 canDrawArgs.fCaps = fRenderTargetContext->caps();
robertphillips391395d2016-03-02 09:26:36 -08001583 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07001584 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001585 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001586 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05001587 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
1588 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07001589 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08001590
1591 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05001592 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05001593 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08001594 if (!pr) {
1595 return false;
1596 }
1597
1598 GrPaint paint;
1599 paint.setCoverageSetOpXPFactory(op, invert);
1600
Brian Salomonf3569f02017-10-24 12:52:33 -04001601 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
1602 std::move(paint),
1603 ss,
1604 fRenderTargetContext,
1605 &clip,
1606 &clipConservativeBounds,
1607 &viewMatrix,
1608 &shape,
1609 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001610 fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08001611 pr->drawPath(args);
1612 return true;
1613}
1614
Brian Osman11052242016-10-27 14:47:55 -04001615SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07001616 ASSERT_SINGLE_OWNER_PRIV
1617
Brian Osman11052242016-10-27 14:47:55 -04001618 if (fRenderTargetContext->wasAbandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07001619 return SkBudgeted::kNo;
1620 }
1621
Brian Osman11052242016-10-27 14:47:55 -04001622 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07001623
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001624 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07001625}
1626
Brian Salomon2fad74a2017-12-20 13:28:55 -05001627void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
1628 GrPaint&& paint,
1629 GrAA aa,
1630 const SkMatrix& viewMatrix,
1631 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08001632 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001633 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04001634 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
1635
Jim Van Verthf86073a2018-10-02 13:05:38 -04001636 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
1637 return;
1638 }
1639
Chris Daltondb91c6e2017-09-08 16:25:08 -06001640 SkIRect clipConservativeBounds;
1641 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
1642
Brian Salomon2fad74a2017-12-20 13:28:55 -05001643 GrShape tempShape;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001644 // NVPR cannot handle hairlines, so this would get picked up by a different stencil and
1645 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1646 // smoother hairlines than MSAA.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001647 GrAllowMixedSamples allowMixedSamples = originalShape.style().isSimpleHairline()
1648 ? GrAllowMixedSamples::kNo
1649 : GrAllowMixedSamples::kYes;
Brian Salomon7c8460e2017-05-12 11:36:10 -04001650 GrAAType aaType = this->chooseAAType(aa, allowMixedSamples);
robertphillips68737822015-10-29 12:12:21 -07001651 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001652 canDrawArgs.fCaps = this->caps();
robertphillips68737822015-10-29 12:12:21 -07001653 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001654 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06001655 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05001656 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001657 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07001658
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001659 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05001660 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05001661 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001662 return;
1663 }
1664
1665 canDrawArgs.fAAType = aaType;
1666
1667 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1668 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1669 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1670
Brian Salomon2fad74a2017-12-20 13:28:55 -05001671 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001672 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05001673 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1674 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001675 return;
1676 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001677 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05001678 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001679 }
1680 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05001681 if (canDrawArgs.fShape->style().applies()) {
1682 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
1683 styleScale);
1684 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07001685 return;
1686 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05001687 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04001688 // This time, allow SW renderer
1689 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1690 } else {
1691 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07001692 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05001693 }
robertphillipsea461502015-05-26 11:38:03 -07001694
bsalomon8acedde2016-06-24 10:42:16 -07001695 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07001696#ifdef SK_DEBUG
1697 SkDebugf("Unable to find path renderer compatible with path.\n");
1698#endif
1699 return;
1700 }
1701
Robert Phillips256c37b2017-03-01 14:32:46 -05001702 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05001703 std::move(paint),
1704 &GrUserStencilSettings::kUnused,
1705 this,
1706 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06001707 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05001708 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05001709 canDrawArgs.fShape,
Brian Salomon82f44312017-01-11 13:42:54 -05001710 aaType,
Brian Osman34ec3742018-07-03 10:40:57 -04001711 this->colorSpaceInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07001712 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07001713}
1714
Brian Salomon467921e2017-03-06 16:17:12 -05001715static void op_bounds(SkRect* bounds, const GrOp* op) {
1716 *bounds = op->bounds();
1717 if (op->hasZeroArea()) {
1718 if (op->hasAABloat()) {
1719 bounds->outset(0.5f, 0.5f);
1720 } else {
1721 // We don't know which way the particular GPU will snap lines or points at integer
1722 // coords. So we ensure that the bounds is large enough for either snap.
1723 SkRect before = *bounds;
1724 bounds->roundOut(bounds);
1725 if (bounds->fLeft == before.fLeft) {
1726 bounds->fLeft -= 1;
1727 }
1728 if (bounds->fTop == before.fTop) {
1729 bounds->fTop -= 1;
1730 }
1731 if (bounds->fRight == before.fRight) {
1732 bounds->fRight += 1;
1733 }
1734 if (bounds->fBottom == before.fBottom) {
1735 bounds->fBottom += 1;
1736 }
1737 }
1738 }
1739}
1740
Brian Salomon348a0372018-10-31 10:42:18 -04001741void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
1742 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08001743 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -05001744 if (this->drawingManager()->wasAbandoned()) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001745 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001746 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05001747 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001748 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04001749 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001750 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07001751
Brian Salomon467921e2017-03-06 16:17:12 -05001752 // Setup clip
1753 SkRect bounds;
1754 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04001755 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04001756 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1757 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1758 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1759 &bounds)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001760 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001761 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001762 }
1763
Brian Salomon54d212e2017-03-21 14:22:38 -04001764 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1765 appliedClip.hasStencilClip()) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04001766 this->getOpList()->setStencilLoadOp(GrLoadOp::kClear);
Robert Phillips95214472017-08-08 18:00:03 -04001767
Robert Phillips65048132017-08-10 08:44:49 -04001768 this->setNeedsStencil();
Brian Salomon54d212e2017-03-21 14:22:38 -04001769 }
1770
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001771 GrXferProcessor::DstProxy dstProxy;
Brian Osman532b3f92018-07-11 10:02:07 -04001772 if (GrDrawOp::RequiresDstTexture::kYes == op->finalize(*this->caps(), &appliedClip)) {
Brian Salomon09181ef2018-11-14 13:39:51 -05001773 if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, *op, &dstProxy)) {
Robert Phillipsc994a932018-06-19 13:09:54 -04001774 fContext->contextPriv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04001775 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04001776 }
1777 }
1778
1779 op->setClippedBounds(bounds);
Brian Salomon348a0372018-10-31 10:42:18 -04001780 auto opList = this->getRTOpList();
1781 if (willAddFn) {
1782 willAddFn(op.get(), opList->uniqueID());
1783 }
1784 opList->addOp(std::move(op), *this->caps(), std::move(appliedClip), dstProxy);
Brian Salomon54d212e2017-03-21 14:22:38 -04001785}
1786
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001787bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip,
Brian Salomon09181ef2018-11-14 13:39:51 -05001788 const GrOp& op,
Robert Phillips16d8ec62017-07-27 16:16:25 -04001789 GrXferProcessor::DstProxy* dstProxy) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001790 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
1791 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
1792 // start and stop the render pass in order to make the copy.
1793 if (rtProxy->wrapsVkSecondaryCB()) {
1794 return false;
1795 }
1796
Brian Salomon467921e2017-03-06 16:17:12 -05001797 if (this->caps()->textureBarrierSupport()) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001798 if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05001799 // The render target is a texture, so we can read from it directly in the shader. The XP
1800 // will be responsible to detect this situation and request a texture barrier.
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001801 dstProxy->setProxy(sk_ref_sp(texProxy));
1802 dstProxy->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001803 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05001804 }
1805 }
1806
Robert Phillipsbf25d432017-04-07 10:08:53 -04001807 SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height());
Brian Salomon467921e2017-03-06 16:17:12 -05001808
Eric Karl74480882017-04-03 14:49:05 -07001809 SkIRect clippedRect;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001810 clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05001811 SkRect opBounds = op.bounds();
1812 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
1813 // 0.5 pixels.
1814 if (op.hasAABloat() || op.hasZeroArea()) {
1815 opBounds.outset(0.5f, 0.5f);
1816 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
1817 // performance we may ignore the clip when the draw is entirely inside the clip is float
1818 // space but will hit pixels just outside the clip when actually rasterizing.
1819 clippedRect.outset(1, 1);
1820 clippedRect.intersect(SkIRect::MakeWH(rtProxy->width(), rtProxy->height()));
1821 }
1822 SkIRect opIBounds;
1823 opBounds.roundOut(&opIBounds);
1824 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001825#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04001826 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05001827#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04001828 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05001829 }
1830
1831 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1832 // have per-sample dst values by making the copy multisampled.
1833 GrSurfaceDesc desc;
Eric Karl74480882017-04-03 14:49:05 -07001834 bool rectsMustMatch = false;
1835 bool disallowSubrect = false;
Brian Salomon2a4f9832018-03-03 22:43:43 -05001836 GrSurfaceOrigin origin;
1837 if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &origin, &rectsMustMatch,
1838 &disallowSubrect)) {
Brian Salomon467921e2017-03-06 16:17:12 -05001839 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsbf25d432017-04-07 10:08:53 -04001840 desc.fConfig = rtProxy->config();
Greg Daniel1efe3222018-04-04 14:02:51 -04001841 origin = rtProxy->origin();
Brian Salomon467921e2017-03-06 16:17:12 -05001842 }
1843
Eric Karl74480882017-04-03 14:49:05 -07001844 if (!disallowSubrect) {
1845 copyRect = clippedRect;
1846 }
Brian Salomon467921e2017-03-06 16:17:12 -05001847
Robert Phillipsbf25d432017-04-07 10:08:53 -04001848 SkIPoint dstPoint, dstOffset;
1849 SkBackingFit fit;
Eric Karl74480882017-04-03 14:49:05 -07001850 if (rectsMustMatch) {
Robert Phillipsbf25d432017-04-07 10:08:53 -04001851 desc.fWidth = rtProxy->width();
1852 desc.fHeight = rtProxy->height();
Eric Karl74480882017-04-03 14:49:05 -07001853 dstPoint = {copyRect.fLeft, copyRect.fTop};
1854 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001855 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07001856 } else {
1857 desc.fWidth = copyRect.width();
1858 desc.fHeight = copyRect.height();
1859 dstPoint = {0, 0};
1860 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04001861 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07001862 }
Brian Salomon467921e2017-03-06 16:17:12 -05001863
Greg Daniel4065d452018-11-16 15:43:41 -05001864 SkASSERT(rtProxy->backendFormat().textureType() == GrTextureType::k2D);
1865 const GrBackendFormat& format = rtProxy->backendFormat();
Robert Phillipsbf25d432017-04-07 10:08:53 -04001866 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001867 format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
Brian Salomonf802e752018-02-13 17:13:31 -05001868 sk_ref_sp(this->colorSpaceInfo().colorSpace()));
Robert Phillipsbf25d432017-04-07 10:08:53 -04001869 if (!sContext) {
1870 SkDebugf("setupDstTexture: surfaceContext creation failed.\n");
1871 return false;
1872 }
1873
1874 if (!sContext->copy(rtProxy, copyRect, dstPoint)) {
1875 SkDebugf("setupDstTexture: copy failed.\n");
1876 return false;
1877 }
1878
Robert Phillipsbb581ce2017-05-29 15:05:15 -04001879 dstProxy->setProxy(sContext->asTextureProxyRef());
1880 dstProxy->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04001881 return true;
robertphillips2334fb62015-06-17 05:43:33 -07001882}